Pages: [1]   Go Down
Author Topic: switch problems  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im try to make an led to turn on when the switch is pressed for 1 time and then turn off and when thbe switch is pressed again another led is switched offf can someone tell me what is wrong in my above code to perform this.
Code:
const int buttonPin = 2;
const int led = 7;
const int led1 = 8;

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);
  pinMode(led, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
 
  digitalWrite(led, LOW);
   digitalWrite(led1, LOW);
  buttonState = digitalRead(buttonPin);
   int buttonState1 = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  
  if (buttonState == HIGH) {    
    // turn LED on:    
  
    digitalWrite(led, HIGH);  
    delay(5000);

  }
  buttonState == LOW;
if(buttonState1 == HIGH){
   digitalWrite(led1, HIGH);  
    delay(5000);
}
  
}
 

Moderator edit: CODE TAGS
« Last Edit: March 17, 2012, 12:09:16 pm by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
buttonState == LOW;
Did you mean "="?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i arrange that however always the same led is going on is there a piece of code that disable the previous action
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to make led1 an output.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int led = 7;
const int led1 = 8;
Do you count things like "1, 2, 3...", or do you count things like "Yes, I have a thing, 2, 3..."?

Code:
  pinMode(led, OUTPUT);
  pinMode(led, OUTPUT);
Maybe you need three or four calls to pinMode that do exactly the same thing.

Code:
  buttonState = digitalRead(buttonPin);
   int buttonState1 = digitalRead(buttonPin);
Unless you are faster than lightening, these two variables will contain the same value.

Code:
  buttonState == LOW;
This is performing a useless test.

Quote
im try to make an led to turn on when the switch is pressed for 1 time and then turn off and when thbe switch is pressed again another led is switched offf can someone tell me what is wrong in my above code to perform this.
Aside from those things, you have several basic problems. For one, you only want to do something when the switch state changes, to pressed. You are not testing for a state change.

Second, you are not counting presses, so you have no idea how many times, on any given pass through loop(), the switch has been pressed.

Finally, hasn't anyone ever told you to "Turn the damned lights off when you are done!"?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how can i make the switch output as it was in the beginning as it is pressed one time
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how can i make the switch output as it was in the beginning as it is pressed one time
You can't affect the output of a function. You can modify the value in a variable, but you really should treat the output from digitalRead() as though it is read only.
Logged

Pages: [1]   Go Up
Jump to: