Blink without delay not working

I'm using an Arduino Pro Micro and HC12 device in a remote control, talking to another device. Works fine...it's only a couple lines of code. I thought it would be good if I had an LED to indicate when the remote was turned on because the batteries I used were small Li-Ion and I didn't want to have to charge them if I didn't have to. So installed an LED and attached it to Pin 10 of the Pro Micro, and because I wanted it to blink at 1 second intervals when the remote joystick was idle OR in use. Therefore I used Blink Without Delay. Despite changing pins, changing timing parameters, my LED blinks at a really fast rate. I put a scope on it and it 25 Hz or 40 mS period. Why? I did notice that the delay used at the end of the joystick Serial.write HAS an effect. If I put delay at 10mS, the period changes to 50 Hz/20mS period. Why?

`/*  Name: MouseRemoteFEV3blink.ino
    By:            
    Start Date:    8Jun2023
    Last Rev:      8Jun2023
    MCU:           Arduino 
    Hardware:      Arduino ProMicro
    Description:   Remote built into Nunchuk case with HC12
    Vers History: V1.0   Start by Dejan Nedelkovski, www.HowToMechatronics.com
    V1.1 Using HC12 and Software Serial, to keep Serial available for monitor
    V2.0 Added Blink without Delay, using Millis for LED.
*/
#include <SoftwareSerial.h>
SoftwareSerial HC12(9, 8);  //9 to Tx of HC12, 8 to Rx

const int LED= 10;             
int ledState = LOW;            // initial state  For Pro Micro, LOW turns it on.
unsigned long previousMillis = 0;  // store last time LED was updated
const long interval = 1000;  // blink rate in milliseconds

int xAxis, yAxis;
void setup() {
  pinMode(LED, OUTPUT); 
  Serial.begin(9600);
  HC12.begin(9600);
  }
 void loop() {
  xAxis = analogRead(A0);  // Read Joystick X-axis
  yAxis = analogRead(A1);  // Read Joystick Y-axis
  Serial.write(xAxis / 4);  // Dividing by 4 for converting from 0 - 1023 to 0 - 256, (1 byte) range
  Serial.write(yAxis / 4);
  delay(10);

  
 //****************************************************
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
      }
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW) {
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  digitalWrite(LED, ledState);
 //******************************************************
 }

if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
     // } 

The closing bracket for the conditional is in the wrong place.
Look again at the example code more closely.

1 Like

Cattledog, Yeah, I was just returning to this forum to say I found the problem. Made the change, all is well. Thanks for quick response anyway!

If your question has been answered to your satisfaction, please mark the thread as solved.

1 Like

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