If Else statement when identifying voltage? HELP! Science Fair Project

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);
}
}

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?

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

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 [u]Analog Serial Read Example[/u] to see what kind of analog readings you are getting (with your actual analog input instead of the pot).

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.

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

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.

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.

The examples are your friends:

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

Great! What was the problem?

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