In my code I have an unwanted retard that i can't figure out. I want the output to stay HIGH for 80ms and then go LOW for 2ms but instead it goes LOW for 3.6ms. Can someone figure out why?
Here is the code where i put a HIGH after the 2ms LOW so that I could measure how much retard I had before
for (int s = 0; s < 8; s++) {
digitalWrite(output, HIGH);
delayMicroseconds(10000);
}
digitalWrite(output, LOW);
delayMicroseconds(20000);
digitalWrite(output, HIGH);
for context here is the whole code but I don't think it's needed
const int sensore = 0;
const int LED = 13;
const int output = 12;
const int terra = A5;
int serialCode[9] = { 1,1,0,1,0,1,0,1,0 };
unsigned long object;
bool objectDetected = false;
void setup() {
pinMode(sensore, INPUT);
pinMode(output, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(output, LOW);
analogWrite(terra, 180);
}
void loop() {
//led alto se il sensore non vede nulla
if (digitalRead(sensore) == LOW) {
digitalWrite(LED, HIGH);
}
if (digitalRead(sensore) == HIGH) {
digitalWrite(LED, LOW); //led basso se il sensore vede un corpo
object = millis();
while (object + 71 > millis()) { //verifica segnale alto per 70ms
if (digitalRead(sensore) == LOW) {
break;
}
if (object + 70 == millis()) {
objectDetected = true;
}
}
if (objectDetected) {
//Carrier Wave;
for (int s = 0; s < 8; s++) {
digitalWrite(output, HIGH);
delayMicroseconds(10000);
}
digitalWrite(output, LOW);
delayMicroseconds(20000);
digitalWrite(output, HIGH);
//Manchester encoding
for (int i = 0; i < 9; i++) {
if (serialCode[i] == 0) {
digitalWrite(output, HIGH);
delayMicroseconds(1000);
digitalWrite(output, LOW);
delayMicroseconds(1000);
} else {
digitalWrite(output, LOW);
delayMicroseconds(1000);
digitalWrite(output, HIGH);
delayMicroseconds(1000);
}
if (i == 8) {
digitalWrite(output, LOW);
delay(500);
objectDetected = false;
}
}
}
}
}