So I was working with a student on reading an analog input (linear pot). The end goal was to read the 10-bit ADC value, then interpret this value as to how long (in arbitrary units) to turn on and off an LED.
Here is the code:
This code will work just as expected. However, if you change
to
The ADC no longer reads properly. Both the LCD and the flashing LED hint that the ADC value randomly jumps from say 512, down to 234, then back to 502 etc.
In addition, when things got shakey, the ADC value will not make it all the way to 1023 even at full voltage.
My first thought was data type issues. So we typecasted everything, removed the LCD function, and finally reverted back to the *10 multiplier, and repeated the for loop 10 times (emulating the 100x multiplier).
Nothing worked. Even hardcoding the time_switch = 102300 made the thing go crazy.
Any ideas on what to try next? It's almost like the for loop cannot handle this many iterations. But even if it can't, why would it mess up my ADC values?
Interested in any thoughts.
Thanks
Here is the code:
Rich (BB code):
void main(void)
{
init(); // Initialization code to setup ports, oscillator, etc.
while(1)
{
ADCvalue=ADCpoll(13); // Get ADC Value
time_Switch=(long)ADCvalue*10;// Multiply the ADC value by scaler
lcdwrite(time_Switch,0);//Displays time_switch on LCD
RC1=1; //Turn off LED
for(time_ON=0; time_ON<=time_Switch; time_ON++) //Wait time_switch
{
}
RC1=0; // Turn On LED
for(time_OFF=0; time_OFF<=time_Switch; time_OFF++) //Wait time_switch
{
}
}
}
Rich (BB code):
time_Switch=(long)ADCvalue*10;
Rich (BB code):
time_Switch=(long)ADCvalue*100;
In addition, when things got shakey, the ADC value will not make it all the way to 1023 even at full voltage.
My first thought was data type issues. So we typecasted everything, removed the LCD function, and finally reverted back to the *10 multiplier, and repeated the for loop 10 times (emulating the 100x multiplier).
Nothing worked. Even hardcoding the time_switch = 102300 made the thing go crazy.
Any ideas on what to try next? It's almost like the for loop cannot handle this many iterations. But even if it can't, why would it mess up my ADC values?
Interested in any thoughts.
Thanks