So for school i have to make a code for my arduino, that let's me turn 4 led's on and off (as an input) with 2 buttons (one to select the led, one to toggle the led), run the inputs trough a logic circuit and based on the output toggle a fifth led on or off. The only thing i can't manage to get working is the button that turns the leds on and off. My main problem is that if i want to let the second button (setButton) do anything, i have to hold it down and press the other button which then kind off controls setButton & inputButton at the same time..
so my main question: How do i make my setButton work in this script.. (sorry for the Dutch comments inbetween)
int led1 = 5; // van links naar rechts: led 1
int led2 = 6; //led 2
int led3 = 9; //led 3
int led4 = 10; //led 4
int ledRuben = 3; //led 5 (geeft de output aan)
const int inputButton = 7; //select a led/input mode on
const int setButton = 8; //toggle a led
int buttonState1; //inputButton state
int buttonState2; //setButton state
int pushCount = 0; //how many times the button is pushed (used to select the right led)
int lastButtonState1; //last inputButton state
int lastButtonState2; //last setButton state
int inputLeds[4] = { 0, 0, 0, 0 }; //used to save the signal needed for the logic gates
char currentLed[4] = { led1, led2, led3, led4 }; //used to select a led
int i16, i09, i61, i34; //inputs
int t16, t09, t61, ruben; //in between and end signals
int ledState = false; //current led on or not
void setup() {
Serial.begin(9600); // hoe veel data er verstuurd wordt.
pinMode(ruben, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(inputButton, INPUT);
pinMode(setButton, INPUT);
}
void loop() {
buttonState1 = digitalRead(inputButton);
buttonState2 = digitalRead(setButton);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH && pushCount == 0) {
analogWrite(led1, 125);
analogWrite(led2, 125);
analogWrite(led3, 125);
analogWrite(led4, 125);
Serial.println(pushCount);
pushCount = pushCount + 1;
delay(200);
}
else if (lastButtonState1 == HIGH && pushCount >= 1 && pushCount <= 4) {
if (buttonState2 == HIGH && lastButtonState2 == LOW && ledState == false) {
analogWrite(currentLed[pushCount], 255);
inputLeds[pushCount] = 1;
Serial.println("test 2");
ledState = true;
}
else if (buttonState2 == HIGH && lastButtonState2 == LOW && ledState == true) {
analogWrite(currentLed[pushCount], 1);
inputLeds[pushCount] = 0;
ledState = false;
}
Serial.println("test");
Serial.println(pushCount);
pushCount = pushCount + 1;
lastButtonState1 = buttonState1;
}
if (buttonState1 == HIGH && pushCount >= 5) {
Serial.println(" currentLed > 4;");
pushCount = 0;
delay(200);
return; 55
}
circuit(); //zorgt dat void circuit gerunt word.
if (ruben == 1) { //Als output ruben 1 is, wordt ledRuben aangezet.
analogWrite(ledRuben, 255);
}
else { //als output ruben 0 is zet ledRuben uit
analogWrite(ledRuben, 1);
}
}
lastButtonState1 = buttonState1;
}
void circuit() { //logic gates
t16 = !i16; //Dit is de NOT gate, maakt van output 1 een 0, en andersom
t09 = t16 && i09; //Dit is de AND gate, als alle inputs 1 zijn, is de output 1
t61 = i61 || i34; //Dit is de OR gate, alleen als alle inputs 0 zijn, is de output 0
ruben = !(t09 || t61); //Dit is de NOR gate, alleen als alle inputs 0 zijn, is de output 1
return ruben;
5
}
led1is the same ascurrentLed[0]Apart from the type of the array is not strictly correct. – Code Gorilla Jan 26 '17 at 15:02lastButtonState2never changes in your code. – Gerben Jan 26 '17 at 15:06if button1block, and directly into the loop function? – Gerben Jan 26 '17 at 15:14lastButtonState2still never changes in your code! – Gerben Jan 26 '17 at 15:16lastButtonState2 = buttonState2; } – Advanzd Jan 26 '17 at 15:45
pinMode(..., INPUT_PULLUP). Second, you probably have to debounce your buttons (by software or hardware), Google is your friend to find out how. – jfpoilpret Jan 28 '17 at 09:21