Help with repeated serial command, Blink Without Delay

Hello all, second post here looking for help and hopefully someone can help me understand, and maybe even correct me, as to why this is happening.

Below is the code that I'm writing and attempting to use.

So, I should be able to send "2" to the Arduino, and have the led turn on, and really quickly, turn off again.

I'm planning on doing this with multiple led's this is why I'm not using delay, as they all need to be controlled independently.

The problem, however, is that the Led does turn on, turns off, and no longer works when I command the arduino to do so. It's certainly getting the command, as the RX light is flashing, but NOT my target Led.

Could anybody point out what/why I've done/gone wrong.

Thanks in advanced for any replies.

const int ledblinktime = 1000;
const int ledPin = LED_BUILTIN;
int current1Millis = 0;
int current2Millis = 0;
int startMillis = 0;

void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
while (!Serial);
}

void loop(){
unsigned long ledTime = millis();

if (Serial.available()){
  int state = Serial.parseInt();
  
  if (state == 2){
      digitalWrite(ledPin, HIGH);
      current2Millis - current1Millis >= ledTime;
          if(ledTime >= ledblinktime){
            ledTime = 0;
            current1Millis = 0;
            current2Millis = 0;
            digitalWrite(ledPin, LOW);
      }
    }
  }
}

(deleted)

You could “cheat” and use TPinOutput from TDuino:

#include <TDuino.h>

#define LED_PIN_1 3
#define LED_PIN_2 4

TPinOutput led1(LED_PIN_1), led2(LED_PIN_2);

void setup()
{
  Serial.begin(9600);
  while (!Serial) ;
  led1.setup();
  led2.setup();
}

void loop()
{
  //Loop leds
  led1.loop();
  led2.loop();
  if (Serial.available())
  {
    int command = Serial.parseInt();
    if (command == 1) led1.pulse(100, 1);
    else if (command == 2) led2.pulse(100, 1);
  }
}

Delay-free and easy! But you may run into trouble with the serial communication if you do not send each serial command with a delimiter (eg. new-line \n) and adjust the sketch to handle this.

Danois90:
You could “cheat” and use TPinOutput from TDuino:

#include <TDuino.h>

#define LED_PIN_1 3
#define LED_PIN_2 4

TPinOutput led1(LED_PIN_1), led2(LED_PIN_2);

void setup()
{
  Serial.begin(9600);
  while (!Serial) ;
  led1.setup();
  led2.setup();
}

void loop()
{
  //Loop leds
  led1.loop();
  led2.loop();
  if (Serial.available())
  {
    int command = Serial.parseInt();
    if (command == 1) led1.pulse(100, 1);
    else if (command == 2) led2.pulse(100, 1);
  }
}




Delay-free and easy! But you may run into trouble with the serial communication if you do not send each serial command with a delimiter (eg. new-line \n) and adjust the sketch to handle this.

This is handy, but how do I go about adding Tduino, as I’m getting this error message:

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\*Redacted*\Documents\Arduino\blinkwithoutled\blinkwithoutled.ino:1:20: fatal error: TDuino.h: No such file or directory

 #include <TDuino.h>

                    ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I’m assuming download the lot and then extract the contents into my arduino installation location?

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

On GitHub you download it as a ZIP. In the Arduino IDE, you can install a library as a ZIP.

Danois90:
On GitHub you download it as a ZIP. In the Arduino IDE, you can install a library as a ZIP.

Got it, I'll give it a try :slight_smile:

Robin2:
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Ok, I'll have a look over that again, thanks :slight_smile: