C if loop, using float values
Rsadeika
Posts: 3,837
I am trying to get this segment of code too work correctly, but having no success. This is using the simpletools adc function, and v0 is a float value. I am trying to turn on an LED when the battery gets to 6.0V or less. I have changed the check value too 6.0, by that is not working, not sure how to code this.
Thanks
Ray
Thanks
Ray
float v0; v0 = adc_volts(0); v0 = (v0*2); if(v0 <= 6) high(7);
Comments
Now I am wondering if the value should be 6.000000? So, if v0 is something like 6.433210, then the comparison value should be the same amount of digits, at 6.000000?
Ray
Ray
And, honestly, as you're using constant values for thresholds, why not divide them in half?
The voltage divider uses two 10K resistors, in this setup the value that v0 read out is 3, assuming the battery voltage is 6, half of the expected value. So, for convenience, I multiply it by 2 to get the real value.
The reason for using float values, I want to setup a metering method using three LEDs, red, green, and yellow. If the battery voltage is >6.5V, the green LED is on. If the battery voltage is between 6.1 and 6.4, then the yellow LED is on. Less than 6.1V the red LED is on. And of course this will run in its own COG, because I have an interactive user program with the XBee working in the main() function.
I will have to debug my original program to see why the ADC is not working as expected. Something is affecting the ADC function, but not sure what it is a the moment.
Ray
Glad you were able to get this working in a smaller program to verify the hardware and basic software functionality. Feel free to post the larger program now if you're unable to find the difference and we might be able to help you find the irregularity.
Jim
The adc_volts function is part of the Simple libraries, and it outputs a float. The documentation for the function does not describe the precision, but here it is: http://david.zemon.name/PropWare/api-develop/adcDCpropab_8h.xhtml#a3e2df0dd371eb7524ed35115e9bf1770
In short:
Don't know if you verified, this is the adc_volts function from the library source I have:
Looks like it assumes a 5 volt range and 12 bit precision, is that your case ?
'. As shown below, the 'adc_init(21, 20, 19, 18);' has to be placed just about first in line, for it to work correctly. Luckily I did not have a whole bunch more code in there, I probably would have spent days trying too figure that one out, but I am still not completely confident that the issue has been resolved.
I also have to be very careful about these functions in terms of COG use, I am not sure if the ADC function uses a COG to run.
Ray
Original code lines: Moved code lines, now works as expected.
I put all the ADC activity into one COG, if I need access to any of the ADC data, I use the global variables vx and vy. It seems that the ADC function works best when it is running and being accessed, in one place. When I tried to use the ADC, for instance, within the main() function, the data gets corrupted.
Looking at the way pwrmon() COG is developing, I wonder if the program should be done in C++, although I am not sure about Class structures. Do these things run like a background thread, which would save me COGs?
Because of the limited RAM of the Propeller, I am now going to try adding a Raspberry Pi to the mix. This way the Propeller will just deal with the battery, DC motors, IR, XBee and other sensors, while the RPi can deal with data acquisition/storge and manipulation.
First I have to see how to deal with the two units, will they cooperate the way I am thinking they should.
Ray