DDGao:
Hi PaulS,
So I modified the code to see if it would stop in 30 seconds and it won't stop. Would you please give me some insight on why it is not stopping?
And the reason why I put 1023 is that I copied the voltage reading example (http://arduino.cc/en/Tutorial/ReadAnalogVoltage) where it used 5.00/1023.0. I thought the purpose of division by 1023 is to calibrate.
unsigned long time;
void setup(){Serial.begin(9600); }
void loop(){
time = millis();
if(millis()-time<30UL*1000UL){
int sensorValue = analogRead(A0); //read the analog input on pin 0
float voltage = sensorValue * (5.00/1023.00);
Serial.print(voltage,DEC);
Serial.println(",");
delay(1000);
}
}
**Thank you!**
In addition to what UKHeliBob said while I was writing this long explanation:
Well, the problem seems to be a misunderstanding of the loop() function. The way the Arduino IDE compiles it will not to halt execution at the end of the loop() function, but continue execution back at the beginning of loop() once the end of loop() is reached. (Thus the name "loop"...
) If you moved the above contents of loop() into setup() and leave loop() empty your code should end. Another way to force operation to appear to end at a certain point is to insert an infinite loop doing nothing (like while (1) {}
) where you want to sketch to stop (or appears to stop, in reality it is very busy doing nothing over and over again). Using the infinite loop doing nothing can be a handy technique for troubleshooting a program to make sure it doesn't execute past a certain point.
The purpose of the 1023 when dealing with values returned from analogRead() is because over an input of 0VDC to 5VDC, analogRead() will return positive integer values 0 through 1023. The full equation of float voltage = sensorValue * (5.00/1023.00);
will proportionally convert the range of 0 through 1023 ADC (Analog to Digital Converter) counts to 0 through 5 Volts DC. I was taught in high school a method of converting between different units where one uses cross multiplication. Knowing that 5 VDC = 1023 ADC counts Thus:
sensorValue ADC counts 5 VDC
* -----------------
1023 ADC counts
Cross multiplication on "ADC counts" will cancel those units out, leaving only the numbers for the equation and the unit "VDC". Adding the ".00" (well you can actually get away with just ".0" and save yourself keystrokes) forces the compiler to handle the 5 and the 1023 as floating point values. (Float is the only datatype we use that can handle decimals, all the other datatypes that were explained in the SparkFun tutorial you read are just different sizes of integer ranges. This is one of the major differences between float and long.) The massive increases in processing time that float costs you (as demonstrated by the SparkFun tutorial) is why I was suggesting having your Arduino sketch simply output the ADC values, and then leave the conversion to VDC to the computer where you are interpreting the data (and as I remember from the beginning of this thread, making graphs).
It's late right now, so if no one else gets to it, I'll write up some pseudocode for totally getting rid of the need for that delay(1000) that you keep using tomorrow evening. Unless, of course, either you come up with something or someone else shows you the way down that path.