I tried to code for the analog sensor output, to perform addition. How to solve the error?
i try to assign name, in1 for first output, in2 for second output and so on.
#include <stdio.h>
int i;
int in1, in2, in3;
int total;
int val;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for (int i = 0; i < 10; i++)
{ val = analogRead(A0);
if (i==0) {
val= analogRead(i);
in1=val;
Serial.println(in1,DEC);
delay(1000);}
else if (i==1) {
val= analogRead(i);
in2=val;
Serial.println(in2,DEC);
delay(1000);}
else (i==3) {
val1= analogRead(i);
int3=val;
Serial.println(in3,DEC);
delay(1000);}
return 0;}
total = in1 + in2 + in3;
Serial.println(total,DEC);
delay(1000);}
This is the error message i got:
assig1:29:21: error: expected ';' before '{' token
else (i=3) {
^
assig1:34:8: error: return-statement with a value, in function returning 'void' [-fpermissive]
return 0;}
^
exit status 1
expected ';' before '{' token
Thank you all for the helpful comment,
now I manage to run the code as what I expected.
however, the reading isn't correct, where it is not the exact output from A0 after several testing.
Do you all have any idea about this? is there something wrong in my code, that causes the output does not come from pin A0?
#include <stdio.h>
int i;
int in1, in2, in3, in4, in5, in6, in7, in8, in9, in10;
int total;
int val;
void setup()
{
Serial.begin(9600);
}
void loop()
{ val = analogRead(0);
delay(100);
for (int i = 0; i < 10; i++)
if (i==0) {
val= analogRead(i);
in1=val;
printf("value0 : %d \n",in1);}
else if (i==1) {
val= analogRead(i);
in2=val;
printf("value1 : %d \n",in2);}
else if (i==2) {
val= analogRead(i);
in3=val;
printf("value2 : %d \n",in3);}
else if (i==3) {
val= analogRead(i);
in4=val;
printf("value3 : %d \n",in4);}
else if (i==4) {
val= analogRead(i);
in5=val;
printf("value4 : %d \n",in5);}
else if (i==5) {
val= analogRead(i);
in6=val;
printf("value5 : %d \n",in6);}
else if (i==6) {
val= analogRead(i);
in7=val;
printf("value6 : %d \n",in7);}
else if (i==7) {
val= analogRead(i);
in8=val;
printf("value7 : %d \n",in8);}
else if (i==8) {
val= analogRead(i);
in9=val;
printf("value8 : %d \n",in9);}
else if (i==9) {
val= analogRead(i);
in10=val;
printf("value9 : %d \n",in10);}
total = in1 + in2 + in3 + in4 + in5 + in6 + in7 + in8+ in9 + in10;
printf("total : %d \n",total);
delay(1000);}
sorry, but i have to stick to this, as i still need to proceed the in1, in2, in3, .. so on later by adding more mathematical algorithm. By introducing an array, i have no clue in assigning each element in there for individual function
#include <stdio.h>
const int N_READINGS = 10;
int readings [N_READINGS];
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(0); // not sure what this is here for
delay(100);
unsigned long total = 0;
for (int i = 0; i < N_READINGS; i++) {
in [i] = analogRead(i);
printf("value %d : %d \n",i, in [i]);
total += in [i];
}
printf("total : %d \n", total);
delay(1000);
}
terW3_:
sorry, but i have to stick to this, as i still need to proceed the in1, in2, in3, .. so on later by adding more mathematical algorithm. By introducing an array, i have no clue in assigning each element in there for individual function
Then avoid using FOR loop, it have no purpose in your code if use anyway IF statements. Just simply sequentially read value one by one without it.
Anyway, using array in this case is indeed logical solution, if you need to preserve and use all results later.