numbers aren't adding up correctly with += or + operators. EX: "a = a + b"


so purchased new photoresistors radioshack. http://www.radioshack.com/product/index.jsp?productid=2062590
i have 1 of own. assume same kind of cds photoresistor, looks same.
i wanted make sure read light @ same levels, made program take care of that. (some of 5-pack photoresistors different, smaller or larger light-sensitive areas, or other minor differences.)

i use arduino uno rev3 board.

one end of photoresistor has wire leading analog input , 10k ohm resistor leading wire leads power source on protoboard. (basically voltage divider) other end of photoresistor leads ground. 6 of photoresistors each independently wired in way.  of course have wire going arduno's 5v pin protoboard , wire leading ground pin protoboard  power , ground pins on protoboard.

i have arduino plugged wall-wart in case power usb isn't enough power arduino. (this issue have experienced previous computers in past when computers didn't use enough power give arduino enough power via usb work.)

here code:

void setup()
{
 serial.begin(9600);
}
void loop()
{
 int a0 = 0;
 int a1 = 0;
 int a2 = 0;
 int a3 = 0;
 int a4 = 0;
 int a5 = 0;
 
 int a0total = 0;
 int a1total = 0;
 int a2total = 0;
 int a3total = 0;
 int a4total = 0;
 int a5total = 0;
 
 a0 = analogread(0);
 serial.print(a0);
 a0total = a0total + a0;
 serial.print("|");
 serial.println(a0total);
 
 a1 = analogread(1);
 serial.print(a1);
 a1total = a1total + a1;
 serial.print("|");
 serial.println(a1total);
 
 a2 = analogread(2);
 serial.print(a2);
 a2total += a2total + a2;
 serial.print("|");
 serial.println(a2total);
 
 a3 = analogread(3);
 serial.print(a3);
 a3total = a3total + a3;
 serial.print("|");
 serial.println(a3total);
 
 a4 = analogread(4);
 serial.print(a4);
 a4total = a4total + a4;
 serial.print("|");
 serial.println(a4total);
 
 a5 = analogread(5);
 serial.print(a5);
 a5total = a5total + a5;
 serial.print("|");
 serial.println(a5total);
 
 serial.println("~~~~~~~");
 delay(3000);
}

here example of serial monitor's output first 2 iterations of loop. seems 6 detecting light correctly:
292|292
218|218
206|206
229|229
175|175
213|213
~~~~~~~
291|291
217|217
206|206
229|229
175|175
212|212
~~~~~~~

i use second set of numbers in line add total of inputs each iteration each input. way, on time,   can clear picture of photoresistor more/less sensitive. (of course when part working correctly divide total each input number of iterations.)

i know in c++ can use  
a5total += a5;
but didn't work either, figured try simpler. alas, neither work. whats problem?

first alway post code in code tags # above.

code: [select]
void loop()
{
  int a0 = 0;
  int a1 = 0;
  int a2 = 0;
  int a3 = 0;
  int a4 = 0;
  int a5 = 0;
 
  int a0total = 0;
  int a1total = 0;
  int a2total = 0;
  int a3total = 0;
  int a4total = 0;
  int a5total = 0;


each time enter loop a0total ect "created" , set 0. each time end of loop these loacal vars ar lost , there values. give them global scope , scope

mark


Arduino Forum > Using Arduino > Programming Questions > numbers aren't adding up correctly with += or + operators. EX: "a = a + b"


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support