I'm kind of new in the Arduino world. I decided to make a self learning project with an Arduino where it controls a conveyor that simply moves from right to left and back again. There is a start and stop button. On each end of the conveyor are inductive sensors to stop the motor when it reaches the end position. After a delay, the conveyer starts again the opposite direction. Inside the code I have a cycle counter that stops the conveyer after a certain number of cycles. Now my question is, how could I change the value of the cycle counter using two push buttons. One to increase the other to decrease.
If there is somebody who could help I would appreciate. Thanks already
const byte sensorLinksEnd = 2;
const byte sensorRechtsEnd = 3;
const byte startPin = 4;
const byte stopPin = 5;
const byte relaisLinks = 6;
const byte relaisRechts = 7;
enum {relaisAn, relaisAus};
enum {warten, on, rechts, haltrechts, links, haltlinks, off};
byte schritt = warten;
byte zyklus = 20; //cycles
const byte maxzyklus = 20; //max cycles
void setup() {
Serial.begin(115200);
Serial.println(F("Start...."));
pinMode(sensorLinksEnd, INPUT);
pinMode(sensorRechtsEnd, INPUT);
pinMode(startPin, INPUT);
pinMode(stopPin, INPUT);
pinMode(relaisLinks, OUTPUT);
digitalWrite(relaisLinks, relaisAus);
pinMode(relaisRechts, OUTPUT);
digitalWrite(relaisRechts, relaisAus);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
schrittkette();
}
void schrittkette()
{
static unsigned long lastmillis;
const unsigned long pauseZeit = 5000; // in ms
if (digitalRead(stopPin))
{
schritt = off;
}
switch (schritt)
{
case warten:
if (digitalRead(startPin))
{
zyklus = 0;
schritt = on;
}
digitalWrite(relaisRechts, relaisAus);
digitalWrite(relaisLinks, relaisAus);
break;
case on:
digitalWrite(LED_BUILTIN, HIGH);
schritt = rechts;
break;
case rechts:
digitalWrite(relaisRechts, relaisAn);
if (digitalRead(sensorRechtsEnd))
{
digitalWrite(relaisRechts, relaisAus);
schritt = haltrechts;
lastmillis = millis();
}
break;
case haltrechts:
digitalWrite(relaisRechts, relaisAus);
digitalWrite(relaisLinks, relaisAus);
if (millis() - lastmillis > pauseZeit)
{
schritt = links;
}
break;
case links:
digitalWrite(relaisRechts, relaisAus);
digitalWrite(relaisLinks, relaisAn);
if (digitalRead(sensorLinksEnd))
{
digitalWrite(relaisLinks, relaisAus);
schritt = haltlinks;
lastmillis = millis();
}
break;
case haltlinks:
digitalWrite(relaisRechts, relaisAus);
digitalWrite(relaisLinks, relaisAus);
if (millis() - lastmillis > pauseZeit)
{
if (zyklus == maxzyklus)
{
schritt = off;
}
else
{
zyklus++;
schritt = rechts;
}
}
break;
case off:
digitalWrite(LED_BUILTIN, LOW);
schritt = warten;
break;
}
}