Pages: [1]   Go Down
Author Topic: second input isn't read by arduino  (Read 1468 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello there,
the problem i'm facing is that i can't get the arduino to read the 2nd input (pushbutton) even though i'm sure it's connected properly because i tried to use it as the 1st one instead and it worked..

so i have 2 pushbuttons and 4 LED's and i want the 1st pushbutton to make LED1 light and LED2 blink and then when i press the 2nd pushbutton i want LED 2 stop blinking and just be lit and LED3 to start blinking..  but i can't get the 2nd pushbutton 2 work... it doesn't read it at all.. so can someone please see my code and tell me what's wrong.. or why it doesnt read it?

Code:
Code:
#define LED 13 // THE PIN FOR THE LED
#define SENS1 0


//VARIABLES

int pin1 = 1;
int pin2 = 2;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 =11;

int sensorValue = 0;
int oldSensorValue=0;

boolean blinking = false;


void setup () {
 pinMode (8, OUTPUT);
 pinMode (9, OUTPUT);
 pinMode (10, OUTPUT);
 pinMode (11, OUTPUT);
  
 pinMode (1, INPUT);
 pinMode (2, INPUT);
}

void loop (){
  
 oldSensorValue = sensorValue;
 sensorValue = digitalRead(pin1);

 if(sensorValue == HIGH && oldSensorValue == LOW){
  
      digitalWrite(pin8, HIGH);
 if(sensorValue == HIGH && blinking == true){
      blinking = false;
    }
    else if(sensorValue == HIGH && blinking == false){
      blinking = true;
    }

    }
if (blinking == true) {
    digitalWrite(pin9, HIGH);
    delay(200);
    digitalWrite(pin9, LOW);
    delay(200);}
    
  sensorValue = digitalRead(pin2);
  
    
 if (blinking == false && sensorValue == HIGH){
   digitalWrite(pin9, HIGH);
   while (pin9, HIGH) {
   digitalWrite(pin10, HIGH);
   delay(200);
   digitalWrite(pin10, LOW);
   delay(200); }
 }
else if (blinking == false && sensorValue == HIGH){
   digitalWrite(pin9, HIGH);
   digitalWrite(pin10, HIGH);
   while (pin10, HIGH) {
   digitalWrite(pin11, HIGH);
   delay(200);
   digitalWrite(pin11, LOW);
   delay(200); }
 }
}


thank you =)  
Back to top  
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your last if/else if statement has identical conditions in the if and else if clauses.  For this reason the else if block of code will never be executed.

You also have a couple of "while (pinX, HIGH)" lines of code as well.  What is the intent of these while statements?  The comma operator really has little use within a while clause (and none at all within the code you're using it in that I can see).
Logged


Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Haven't you already started a thread?  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1295213633/0#0

Your variables don't make any sense.  So why don't you fix that first.  There is absolutely no point in naming a variable "pin1".  Just use the number "1".  That being the case, you should never use Pin 0 or Pin 1 in your sketch.  These are connected to the serial lines on the Arduino.  Unless you know exactly what you are doing, this will cause you problems.

Name your variables something intelligent like "pushbutton1" or "left_pushbutton"  or "blinking_pushbutton".  Something that makes sense to you and others reading the code.

Don't repeat the use of "sensorValue".  Instead use two varibles "button1State" and "button2State".  Read both buttons at the beginning of loop() AND THEN do all of your if-checking.  (And you probably want to add a small delay for de-bouncing unless your switches are hardware de-bounced with a capacitor.)

Right now your code is so complicated, it is difficult to work through it.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i know the if statements at the end are identical, but it doesn;t even execute the part before it, i can't get it to read the second input.. but otherwise everything is working fine..  

something i just realized now.. is that when i upload the sketch, if i press on the second pushbutton straight away (the first ordder given to the arduino) it runs the second part the way i want it... and if i press the first pushbutton( straight after its uploaded) it runs the first part... but i can't seem to combine them both.. and i know my code is messed up, i'm just a beginner and this is far as i've gone.. i'm just really stuck and helpless at the moment... i;ve reached a dead end and don;t know what to do. So any suggestions as how to start fixing it.. or how to get both pushbuttons to work?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As I said, you are incorrectly re-using the sensorValue variable.

You have a check at the very beginning to copy the old one, which is pointless.  Since two different buttons can change it.

1.  Change all of your variable names to something meaningful.
2.  Stop using sensorValue, give each button their own state variable, and read them both at the beginning of loop().
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

2.  Stop using sensorValue, give each button their own state variable, and read them both at the beginning of loop().

what does their own state variable mean? can you please give me an example.. the reason i used it was so i could be able to tell the arduino to read a different input from the sensor each time, or then how else could i do that?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't bother trying to "tell the arduino to read a difference sensor value" each time.  Also, are you using a push-sensor?  Or a push-button?  If you are using a "push button", stop using the word "sensor."

Instead, read both buttons each time through the loop.  

The problem with determining which was read last is the loop will execute hundreds or thousands or tens-of-thousands of times before the next time you push a button.  Why does it matter which button was pushed last?

You're already tracking if you are doing an action based on the last button.  Or at least, I think you are based on the really really complicated IF statements you have.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.. il try that and get back to you.. thanx for your help =)
Logged

Pages: [1]   Go Up
Jump to: