2

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
}
Advanzd
  • 21
  • 2
  • You might want to look at putting you LED pin numbers into an array, or rather using the one you already have. led1 is the same as currentLed[0] Apart from the type of the array is not strictly correct. – Code Gorilla Jan 26 '17 at 15:02
  • Thanks for the tip, i gues you're right because now i'm just using more variables then needed. But do you have any idea how to fix the button issue? – Advanzd Jan 26 '17 at 15:06
  • lastButtonState2 never changes in your code. – Gerben Jan 26 '17 at 15:06
  • @gerben i added lastButtonState2 = buttonState2; to both the if and else if that should toggle the button, it didn't change anything tho.. – Advanzd Jan 26 '17 at 15:11
  • Can't you just move the button2 code out of the if button1 block, and directly into the loop function? – Gerben Jan 26 '17 at 15:14
  • lastButtonState2 still never changes in your code! – Gerben Jan 26 '17 at 15:16
  • @gerben thanks to you i've fixed it! i put if (buttonState1 != lastButtonState1) { around the button2 if/else, ending with
    lastButtonState2 = buttonState2; }
    – Advanzd Jan 26 '17 at 15:45
  • 1
    Do you use a pullup or pulldown resistor for your buttons? If not, then add one or use 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

0 Answers0