Go Down

### Topic: If Else statement when identifying voltage? HELP! Science Fair Project (Read 1 time)previous topic - next topic

#### ShariL

##### Nov 14, 2012, 11:25 pm
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(){

if( val = (warningThreshold >= .001)){

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

#### KeithRB

#1
##### Nov 14, 2012, 11:33 pm
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?

#### ShariL

#2
##### Nov 14, 2012, 11:39 pm
He wants the card to identify when the phone is plugged in. What does he need to use?

#### DVDdoug

#3
##### Nov 14, 2012, 11:40 pmLast Edit: Nov 14, 2012, 11:44 pm by DVDdoug Reason: 1
Code: [Select]
`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?

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.

#### ShariL

#4
##### Nov 14, 2012, 11:45 pm
He's 9. He doesn't have a potentiometer. The phone cord is 5VDC, does that help?

#### KeithRB

#5
##### Nov 14, 2012, 11:55 pm
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.

#### ShariL

#6
##### Nov 15, 2012, 12:09 am
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.

#7

#### ShariL

#8
##### Nov 15, 2012, 05:36 pm
Jst wanted to say Thanks! He figured out the problem and his Science Fair project is awesome!

#### KeithRB

#9
##### Nov 15, 2012, 05:42 pm
Great! What was the problem?

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

Go Up

Please enter a valid email to subscribe