0

I'm currently working with an Raspberry Pi for a exercise.

I'm working with the following on the breadboard:

  • 5 LED's
  • 2 Switches

My following code is supposed to work as follows:

With the first button you can select the next LED and with the second button you can put the selected LED on/off. When you're at the last LED it gives a true or a false (on or off) with the following output:

Circuits I don't see what's wrong with my code:

//const variables  
const int leds[] = {3, 5, 6, 9, 11};  
const int buttons[] = {12, 13};

//variables that will change  
int buttonState[] = {false, false};  
int lastButtonState[] = {false, false};  
int values[] = {false, false, false, false};  

void setup() {  
    //init LEDs  
    for(int i = 0; i < sizeof(leds); i++){  
        pinMode(leds[i], OUTPUT);  
    }

    //init buttons  
    for(int i = 0; i < sizeof(buttons); i++){  
        pinMode(buttons[i], INPUT);  
    }
}

void loop() {  
    //fade when game starts  
    fade();

    //start game
    start();

    //output of game
    output();
}

void output(){
    bool t1 = !values[0];
    bool t2 = t1 && values[1];
    bool t3 = values[2] || values[3];
    bool Q = !(t2 || t3);
    if(!Q){
        digitalWrite(leds[4], true);
    }else{
        digitalWrite(leds[4], false);
    }
}

void start(){
    //total of leds
    int j = 0;
    //check if user is not at 5th led
    while(j < 4){
        //loop through buttons
        for(int i = 0; i < 2; i++){
            // Read button
            buttonState[i] = digitalRead(buttons[i]);
            // check button state
            if (buttonState[i] != lastButtonState[i]) {
                // if the state has changed
                if (buttonState[i] == HIGH) {
                    //check if button 1
                    if(i == 0){
                        //select next LED
                        j++;
                    }
                    //else button 2
                    else{
                        // if the current state of the 2nd button is HIGH
                        while(i == 1){
                            //if current value of led is false, put it true
                            if(values[j] == false){
                              //put led on
                                digitalWrite(leds[j], true);
                                values[j] = true;
                                delay(50);
                            }else{
                                //put led off
                                digitalWrite(leds[j], false);
                                delay(50);
                                values[j] = false;
                            }
                            //go back to button 1?
                            i = 0;
                        }
                    }
                    //go back to button 1?
                    i = 0;
                }
            }
            // save the current state as the last state,
            // for next time through the loop
            lastButtonState[i] = buttonState[i];

            // wait a little
            delay(50);
        }
    }
}

void fade(){
    //put every led on half-on
    for(int i = 0; i < sizeof(leds); i++){
        analogWrite(leds[i], 100);
    }
}
DiceOfDoom
  • 25
  • 4
  • 2
    Surely this doesn't build without warnings? Why don't you mention any problems? The `pinMode(leds, OUTPUT);` statement must be wrong, it should be `pinMode(leds[i], OUTPUT);`, that was the point of the loop after all (and the same with the inputs). – unwind Jan 23 '17 at 12:03
  • When I verified it, it ran without giving me errors, but I updated it now. So that's not the problem anymore. So currently the second button, which is supposed to put LED on/off puts the LEDs only off and not on. – DiceOfDoom Jan 23 '17 at 12:24
  • As long as your question is purely about software it is on-topic here. But just for the record, questions that could be related to either software or hardware should be asked at http://electronics.stackexchange.com/ (where they will want some schematics in addition to the code). For questions about Ardunio specifically, ask at http://arduino.stackexchange.com/. There is also http://raspberrypi.stackexchange.com/. – Lundin Jan 23 '17 at 12:25
  • What is "digitalRead"? Does it contain the de-bouncing code? Where is the de-bouncing done? – Lundin Jan 23 '17 at 12:31
  • "digitalRead" is a build-in function which reads the status of the button. And in my case the whole start() function is the de-bouncing. – DiceOfDoom Jan 23 '17 at 12:37
  • 1
    "I'm currently working with an Raspberry Pi for a exercise.", but your code, title and tags say Arduino. These are completely different architectures. And Arduino is not C! – too honest for this site Jan 23 '17 at 13:05

1 Answers1

1

In C and C++, if you want to compare two values to check if they are equal, you have to use == (the comparison operator) instead of = (the assignment operator). You accidentily use the wrong operator here:

while(i = 1){

And here:

if(values[j] = false){

Change those to ==.

G. Sliepen
  • 7,637
  • 1
  • 15
  • 31