Pages: [1]   Go Down
Author Topic: If Else statement when identifying voltage? HELP! Science Fair Project  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is the code my 9 year old son is using for his Science Fair project. It is supposed to turn off the siren when the phone is plugged in. It should do this by identify
ing when power is being pulled by the phone. It's not working! It was working and has stopped. His project is due tomorrow and now this awesome project is going to fail. Please help!

long warningThreshold = .001;

const int alarmPin = 9;  //siren
const int plugPin = A0;  //phone

void setup(){
 
  pinMode(alarmPin, OUTPUT);
  pinMode(plugPin, INPUT);
}
void loop(){
 
  int val =analogRead(plugPin);
  if( val = (warningThreshold >= .001)){

      digitalWrite(alarmPin, LOW);
  }
else
  {
    digitalWrite(alarmPin, HIGH);
  }
}
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One problem is that .001 is not an int (which stands for integer). If you need them use floats, otherwise set it to zero.

You are also setting value to the logical value of (warningThreshold >= .001), do you want == here?
Logged

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

He wants the card to identify when the phone is plugged in. What does he need to use?
Logged

Offline Offline
Edison Member
*
Karma: 36
Posts: leet
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int val =analogRead(plugPin);
  if( val = (warningThreshold >= .001)){

analogRead returns integers (whole numbers) between 0 and 1023.   If you get anything other than zero, your if-statement will be true.   It's not unusual to have noise or bias on the analog inputs, so you may never read that low, or the value could jump-around between zero and something else...

What kind of voltage do you have (or what do you expect) with the phone plugged-in or unplugged?

Try using the Analog Serial Read Example to see what kind of analog readings you are getting (with your actual analog input instead of the pot).


Quote
He wants the card to identify when the phone is plugged in. What does he need to use?
We don't know!  We don't know what the phone is plugged-into, or how the phone is connected to the Arduino, etc.

« Last Edit: November 14, 2012, 05:44:12 pm by DVDdoug » Logged

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

He's 9. He doesn't have a potentiometer. The phone cord is 5VDC, does that help?
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a Serial.write(val) command to write the AnalogRead value onto the serial monitor. Plug and unplug the phone to see how val changes. That will tell you your proper threshold value. (and the the if command should simply be if (val >- threshold).

If it was working before and not now, you might have damaged something by plugging and un-plugging the phone with the power on.
Logged

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

He used the charger cord for his phone. He cut the plug off it and wired it into a breadboard. He wired a siren into the breadboard as well. Many headaches later, the siren has been temporarily replaced by an LED. The light is on(siren)but it isn't turning off when the phone is plugged in.

I should preface this by saying tht up until this project, he has never used a board before or programmed anything. He is very smart and willingly perserveres but this one....UGH! My point is that we don't know how to print to the serial port. We don't know how to do much of anything in regard to this. The guy at the store sold him the Uno kit and a book called the Arduino cookbook and sent him on his way.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The examples are your friends:
http://arduino.cc/en/Tutorial/AnalogReadSerial
Logged

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

Jst wanted to say Thanks! He figured out the problem and his Science Fair project is awesome!
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great! What was the problem?

Sounds like you have a smart kid, and he has a great supportive Mom!
Logged

Pages: [1]   Go Up
Jump to: