I've been doing a bit more "learning by doing" supported by Simon Monk's 2 books, and have hit a snag that I don't seem to be able to find my way around. I started by testing some code in single passes in the void setup function, and it worked fine, but now I've moved it to void loop and added the elapsed time test, it fails at the line:
ctVolts[n] = analogRead(ctVoltPin); // Read the sample voltage from the CT
with a string of errors
sketch_RMS_2nd_step.ino: In function 'void loop()':
sketch_RMS_2nd_step:42: error: expected primary-expression before '=' token
sketch_RMS_2nd_step:42: error: expected primary-expression before ')' token
sketch_RMS_2nd_step:42: error: expected `;' before ')' token
Here's the code:
[quote]
[color=#7E7E7E]// sketch RMS Second Step[/color]
[color=#7E7E7E]// It seems that the maths is ok in principle. Need to centre the dc on A0[/color]
[color=#7E7E7E]// and check the division factor to read actual volts.[/color]
[color=#7E7E7E]// Moving the working code into loop() and changing the for loop into nested ifs[/color]
[color=#7E7E7E]// has broken it. I think there is a primary expression before "=" but clearly[/color]
[color=#7E7E7E]// something is causing the compiler to conclude otherwise.[/color]
[color=#CC6600]const[/color] [color=#CC6600]byte[/color] PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
[color=#CC6600]const[/color] [color=#CC6600]byte[/color] PS_16 = (1 << ADPS2);
#define ctVoltPin = A0;
[color=#CC6600]float[/color] ctVolts[40];
[color=#CC6600]float[/color] ctTotal = 0;
[color=#CC6600]float[/color] ctRMS = 0;
[color=#CC6600]long[/color] timeNow = [color=#CC6600]micros[/color]();
[color=#CC6600]long[/color] timeStart = [color=#CC6600]micros[/color]();
[color=#CC6600]int[/color] n = 0;
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
ADCSRA &= ~PS_128; [color=#7E7E7E]// remove prescale of 128[/color]
ADCSRA |= PS_16; [color=#7E7E7E]// add prescale of 16 (1MHz)[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"Starting Test"[/color]);
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{
[color=#CC6600]if[/color] ((timeNow - timeStart) > 4999); [color=#7E7E7E]// Has 5mS elapsed?[/color]
{
timeStart = timeNow;
[color=#CC6600]if[/color] (n = 39) [color=#7E7E7E]// Start again with the first array element - sample [/color]
{
n = 0; [color=#7E7E7E]// Reset the array element counter[/color]
ctRMS = [color=#CC6600]sqrt[/color](ctTotal/40); [color=#7E7E7E]// Calculate the mean of the total of the samples[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Sum of squares = "[/color]); [color=#7E7E7E]// and publish the results[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](ctTotal);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"The RMS of which = "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](ctRMS);
}
[color=#CC6600]else[/color] [color=#CC6600]if[/color] (n < 39) [color=#7E7E7E]// Count through the sample storage array[/color]
{
ctVolts[n] = [color=#CC6600]analogRead[/color](ctVoltPin); [color=#7E7E7E]// Read the sample voltage from the CT[/color]
ctVolts[n] = ctVolts[n]/100; [color=#7E7E7E]// Convert the sample to real Volts - the correct value needs to be determined[/color]
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Array element No. "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](n);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]" = "[/color]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](ctVolts[n]);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]" Volts"[/color]);
ctVolts[n] = pow(2, ctVolts[n]); [color=#7E7E7E]// Square each sample in the ctVolts array[/color]
ctTotal = ctTotal + ctVolts[n]; [color=#7E7E7E]// Total up the samples squared[/color]
n = n + 1; [color=#7E7E7E]// Step to the next array element [/color]
}
}
}
[/quote]
I know there are libraries out there that contain code to do stuff like this, but I'm at an early stage with Arduino, and want to practice on things I'll have a use for, not just incorporate others work.
Can anybody spot the cause?