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:
//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.