I wrote Arduino code to receive analog input from different voltage dividers connected to four pins (A0 to A4) and analog input from a temperature sensor (LM35) to A5. analogRead
inside the for loop always returns zero whereas analogRead
outside the for loop returns correct values.
What has to be changed to get correct values?
int time=0,y=0;
float volt;
String str;
int countParallel=0;
float voltage[]={0,0,0,0};
float temperature=0;
int sensors_pin[]={A1,A2,A3,A4,A5};
String x="";
// the setup rouontine runs once when you press reset;
void setup() {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
Serial.begin(9600);
Serial.setTimeout(50);
}
// the loop routine runs over and over again forever;
void loop() {
Serial.println(analogRead(A5)*500/1023);
delay(1000);
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
x += (char)inChar;
}
}
// Parallel Disharging
// Serial.println(x);
if(x.toInt()==1) {
digitalWrite(13,HIGH);
if(countParallel<1) {
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
countParallel++;
}
for(int i=1,x=14;i<=5;i++,x++) {
if(i<4) {
Serial.println(analogRead(x)*(5/1023));
delay(100);
} else if(i==5) {
Serial.println(analogRead(x) * p(500/1023));
delay(100);
}
if(volt<2.8){
digitalWrite(i+1,HIGH);
} else if(temperature>40) {
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
}
}
}
// Series Discharge
if(x.toInt()==2){
for(int i=A0;i<=A4;i++){
if(i<A4) {
voltage[i] = analogRead(sensors_pin[i]) * (5/1023);
Serial.println(10);
delay(100);
} else if(i==A4) {
temperature = analogRead(sensors_pin[i])*(500/1023);
Serial.println(9);
delay(100);
}
if(volt<2.8) {
digitalWrite(i, HIGH);
delay(100);
} else if(temperature>40) {
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
}
}
}
}