View Full Version : Arrays and For Loops in Python
dp7896
04-23-2008, 09:24 AM
Hello,
I am trying to create a look up table in Python. I want to use a for loop to use a look up table, for instance:
Temp_F[] = { 980, 970, 960, 950, 940, 930, 920, 900 }
for (i = 0; i <= 9; i++)
{
if ( curReading > Temp_F[i])
{
break;
}
}
Thanks for any help.
Dave
mgenti
04-23-2008, 10:12 AM
Dave,
Currently for loops and arrays are not supported in SNAPpy. We do however support the while loop, like:
x = 0
while x < 10:
print x
x = x + 1
For a quick glace at what's supported in SNAPpy checkout the "SNAPpy Cheat Sheet" in the SNAP Reference Manual.
Can you give us some more details of what you are trying to use the array and loop for and we might be able to come up with something else that might work?
mgenti
04-23-2008, 10:30 AM
After re-reading your post it sounds like maybe you are trying to do this in Portal. If this is the case than yes we support for loops and arrays. If you were to take your code as it is and convert it to python syntax it might look like:
Temp_F = [980, 970, 960, 950, 940, 930, 920, 900]
for i in range(len(Temp_F)):
if curReading > Temp_F[i]:
print "High reading"
break
dp7896
04-23-2008, 10:38 AM
I am trying to produce a look up table for a thermistor to display the temp on the 7-seg display.
A while loop would work fine, but the array would be needed.The for loop increments i until it matches the element in the array. So if the readAdc(0) was equal to 512 it would be 72 degrees because 512 is the 72nd number in the array. I am trying to use this in SNAPpy. These devices will not be connected to a PC. I will have to come up with a different way to determine the temp. Thanks for your help.
Dave
David Ewing
04-23-2008, 12:04 PM
One way to do lookup tables is with strings in SNAPpy. The values are byte-sized, and the string length must be < 255, but for many applications this is an efficient way to handle it. In this case, if your temperatures can be constrained to, say, Fahrenheit values of 0-255, we can make a 128 byte array of temperatures for lookup purposes. Then we just have to divide the 10-bit ADC reading by 8 and we've got a fast lookup without loops.
adcValue = readAdc(0)
temp = ord(tempArray[adcValue / 8])
display2digits(temp)
The attached script, "temperatureInternal.py" demonstrates this. Run on a demonstration board (with 7-seg display) and you'll see the temperature in Fahrenheit.
Also attached are a couple of utility scripts (run on the PC) to help generate lookup tables for NTC thermistors.
- David
dp7896
04-23-2008, 12:19 PM
Thank you very much. That just seems too simple. You guys are the best, and really quick.
Dave
vBulletin® v3.7.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.