Hello, I am currently working on a code for my versity project where two individual outputs fire at a time depending on different number of rotations. I am counting revolutions by IR sensor.I am facing troubles doing it. I am still getting outputs even if there's no input given on the IR. My code is below, can someone tell me what is wrong in it. TIA
(PS: I am kinda new in this so pardon me for any mistake )
int x = 0;
int cycle = 0;
int state = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop() {
int x = digitalRead(A0);
if (state == 0) {
switch (x) {
case 1: state = 1; cycle = cycle + 1; break; //measuring the revolution
case 0: state = 0; break;
}
}
if (x == LOW) {
state = 0;
}
Serial.println(cycle);
if (cycle > 0) {
if (cycle % 2 != 0) { //odd numbers of revolution will give output in pin 9
if (digitalRead(A0) == 1) {
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
}
else if (digitalRead(A0) == 0) { //no input so no output
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
}
if (cycle % 2 == 0) {
if (digitalRead(A2) == 1) { //even numbers of rotation will fire pin 10
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
}
else if (digitalRead(A2) == 0) {
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
}
}
}
}