If button held down during power up = change arduino program?

Hi. This is my first time posting here, and I have very little experience with the Arduino. So please go easy on me :slight_smile:

I have completed most of the projects in the Arduino Projects Book, so now I wanted to try and make what should be a very simple program. However, while I have been googling and trying various things since yesterday I haven't been able to proceed, so I'm hoping someone here can help me get further.

I have 3 buttons hooked up to pin 2, 3 and 4. All are pulled high with 10k resistors.

I have 2 outputs that are also pulled high to pin 8 and 9, with LEDs connected to show that they work.

What I want to do is to have two different programs running depending on if button 1 has been pressed during the first second after powering on. This will never have to be changed again once the power is on.

The difference of these programs is which outputs are pulled low (LEDs lighting up), when button 2 and 3 are pressed.

One of the LEDs should only light up when BOTH button 2 and 3 are pressed at the same time. The other LED should light up when button 3 is pressed. This bit I have kinda figured out.

I'm sure I'm doing a lot of things wrong, but here goes. I have have tried using the "millis" command during the setup to determine which program is being used after the one second has passed.

This seemed promising enough to begin with, but I never got the results I wanted. When reading the serial monitor this almost always seemed to cause the wrong state (1 instead 0).
Unfortunately the code has been messed up a bit, because I wasn't aware that it was being saved automatically :slight_smile:

This is basically what I've got:

//inputs:
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;

//outputs:
const int out1 = 8;
const int out2 = 9;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int arduinoMode = 0;

void setup(){
  Serial.begin(9600);
  
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);

    while (millis() < 1000) { //A one second delay from powering on the Arduino.
    if (digitalRead(button1) == LOW) { // If button is pressed during powering on.
      arduinoMode = 1; // Secondary mode enabled
    }
    else {
      arduinoMode = 0; // Secondary mode disabled.
    }
  }
  
}
void loop(){
  button2State = digitalRead(3);
  button3State = digitalRead(4);
  arduinoMode = digitalRead (2);

  Serial.println(arduinoMode);

// Primary:
  if (arduinoMode = 0 && button2State == LOW  && button3State == LOW) {
      digitalWrite(out1, LOW);
    }
    else {
      digitalWrite(out1, HIGH);
    }
  if (arduinoMode = 0 && button3State == LOW) {
    digitalWrite(out2, LOW);
  }
  else {
    digitalWrite(out2, HIGH);
  }
  
// Secondary:
  if (arduinoMode = 1 && button2State == LOW  && button3State == LOW) {
      digitalWrite(out2, LOW);
    }
    else {
      digitalWrite(out2, HIGH);
    }
  if (arduinoMode = 1 && button3State == LOW) {
    digitalWrite(out1, LOW);
  }
  else {
    digitalWrite(out1, HIGH);
  }
}

Any help would be greatly appreciated. There is probably a super simple way to do this.

arduinoMode = 0 &&
OR
arduinoMode == 0 &&

arduinoMode = 1
OR
arduinoMode == 1

You have this in setup()

   if (digitalRead(button1) == LOW) { // If button is pressed during powering on.
      arduinoMode = 1; // Secondary mode enabled
    }

Then you have this in loop()

 arduinoMode = digitalRead (2);

button1 = 2, be constant.

Also, this is wrong

if (arduinoMode = 0

it should be

if (arduinoMode == 0

...R

Thank you both for the quick replies :slight_smile:

larryd>

I'm very sorry, but I don't understand what errors you are trying to point out.

Robin2>

Of course, that's a stupid mistake. For what it's worth I did have it like that yesterday, but I forgot now that I was trying to get the code back together so I could post it here :slight_smile:

Konsolkongen:
Of course, that's a stupid mistake.

That can happen to anyone. But now that it is pointed out please post the latest version of the program (in a new Reply so that your earlier version can still be viewed - being able to compare versions can be very helpful)

...R

Sure :slight_smile:

//inputs:
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;

//outputs:
const int out1 = 8;
const int out2 = 9;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int arduinoMode = 0;

void setup(){
  Serial.begin(9600);
  
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);

    while (millis() < 1000) { //A one second delay from powering on the Arduino.
    if (digitalRead(button1) == LOW) { // If button is pressed during powering on.
      arduinoMode == 1; // Secondary mode enabled
    }
    else {
      arduinoMode == 0; // Secondary mode disabled.
    }
  }
  
}
void loop(){
  button2State = digitalRead(3);
  button3State = digitalRead(4);
  arduinoMode = digitalRead (2);

  Serial.println(arduinoMode);

// Primary:
  if (arduinoMode == 0 && button2State == LOW  && button3State == LOW) {
      digitalWrite(out1, LOW);
    }
    else {
      digitalWrite(out1, HIGH);
    }
  if (arduinoMode == 0 && button3State == LOW) {
    digitalWrite(out2, LOW);
  }
  else {
    digitalWrite(out2, HIGH);
  }
  
// Secondary:
  if (arduinoMode == 1 && button2State == LOW  && button3State == LOW) {
      digitalWrite(out2, LOW);
    }
    else {
      digitalWrite(out2, HIGH);
    }
  if (arduinoMode == 1 && button3State == LOW) {
    digitalWrite(out1, LOW);
  }
  else {
    digitalWrite(out1, HIGH);
  }
}

If the purpose of arduinoMode is to detect a button state in setup() then you should NOT be changing its value in loop()

...R

Thank you again. I've been held up by other things, so I'll give this a go ASAP. I really do appreciate the help though :slight_smile: