I have a switch connected to D1 + 10k resistor, this works
My next switch is connected to A0 (last available pin) +10k, this do not work.
Any pro tip to get?
My code for this issue:
int switchTare = D1; // select the input pin for the switch
int switchTareValue = 0; // variable to store the value coming from the sensor
const int switchAlarm = A0;
int switchAlarmValue = 0; // variable to store the value coming from the sensor
Void loop()
1) This works
switchTareValue = analogRead(switchTare); // read switch tare S1
2) this work not
switchAlarmValue = analogRead(switchAlarm); // read switch alarm (A0)
void loop()
if (switchTareValue > 0) { /
tareValue = 1;
Serial.print("Tare valgt ");
Serial.println(tareValue);
delay(5000);
functionTare(); //call function tare
}
if (switchAlarmValue < 1) {
alarmValue = 1;
Serial.print("ALARM!!!! ");
Serial.println(alarmValue);
delay(5000); /
functionAlarm(); //call function alarm
}
else {
switchAlarmValue = 0;
}
Functions
void functionTare() {
scale.tare();
tareValue = 0; // set tare back to 0, tare function done
Serial.print("Tare done, tareValue set to: ");
Serial.println(tareValue);
}
// This function is also hardwired to a relay that can trigger an alarm/siren
void functionAlarm() {
Serial.print("ALARM sent to Cayenne: ");
Serial.println(alarmValue);
}
Still the ESP8266 has only one analog pin. and this switchAlarmValue = analogRead(switchAlarm); // read switch alarm (A0) will not work because you assign the value of switchAlarm as 0 which is not the A0 pin of the ESP8266, which only has ONE A:D converter.
Are you trying to make an Analog input pin act like a digital pin?
I have made a test code, it reads correct values between 0 and 1024
What is trouble is that if sentence don't seems to work, i get ALARM in any case
Code:
void setup() {
Serial.begin(57600);
}
void loop() {
Serial.print("Value: ");
Serial.println(analogRead(A0));
Serial.println("--------------------------");
if (analogRead(A0) < 1024); {
Serial.print("ALARM!!!! ");
delay(2000); //litt pause for å se teksten på serial print
//functionAlarm(); //call function alarm
}
}
Not sure, but one thing to check is that you read the pin twice, both in the Serial.println(... and later in the if (analogRead(A0)...
Is there a chance the value might have changed in between? Why not read the pin only once to be sure.
did you try printing out the A0 read to see what it is?
Serial.println( analogRead(A0) );//I bet most of the readings are below 1024?
if (analogRead(A0) < 1024); {
Serial.print("ALARM!!!! ");
delay(2000); //litt pause for å se teksten på serial print
//functionAlarm(); //call function alarm
}
The important thing to understand here is the difference between > (greater than) and < (less than).