Go Down

Topic: [HELP]Switch Case syntax and edge detection with two push buttons for a van (Read 209 times) previous topic - next topic

ArizonaJim

First post, so
Hello everyone! the idea is that a friends van is getting lights installed in the back, so i thought, let's make it controllable through one switch at each point of entry into the back of the vehicle. I won;t go too far into the schematics (just yet) but i will say i am using two pushbutton switches on 5v logic with 10k resistors and a darlington array (uln2003a) to switch the 6 12v led lights. the lights are on a 12v circut, wired as per the diagram, and the darlington is on 3.3v logic and everything shares a common ground. the lights are all lit and button pushes do nothing. i loaded a button push example and ran through all of the pins with both buttons and that all works, so i know its something in the code, and before i post it below all i ask is that you all don't laugh too hard. thanks in advance and let me know if you have any questions:
sorry, first post here's the code so you don't have to download anything:
 
Code: [Select]


#include "Arduino.h"
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;
const int led6 = 7;

const int backButton = 10;
const int slideButton = 9;

int backButtonState = 0;
int backButtonCounter = 0;
int lastBackButtonState = 0;

int slideButtonState = 0;
int slideButtonCounter = 0;
int lastSlideButtonState = 0;

void setup () {
    pinMode(backButton, INPUT);
   
    pinMode(slideButton, INPUT);
   
   
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    pinMode(led5, OUTPUT);
    pinMode(led6, OUTPUT);
    backButtonState = digitalRead(backButton);
    slideButtonState = digitalRead(slideButton);
   
}
    void loop () {
     
    backButtonState = digitalRead(backButton);
    if (backButtonState != lastBackButtonState) {
      if(backButtonState == HIGH) {
        backButtonCounter+1;
 //       if (backButtonCounter == 3) backButtonCounter = 0;
      } else{}
      delay(20);
    }
    lastBackButtonState = backButtonState;
   
    slideButtonState = digitalRead(slideButton);
    if (slideButtonState != lastSlideButtonState){
      if(slideButtonState == HIGH){
        slideButtonCounter+1;
 //       if(slideButtonCounter == 3) slideButtonCounter = 0;
      } else{}
      delay(20);
    }
     
   
    lastSlideButtonState = slideButtonState;
   
 //  int backButtonCase =
 //  int slideButtonCase =


   switch (backButtonState){
        case 0:
        case 1:// back button pressed once
        digitalWrite(led1, HIGH);
        digitalWrite(led2, HIGH);
        break;
//        case 2:// back button pressed once and held
//        break;
        case 2:// back button pressed twice
        digitalWrite(led1, HIGH);
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        break;
//        case 4:// back button pressed twice and held
//        break;
        case 3:// back button pressed three times
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        break;
        default: // lights off
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
       
   }
   delay(1);
   
        switch (slideButtonState ) {
        case 0:
        case 1:// slide button pressed once
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        break;
//        case 2:// slide button pressed once and held
//        break;
        case 2:// slide button pressed twice
        digitalWrite(led1, HIGH);
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
        digitalWrite(led6, HIGH);
        break;
//        case 4:// slide button pressed twice and held
//        break;
        case 3: // slide button pressed three times
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        break;
        default: // lights off
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
        digitalWrite(led6, LOW);
        }
        delay(1);
       
    }

slipstick

Do you think it might be helpful to say what the button pushes are supposed to do?

Steve

ArizonaJim

sorry, there are two buttons, the one at the back and the one on the sliding door. when the back door button is pushed once, the back door lights turn on, a second time, all the lights turn on, and a third time, all lights turn off. for the sliding door, when pushed once, the sliding door lights turn on, a second time, they all turn on, and a third time, they all turn off. sorry, should have been clearer.

dougp

if(backButtonState == HIGH)

implies you are switching the input pin to Vcc.  Is there a pulldown resistor to insure the pin is at ground potential when the switch is inactive?
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.


sterretje

How about adding serial print statements in strategic places so you can follow what the code is doing,?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

ArizonaJim

I did right after posting the first post and it was running through all the cases over and over. tweaking the case numbers resulted in the code not printing to the sections altered. i thought it could be the pushbuttons in need of debouncing, so i've been looking into that. i wrote a program that worked in lighting up the leds when the button was pressed with out a case switch and everything worked. this is why i think its wither a bracketing issue or a problem with my case syntax.

UKHeliBob

Code: [Select]
  switch (backButtonState)Did you mean to switch using the state or the counter ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

The art of getting good answers lies in asking good questions.

UKHeliBob

Why are you counting switch presses?
Actually he is trying to but failing
Code: [Select]
      backButtonCounter + 1;
Code: [Select]
      slideButtonCounter + 1;
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

The art of getting good answers lies in asking good questions.

Go Up