Go Down

Topic: Were am I wrong? oldie newbie!! (Read 679 times) previous topic - next topic

intertronic

Hi all,

I have gone through the sketch  again. This is the all the sketch. Cannot find out whats wrong.  When I press the middle button, nothing is happening.

All I want to do is simple. When I press Button UP. Led UP comes on for 10 seconds (If I press any other button this is ignored, until the 10 seconds pass), When I press Button Down, LED Down comes on for 10 seconds, and again, if i press any other button, this is ignored until the 10 second timeout)

So far everything works good.

Than i have the third button, which when I press it, should look at the state of the A0 and A1 inputs, which have been programmed as a digital switch. (This has been done by connecting a wire from A0 and A1 to negative, and than using the command below)  It should follows the If and and / else / If and and command. 

I doubt this is good!..

However nothing is happening, with the middle button.

any ideas? Could it be that the limit switches A0 and A1, are not working and being ignored?

Thanks.

Code: [Select]



// set pin numbers:
const int buttonUp = 2;       // the number of the pushbutton pin for UP
const int buttonMiddle = 3;   // the number of pushbotton pin for middle
const int buttonDown = 4;     // the number of the pushbutton pin for Down
const int ledPinUp = 12;      // the led output for UP Position
const int ledPinMiddle = 13;  // the led output for MIDDLE Position
const int ledPinDown = 11;    // the led output for DOWN Position
const int limitup = A0;
const int limitdown = A1;


// inputs that will change:
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;// variables for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  pinMode(ledPinUp, OUTPUT);        //output for led UP
  pinMode(ledPinDown, OUTPUT);      //output for led DOWN
  pinMode(buttonUp, INPUT);         //trigger for led UP
  pinMode(buttonDown, INPUT);        //trigger for led Down
pinMode(limitup,INPUT_PULLUP);        //signal switch for buttonmiddle
pinMode(limitdown, INPUT_PULLUP);      //signal limit switch for buttonmiddle
}

void loop() {
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonUp);
  buttonState3 = digitalRead(buttonMiddle);
  buttonState4 = digitalRead(buttonDown);
  

  if (buttonState2 == HIGH) {
    digitalWrite(ledPinUp, HIGH); 
    delay (10000);
    
  }
  else {
    digitalWrite(ledPinUp, LOW);
    }
    
  
  if ((buttonState3 == HIGH) && (limitup == HIGH) && (limitdown == LOW))

  {
  digitalWrite(ledPinUp, HIGH);
  delay (10000);
  }
  else
  if ((buttonState3 == HIGH) && (limitup == LOW) && (limitdown == HIGH)){
    digitalWrite(ledPinDown, HIGH);
    }
    
  if (buttonState4 == HIGH) {
      digitalWrite(ledPinDown, HIGH);
      delay (10000);
      }
      else {
     digitalWrite(ledPinDown, LOW);   
        }
        }
        
      
    


Grumpy_Mike

#1
Mar 09, 2015, 11:46 pm Last Edit: Mar 10, 2015, 10:13 am by Grumpy_Mike
Don't use else, it overrides everything if that specific condition is not met. Test instead for specifically what you want.

Also you read the state if the buttons and then have a 10 econd delay so that by the time you come to the decision on the second button you are Useing the state of the buttons as it was ten seconds ago.

You should not be using delay at all if you want it to be responsive. Look at the blink without delay example in the IDE

Go Up