LED Blink Off / On / Bright

So I have two ESP8266s connected via Serial (UART1 transmit from D4, UART0 receive on RX). I have an LED with 220Ohm resistor to ground on D1 on the receiving board. The grounds for both boards, and the LED, are connected to the same point. Both boards are connected / powered via USB. The sending script is here

// sender for test programs

 int letter_out;
  
void setup() {
  // initialize serials:
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  Serial.flush();
  for (int i = 65; i < 90; i++)
  {
 // send letter:
    letter_out = i;
    Serial1.write(letter_out);
    Serial.write(letter_out);
    delay(2000);
  }
}

and the receiving code is here

// pins for the LEDs:
const int redPin = 5; //D1 
bool redPinState;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pin output:
  pinMode(redPin, OUTPUT);
}

void loop() {
  // if there's any serial available, swap LED state:
  while (Serial.available() > 0) {

   if (redPinState)
   {
    digitalWrite(redPin,LOW);
        redPinState=0;
    }
    else
   {
    digitalWrite(redPin,HIGH);
    redPinState=1;
    };
    Serial.flush();
    }
  }

So what I expect to happen is that each time I send a serial message ( single letter) from the sender, the LED on the receiver will change state. It does, but rather than going on then off then on, etc, sometimes it will go on, then brighter, then back to on, then off, then on , then bright... etc. The timing matches the change of state I expect from the send timings, but it doesnt just go on and off. I'm thinking its some sort of grounding / charging / capacitance problem?? Any ideas ? I'm attaching a photo of the setup although its hard to get clear. Blue, Orange and Green are ground wires. Red is the signal wire.

Please post a wiring diagram.

Very simple. just tried this (absolute minimal version ) and get same results.

You forgot to draw the gnd connection (which is present in your pic)...

That's true, but I get the same results either way

Try send at 115200 bps.

Try with the builtin led (to eliminate the possibility of bad connection).

first thing to note is that Serial.flush() doesn't do what you think it's doing anymore. Do this instead:

while(Serial.available() != 0){
    Serial.read();
}

Second, your if statement doesn't have a condition. Since the variable its checking is a boolean, my guess is it's checking for a change? write a condition for your if() statements.

if (redPinState == 1){
    digitalWrite(redPin,LOW);
    redPinState=0;
}
else {
    digitalWrite(redPin,HIGH);
    redPinState=1;
}
while(Serial.available() != 0){
    Serial.read();
}

I can try that, but based on the consistent performance of the off/medium/bright LED display on the receiver board, in synch with the sent signals, I'm pretty confident that the connectivity of the LED is not the issue.

Thanks. I'll recheck the docs and see what it does. I was using it in another context and it seemed to be emptying the buffer : but maybe the buffer was always empty already....

It empties the transmit buffer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.