Go Down

Topic: If button held down during power up = change arduino program? (Read 156 times) previous topic - next topic

Konsolkongen

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

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 :)

This is basically what I've got:
Code: [Select]
//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.

larryd

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


arduinoMode = 1
OR
arduinoMode == 1

You have this in setup()
Code: [Select]
   if (digitalRead(button1) == LOW) { // If button is pressed during powering on.
      arduinoMode = 1; // Secondary mode enabled
    }

Then you have this in loop()
Code: [Select]
 arduinoMode = digitalRead (2);

button1 = 2,   be constant.

No technical PMs.
The last thing you did is where you should start looking.

Robin2

Also, this is wrong
Code: [Select]
if (arduinoMode = 0
it should be
Code: [Select]
if (arduinoMode == 0

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Konsolkongen

Thank you both for the quick replies :)

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 :)

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Konsolkongen

Sure :)

Code: [Select]
//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);
  }
}

Robin2

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

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Konsolkongen

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 :)

Go Up