Multi Mode PUSHBUTTON (NON HOLDING)

Hello Help with a newbie here is incredibly grateful.

LED's are going to represent outputs, eventually I intend on changing to transistors to drive relays.

what do I need to do...

int pushbutton1 = 13; //Pin to send commands OPEN - STOP - CLOSE then Back to Open again...

OPEN MODE (needs a timer for 30 Seconds) (without DELAY) but I will get to this much much later.

Interrupt (if I have got this correct) I want to have the ability for the same button during a mode if selected in OPEN and CLOSE mode to PAUSE the Program. (again I expect to do this allot later on)

Pins as follows

int reading;
int LEDSTOP = 11; //Enables LEDSTOP (NO RELAYS ON) so = STOP
int LEDoPEN = 10; //Enables LEDOPEN to Turn ON RELAY 1
int LEDCLOSE = 9; //Enables LEDCLOSE to Turn ON RELAY 2

so far I have done the following (mixing and matching with code I have found online along with my own mess)

(I want to use serial so I can visually see what is going on as and when I press the button, I also have tried to implement and debounce but once again no idea what I am doing.

I am trying to learn through just editing and compiling and I clearly still do not know what I am doing... but I am really eager to learn.

please can some one look at the bellow and give me their thoughts, Hell even deconstructive criticism if you are that offended is fine as long as you follow with a constructed comment.

Many Thanks

Aaron

int counter = 0; //system starts Counter on 0

int pushbutton1 = 13; //Pin to OPEN & CLOSE
int reading;
int LEDSTOP = 11; //Enables LEDSTOP (NO RELAYS ON) so = STOP
int LEDoPEN = 10; //Enables LEDOPEN to Turn ON RELAY 1
int LEDCLOSE = 9; //Enables LEDCLOSE to Turn ON RELAY 2

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers

void setup()

{
pinMode(pushbutton1, INPUT);
pinMode(LEDSTOP, OUTPUT);
pinMode(LEDoPEN, OUTPUT);
pinMode(LEDCLOSE, OUTPUT);
Serial.begin(9600);
} //Open Serial Monitor

void loop()
//Handle input
{
pushbutton1 = digitalRead(pushbutton1);
int switchVal = (digitalRead(pushbutton1) == HIGH);
if (pushbutton1 == HIGH && previous == LOW && millis() - time > debounce)
{ if (LEDSTOP == HIGH)

if (switchVal == HIGH)
}
state = LOW;
else
state = HIGH;
}

{ counter ++; //Reset count if over max mode number
if (counter == 4)
{counter = 0;}
}
else
//change mode
switch (counter) {
case 1:
digitalWrite(LEDSTOP, HIGH);Serial.println("LEDSTOP");
break;
case 2:
digitalWrite(LEDoPEN, HIGH);Serial.println("LEDOPEN");
break;
case 3:
digitalWrite(LEDCLOSE, HIGH);Serial.println("LEDCLOSE");
analogWrite(LEDCLOSE, 0);
break; }

Interrupt (if I have got this correct) I want to have the ability for the same button during a mode if selected in OPEN and CLOSE mode to PAUSE the Program. (again I expect to do this allot later on)

You don't need interrupts. Write non-blocking code, and poll the switches often. If you write blocking code, the blocking function will be interrupted, but control will return right back to the blocking function, having wasted the effort.

  pushbutton1 = digitalRead(pushbutton1);

Read the state of a pin, and overwrite the value of the pin with the state of that pin. Hardly seems logical.

if (LEDSTOP == HIGH)

Eleven is not HIGH.

Put every { and every } on a new line BY ITSELF. Use Tools + Auto Format to clean up that awful indenting.

pushbutton1 = digitalRead(pushbutton1);

is this what you mean, not sure I follow (clearly something staring me right in the face)

such as.....

pushbutton1 = digitalRead(INPUT);

The digitalRead() function takes an argument. What is that argument? A pin number, right?

It returns the state of the pin. You overwrite the pin number with the state. From then on, you read from a different pin - either 0 or 1, depending on the state of the pin the first time the Arduino starts up.

in my case would it be?

pushbutton1 = digitalRead(INPUT);
int switchVal = (digitalRead(INPUT) == HIGH);

my argument is a digital read (constantly read) and next val is it needs to be HIGH?

I think this is what you are trying to write. The reason I wrote it is to be able to point out some of the problems

byte switchVal = 0;
byte prevSwitch = 0;
byte counter = 0;
const byte pushbutton1 = 13;// this stops anyone trying to change the pin number
const byte LEDSTOP = 11;
const byte LEDoPEN = 10;
const byte LEDCLOSE = 9;
//in the code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (pushbutton1, INPUT);//research pullup when using a pushbutton
  pinMode (LEDSTOP, OUTPUT );//stick to a pattern with capital letters
  pinMode (LEDoPEN, OUTPUT);//reverse camel code ?
  pinMode (LEDCLOSE, OUTPUT);
}

void loop() {

  switchVal = digitalRead(pushbutton1);
  //switchval now equals what ever was read by digitalread pin 13

  //if switchval is high and prevswitch is low add one to counter then
  // change prevswitch to 1 to block this line being repeated while finger
  // is on the button
  if ((switchVal == HIGH) && (prevSwitch == 0)) {
    counter ++;
    prevSwitch = 1;
  }

  //once you remove your finger from the button it will no longer be repeated
  //so remove the block by setting prevswitch to 0
  else if (switchVal == LOW) {
    prevSwitch = 0;
  }

  if (counter >= 4) {
    counter = 1;
    //telling counter to go to 0 when there
    //is no 0 kinda seems pointless
  }

  switch (counter) {
    case 1:
      digitalWrite(LEDSTOP, HIGH); Serial.println("LEDSTOP");
      break;
    case 2:
      digitalWrite(LEDoPEN, HIGH); Serial.println("LEDOPEN");
      //maybe i should be turning the ledstop off at this point
      break;
    case 3:
      digitalWrite(LEDCLOSE, HIGH); Serial.println("LEDCLOSE");
      //analogWrite(LEDCLOSE, 0); no idea why you what you are thinking here?
      break;


      // ok we turned the leds on.
      // theres no code to turn them off?
  }

}

I want to have the ability for the same button during a mode if selected in OPEN and CLOSE mode to PAUSE the Program. (again I expect to do this allot later on)

don't think about pausing or stopping a program

if you select open and open needs 50 seconds to do its thing then you don't pause for 50 seconds you just ignore/skip other sections of the program until open has done its thing.

gpop1:
I think this is what you are trying to write. The reason I wrote it is to be able to point out some of the problems

why change from int to const byte?

byte switchVal = 0;

byte prevSwitch = 0;
byte counter = 0;
const byte pushbutton1 = 13;// this stops anyone trying to change the pin number
const byte LEDSTOP = 11;
const byte LEDoPEN = 10;
const byte LEDCLOSE = 9;
//in the code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (pushbutton1, INPUT);//research pullup when using a pushbutton
  pinMode (LEDSTOP, OUTPUT );//stick to a pattern with capital letters
  pinMode (LEDoPEN, OUTPUT);//reverse camel code ?
  pinMode (LEDCLOSE, OUTPUT);
}

void loop() {

switchVal = digitalRead(pushbutton1);
  //switchval now equals what ever was read by digitalread pin 13

//if switchval is high and prevswitch is low add one to counter then
  // change prevswitch to 1 to block this line being repeated while finger
  // is on the button
  if ((switchVal == HIGH) && (prevSwitch == 0)) {
    counter ++;
    prevSwitch = 1;
  }

//once you remove your finger from the button it will no longer be repeated
  //so remove the block by setting prevswitch to 0
  else if (switchVal == LOW) {
    prevSwitch = 0;
  }

if (counter >= 4) {
    counter = 1;
    //telling counter to go to 0 when there
    //is no 0 kinda seems pointless
  }

switch (counter) {
    case 1:
      digitalWrite(LEDSTOP, HIGH); Serial.println("LEDSTOP");
      break;
    case 2:
      digitalWrite(LEDoPEN, HIGH); Serial.println("LEDOPEN");
      //maybe i should be turning the ledstop off at this point
      break;
    case 3:
      digitalWrite(LEDCLOSE, HIGH); Serial.println("LEDCLOSE");
      //analogWrite(LEDCLOSE, 0); no idea why you what you are thinking here?
      break;

// ok we turned the leds on.
      // theres no code to turn them off?
  }

}

why change from int to const byte?

a byte is big enough to hold any pin number on a arduino so why waste memory using a larger int

do you really want some one in the program writing a different pin number. If that sounds like a bad idea then make it a const so it can not be changed.

Okay this makes sense, but then again you just stated that it cannot be changed, so does that mean that once its been set it will stay set until it has been restarted?

sorry I am trying to fully understand, as allot of the tutorials I have taken and seen have not used it just yet.

Thanks

if you add the word const before byte then it can not be changed.

const byte LEDSTOP = 11;

the line above says that LEDSTOP=11 and it can not be changed while the code is running
In simple terms everything moves from right to left so you have a bucket labeled LEDSTOP that contains 11.

pinMode (LEDSTOP, OUTPUT );

the line above says set pin 11 as a output.
you could have just skipped the first line and said pinMode (11, OUTPUT ); but its a pain to remember what pin is doing what so its easier to use a name that makes sense.
Notice that we are not setting a state like high or low at this point. We are just telling the arduino that we wish pin 11 to be a output pin

digitalWrite(LEDSTOP, HIGH);

the line above says write a digital output to pin 11 make the digital write high
read is a input write is a output
digital can either be a 0 or a 1.
0 can also be called low or false, 1 can also be called high or true.
so the arduino knows you want to set a digital pin to........get the bucket with the memory tag LEDSTOP and look in the bucket for the pin number. Well that's pin 11.
Now what would you like to write to that pin. HIGH means write 1 to that pin which means it turns on.
remember that outputs are set then remain set. So if we turn the pin on it stays on forever unless we tell it to turn off or reboot.

to turn the pin off we would have to say
digitalWrite(LEDSTOP, LOW);
meaning pin 11 set to 0 which is the same as turning the pin off

does this make sense?

this code working fine seems.can you elobrate what are operation need to perform when

mode = open;
mode= close;
mode =stop;

what operational differnce makes in open & stop Here. ELobarte problem statement . how you wanna see your output.

int  switchVal = 0;
int prevSwitch = 0;
static int counter = 0;
const int pushbutton1 = 8;// this stops anyone trying to change the pin number
const int LEDSTOP = 11;
const int LEDoPEN = 10;
const int LEDCLOSE = 9;
//in the code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (pushbutton1, INPUT);//research pullup when using a pushbutton
  pinMode (LEDSTOP, OUTPUT );//stick to a pattern with capital letters
  pinMode (LEDoPEN, OUTPUT);//reverse camel code ?
  pinMode (LEDCLOSE, OUTPUT);
}

void loop() {

  switchVal = digitalRead(pushbutton1);
  Serial.print("switchVal:");Serial.println(switchVal);
  if ((switchVal == HIGH) && (prevSwitch == 0)) {
    counter ++;
    prevSwitch = 1;
  }

 else if (switchVal == LOW)
  {
    prevSwitch = 0;
  }

  if (counter >= 4) {
    counter = 1;
    //telling counter to go to 0 when there
    //is no 0 kinda seems pointless
  }

Serial.print("counter:");Serial.println(counter);
  switch (counter) {
  case 1:
    digitalWrite(LEDSTOP, HIGH); 
    Serial.println("LEDSTOP");
    break;
  case 2:
    digitalWrite(LEDoPEN, HIGH); 
    Serial.println("LEDOPEN");
    //maybe i should be turning the ledstop off at this point
    break;
  case 3:
    digitalWrite(LEDCLOSE, HIGH); 
    Serial.println("LEDCLOSE");
    //analogWrite(LEDCLOSE, 0); no idea why you what you are thinking here?
    break;

  }
  
  Serial.println("...................................");
  delay(1000);

}