Slow to respond to analogRead?

Hi everyone!,

First go at doing anything like this so bare with me!

I'm using an Arduino Nano to control an addressable LED taillight for a motorbike, using the original analog output of the bikes brake and indicator stepped down to 5v and there seems to be a delay between the signal being sent to the arduino and it responding with its digital command? its not a huge gap, but as im using the indicator signal straight from the bikes relay any delay translates to a reduced time the indicator flashes if that makes sense?

I've did a bit of research and I believe it has to do with the time it takes to convert an analog signal to digital in the arduino, Is this something that can be changed or is it a limitation with the hardware?

The problem is with your code.

Connor9422:
Hi everyone!,

First go at doing anything like this so bare with me!

I've did a bit of research and I believe it has to do with the time it takes to convert an analog signal to digital in the arduino, Is this something that can be changed or is it a limitation with the hardware?

So you read a digital in and write a digital out?

You don't say how long the delay is. It does take time to convert, but I doubt it is a humanly noticeable delay.

Are you sending stuff to Serial? That takes quite a long time, and while useful for debugging is probably pointless on your bike,.

Could you set the hardware up without the bike and test it on your kitchen table...?

ahh! I used serial as a voltmeter for testing and didn't take it out so that could be the problem!

Sorry if im not making it very clear! I have for example the brake going to pin A0 and then out pin 3 for the signal for the led's if that makes sense?

The delay is around 1/4 of a second.

Yes thats what I have been doing so far, I have it all in a little project box now but its fairy easy to hook up to the bike battery for testing.

Terrible picture but basically;

the led's are in three sections output pins 3,5,6

As you can't physically turn the lights off when the ignition is on I used the taillight feed to power the arduino, and the three feeds from both indicators and brake light.

Here's the very sketchy code;

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif

Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(13, 6, NEO_GRB + NEO_KHZ800); // rightInd (orange)
Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(6, 3, NEO_GRB + NEO_KHZ800); // brakeLight (white)
Adafruit_NeoPixel strip_c = Adafruit_NeoPixel(13, 5, NEO_GRB + NEO_KHZ800); // leftInd (blue)

int rightInd = A0;
int brakeLight = A2;
int leftInd = A4;

////////////////////////////////////////////////////////////////////////
void setup() {

Serial.begin(9600);
strip_a.begin();
strip_b.begin();
strip_c.begin();
strip_a.show();
strip_b.show();
strip_c.show();
pinMode(rightInd, INPUT); // A0
pinMode(brakeLight, INPUT); // A2
pinMode(leftInd, INPUT); // A4
}

//////////////////////////////////////////////////////////////////////

void loop() {

///////////////////////
int voltageIn = analogRead(A0);
Serial.println (voltageIn);
delay (200);
//////////////////////
int voltageIn2 = analogRead(A2);
Serial.println (voltageIn2);
delay (200);
//////////////////////
int voltageIn3 = analogRead(A4);
Serial.println (voltageIn3);
delay (200);
////////////////////////

int rightIndIn = analogRead (A0); ////strip_a - right indicator -
int brakeLightIn = analogRead (A4); ////strip_b - brake light -
int leftIndIn = analogRead (A2); ////strip_c - left indicator -

if (rightIndIn >= 100){
strip_a.setPixelColor (0, 70, 35, 0);
strip_a.setPixelColor (1, 70, 35, 0);
strip_a.setPixelColor (2, 70, 35, 0);
strip_a.setPixelColor (3, 70, 35, 0);
strip_a.setPixelColor (4, 70, 35, 0);
strip_a.setPixelColor (5, 70, 35, 0);
strip_a.setPixelColor (6, 70, 35, 0);
strip_a.setPixelColor (7, 70, 35, 0);
strip_a.setPixelColor (8, 70, 35, 0);
strip_a.setPixelColor (9, 70, 35, 0);
strip_a.setPixelColor (10, 70, 35, 0);
strip_a.setPixelColor (11, 70, 35, 0);
strip_a.setPixelColor (12, 70, 35, 0);
strip_a.show();
} else {
strip_a.setPixelColor (0, 20, 0, 0);
strip_a.setPixelColor (1, 20, 0, 0);
strip_a.setPixelColor (2, 20, 0, 0);
strip_a.setPixelColor (3, 20, 0, 0);
strip_a.setPixelColor (4, 20, 0, 0);
strip_a.setPixelColor (5, 20, 0, 0);
strip_a.setPixelColor (6, 20, 0, 0);
strip_a.setPixelColor (7, 20, 0, 0);
strip_a.setPixelColor (8, 20, 0, 0);
strip_a.setPixelColor (9, 20, 0, 0);
strip_a.setPixelColor (10, 20, 0, 0);
strip_a.setPixelColor (11, 20, 0, 0);
strip_a.setPixelColor (12, 20, 0, 0);
strip_a.show();
}

if (leftIndIn >= 100){
strip_c.setPixelColor (0, 70, 35, 0);
strip_c.setPixelColor (1, 70, 35, 0);
strip_c.setPixelColor (2, 70, 35, 0);
strip_c.setPixelColor (3, 70, 35, 0);
strip_c.setPixelColor (4, 70, 35, 0);
strip_c.setPixelColor (5, 70, 35, 0);
strip_c.setPixelColor (6, 70, 35, 0);
strip_c.setPixelColor (7, 70, 35, 0);
strip_c.setPixelColor (8, 70, 35, 0);
strip_c.setPixelColor (9, 70, 35, 0);
strip_c.setPixelColor (10, 70, 35, 0);
strip_c.setPixelColor (11, 70, 35, 0);
strip_c.setPixelColor (12, 70, 35, 0);
strip_c.show();
} else {
strip_c.setPixelColor (0, 20, 0, 0);
strip_c.setPixelColor (1, 20, 0, 0);
strip_c.setPixelColor (2, 20, 0, 0);
strip_c.setPixelColor (3, 20, 0, 0);
strip_c.setPixelColor (4, 20, 0, 0);
strip_c.setPixelColor (5, 20, 0, 0);
strip_c.setPixelColor (6, 20, 0, 0);
strip_c.setPixelColor (7, 20, 0, 0);
strip_c.setPixelColor (8, 20, 0, 0);
strip_c.setPixelColor (9, 20, 0, 0);
strip_c.setPixelColor (10, 20, 0, 0);
strip_c.setPixelColor (11, 20, 0, 0);
strip_c.setPixelColor (12, 20, 0, 0);
strip_c.show();
}

if (brakeLightIn >= 100){
strip_b.setPixelColor (0, 255, 0, 0);
strip_b.setPixelColor (1, 255, 0, 0);
strip_b.setPixelColor (2, 255, 0, 0);
strip_b.setPixelColor (3, 255, 0, 0);
strip_b.setPixelColor (4, 255, 0, 0);
strip_b.setPixelColor (5, 255, 0, 0);
strip_b.show();
} else {
strip_b.setPixelColor (0, 20, 0, 0);
strip_b.setPixelColor (1, 20, 0, 0);
strip_b.setPixelColor (2, 20, 0, 0);
strip_b.setPixelColor (3, 20, 0, 0);
strip_b.setPixelColor (4, 20, 0, 0);
strip_b.setPixelColor (5, 20, 0, 0);
strip_b.show();
}

}

How are you stepping down the voltage to 5v?

Serial will slow it down but the biggest problem is these delays:

Connor9422:

  ///////////////////////

int voltageIn = analogRead(A0);
  Serial.println (voltageIn);
  delay (200);
  //////////////////////
  int voltageIn2 = analogRead(A2);
  Serial.println (voltageIn2);
  delay (200);
  //////////////////////
  int voltageIn3 = analogRead(A4);
  Serial.println (voltageIn3);
  delay (200);
  ////////////////////////

That means every loop is delayed 600ms. You are also making a lot of potentially unnecessary calls to setPixelColor() every loop but I think just getting rid of the unused Serial and delays will mostly fix your problem. If you wanted to keep the Serial stuff for debugging purposes but turn it off when the sketch is in actual use you can switch it on and of with an if statement or a preprocessor directive.

DrAzzy:
How are you stepping down the voltage to 5v?

Little £0.99 cigarette lighter usb chargers from ebay aha

I'm not too great at this stuff and know they step it down to around 5V

pert:
Serial will slow it down but the biggest problem is these delays:That means every loop is delayed 600ms. You are also making a lot of potentially unnecessary calls to setPixelColor() every loop but I think just getting rid of the unused Serial and delays will mostly fix your problem. If you wanted to keep the Serial stuff for debugging purposes but turn it off when the sketch is in actual use you can switch it on and of with an if statement or a preprocessor directive.

ahh thanks!!

Would it also work if I put the three separate LED sections in different loops?

Connor9422:
Would it also work if I put the three separate LED sections in different loops?

I'm not sure what you mean by that. You can only have one loop() function, you could call other functions from loop() but that won't provide any benefit for your current code. I am assuming we're talking about the unnecessary calls to setPixelColor() and show(). This will happen when the state of the light hasn't changed since the last loop so you're just setting the color to the same color it already is. This probably only causes a very small delay but if you start adding more features to your code little bits of unnecessary delay can add up and you may have things that require your program to be very responsive. So the way you could fix this is to only set the color when the state of the light needs to be changed. So you add global variables to store the light states:

boolean rightIndOn=false;
boolean leftIndOn=false;
boolean brakeLightOn=false;

And then you change your light control code to use the state, the right indicator code for example:

  if (rightIndOn == false && rightIndIn >= 100) {
    rightIndOn = true;
    strip_a.setPixelColor(0, 70, 35, 0);
    strip_a.setPixelColor(1, 70, 35, 0);
    strip_a.setPixelColor(2, 70, 35, 0);
    strip_a.setPixelColor(3, 70, 35, 0);
    strip_a.setPixelColor(4, 70, 35, 0);
    strip_a.setPixelColor(5, 70, 35, 0);
    strip_a.setPixelColor(6, 70, 35, 0);
    strip_a.setPixelColor(7, 70, 35, 0);
    strip_a.setPixelColor(8, 70, 35, 0);
    strip_a.setPixelColor(9, 70, 35, 0);
    strip_a.setPixelColor(10, 70, 35, 0);
    strip_a.setPixelColor(11, 70, 35, 0);
    strip_a.setPixelColor(12, 70, 35, 0);
    strip_a.show();
  }
  else if (rightIndOn == true && rightIndIn < 100) {
    rightIndOn = false;
    strip_a.setPixelColor(0, 20, 0, 0);
    strip_a.setPixelColor(1, 20, 0, 0);
    strip_a.setPixelColor(2, 20, 0, 0);
    strip_a.setPixelColor(3, 20, 0, 0);
    strip_a.setPixelColor(4, 20, 0, 0);
    strip_a.setPixelColor(5, 20, 0, 0);
    strip_a.setPixelColor(6, 20, 0, 0);
    strip_a.setPixelColor(7, 20, 0, 0);
    strip_a.setPixelColor(8, 20, 0, 0);
    strip_a.setPixelColor(9, 20, 0, 0);
    strip_a.setPixelColor(10, 20, 0, 0);
    strip_a.setPixelColor(11, 20, 0, 0);
    strip_a.setPixelColor(12, 20, 0, 0);
    strip_a.show();
  }

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Little £0.99 cigarette lighter usb chargers from ebay aha

Will be one of the reasons you have a delay.
These devices are small switchmode power supplies, they take time to start and run, so when you apply 12V it will take some time to get 5V output.

The delay(200) will also cause problems, because it is a blocking delay, everything stops until 200mS has run.
You do not need to measure analog input in your application.

Because the voltages you will be measuring will be zero/gnd or 12V, you can change the levels via a potential divider that will give you gnd to 5V, use digital input which will be input as 0 or 1.

Tom... :slight_smile:

pert:
I'm not sure what you mean by that. You can only have one loop() function, you could call other functions from loop() but that won't provide any benefit for your current code. I am assuming we're talking about the unnecessary calls to setPixelColor() and show(). This will happen when the state of the light hasn't changed since the last loop so you're just setting the color to the same color it already is. This probably only causes a very small delay but if you start adding more features to your code little bits of unnecessary delay can add up and you may have things that require your program to be very responsive. So the way you could fix this is to only set the color when the state of the light needs to be changed. So you add global variables to store the light states:

boolean rightIndOn=false;

boolean leftIndOn=false;
boolean brakeLightOn=false;



And then you change your light control code to use the state, the right indicator code for example:


if (rightIndOn == false && rightIndIn >= 100) {
   rightIndOn = true;
   strip_a.setPixelColor(0, 70, 35, 0);
   strip_a.setPixelColor(1, 70, 35, 0);
   strip_a.setPixelColor(2, 70, 35, 0);
   strip_a.setPixelColor(3, 70, 35, 0);
   strip_a.setPixelColor(4, 70, 35, 0);
   strip_a.setPixelColor(5, 70, 35, 0);
   strip_a.setPixelColor(6, 70, 35, 0);
   strip_a.setPixelColor(7, 70, 35, 0);
   strip_a.setPixelColor(8, 70, 35, 0);
   strip_a.setPixelColor(9, 70, 35, 0);
   strip_a.setPixelColor(10, 70, 35, 0);
   strip_a.setPixelColor(11, 70, 35, 0);
   strip_a.setPixelColor(12, 70, 35, 0);
   strip_a.show();
 }
 else if (rightIndOn == true && rightIndIn < 100) {
   rightIndOn = false;
   strip_a.setPixelColor(0, 20, 0, 0);
   strip_a.setPixelColor(1, 20, 0, 0);
   strip_a.setPixelColor(2, 20, 0, 0);
   strip_a.setPixelColor(3, 20, 0, 0);
   strip_a.setPixelColor(4, 20, 0, 0);
   strip_a.setPixelColor(5, 20, 0, 0);
   strip_a.setPixelColor(6, 20, 0, 0);
   strip_a.setPixelColor(7, 20, 0, 0);
   strip_a.setPixelColor(8, 20, 0, 0);
   strip_a.setPixelColor(9, 20, 0, 0);
   strip_a.setPixelColor(10, 20, 0, 0);
   strip_a.setPixelColor(11, 20, 0, 0);
   strip_a.setPixelColor(12, 20, 0, 0);
   strip_a.show();
 }

Thanks so much for the help!

I've been playing with it a little more and have found a new problem..

The left bank of LED's flicker when on, I have switched side for a test and the problem followed and their is no change in voltage when it does it. Its almost as if there is a delay hidden somewhere?

Here is the code;

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif




Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(3, 5, NEO_GRB + NEO_KHZ800); // rightInd 
Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(2, 3, NEO_GRB + NEO_KHZ800); // brakeLight 
Adafruit_NeoPixel strip_c = Adafruit_NeoPixel(3, 6, NEO_GRB + NEO_KHZ800); // leftInd 


int rightInd = A4;
int brakeLight = A2;
int leftInd = A0;

boolean rightIndOn=false;
boolean leftIndOn=false;
boolean brakeLightOn=false;

////////////////////////////////////////////////////////////////////////
void setup() {
  
  
  strip_a.begin();
  strip_b.begin();
  strip_c.begin();
  strip_a.show();
  strip_b.show();
  strip_c.show();
  pinMode(rightInd, INPUT);     // A4
  pinMode(brakeLight, INPUT);   // A2
  pinMode(leftInd, INPUT);      // A0
}

//////////////////////////////////////////////////////////////////////

void loop() {


  int rightIndIn = analogRead (A4);     ////strip_a - right indicator - 
  int brakeLightIn = analogRead (A2);   ////strip_b - brake light - 
  int leftIndIn = analogRead (A0);      ////strip_c - left indicator -
  

  if (rightIndOn == false && rightIndIn >= 100) {            /////// if right indicator is on
  rightIndOn = true;
  strip_a.setPixelColor ( 0, 70, 35, 0);
  strip_a.setPixelColor ( 1, 70, 35, 0);
  strip_a.setPixelColor ( 2, 70, 35, 0);
  strip_a.show(); 
        } else if (rightIndOn == true && rightIndIn < 100) {  ///// if nothing is on
          rightIndOn = false;
          strip_a.setPixelColor ( 0, 20, 0, 0);
          strip_a.setPixelColor ( 1, 20, 0, 0);
          strip_a.setPixelColor ( 2, 20, 0, 0);
          strip_a.show();
   }


  if (leftIndOn == false && leftIndIn >= 100){           ////////// if left indicator is on 
  leftIndOn = true;
  strip_c.setPixelColor ( 0, 70, 35, 0);
  strip_c.setPixelColor ( 1, 70, 35, 0);
  strip_c.setPixelColor ( 2, 70, 35, 0);
  strip_c.show();
         } else if (leftIndOn == true && leftIndIn >= 100){       /////// if nothing is on
           leftIndOn = false;
           strip_c.setPixelColor ( 0, 20, 0, 0);
           strip_c.setPixelColor ( 1, 20, 0, 0);
           strip_c.setPixelColor ( 2, 20, 0, 0);
           strip_c.show();
  }


  if (brakeLightOn == false && brakeLightIn >= 100){        ////// if brake is on 
  brakeLightOn = true;  
  strip_b.setPixelColor ( 0, 55, 0, 0);
  strip_b.setPixelColor ( 1, 55, 0, 0);
  strip_a.setPixelColor ( 0, 55, 0, 0);
  strip_a.setPixelColor ( 1, 55, 0, 0);
  strip_a.setPixelColor ( 2, 55, 0, 0);
  strip_c.setPixelColor ( 0, 55, 0, 0);
  strip_c.setPixelColor ( 1, 55, 0, 0);
  strip_c.setPixelColor ( 2, 55, 0, 0);
  strip_c.setPixelColor ( 3, 55, 0, 0);
  strip_b.show();
  strip_a.show();
  strip_c.show();
  }  else if (brakeLightOn == true && brakeLightIn < 100){  /////// if nothing is on
       brakeLightOn = false;
       strip_b.setPixelColor ( 0, 20, 0, 0);
       strip_b.setPixelColor ( 1, 20, 0, 0);
       strip_a.setPixelColor ( 0, 20, 0, 0);
       strip_a.setPixelColor ( 1, 20, 0, 0);
       strip_a.setPixelColor ( 2, 20, 0, 0);
       strip_c.setPixelColor ( 0, 20, 0, 0);
       strip_c.setPixelColor ( 1, 20, 0, 0);
       strip_c.setPixelColor ( 2, 20, 0, 0);
       strip_c.setPixelColor ( 3, 20, 0, 0);
       strip_b.show();
       strip_a.show();
       strip_c.show();
  }

 
}

Connor9422:
I have switched side for a test and the problem followed

Please explain what this means. What did you switch, what did the problem follow?

Please remove the unnecessary blank spaces in your code. A couple of blank lines between functions is fine and a single blank line to separate sections of code but you just have random huge spaces and it makes it much more work to scroll through. Please do a Tools > Auto Format before posting code. This will make it much easier for us to read and the indentation can help you to find bugs.

Thanks for the tip!

Sorry for not making this clear, I thought I maybe had a hardware issue so I switched the inputs for the indicators from left (pin A0) to right (pin A4) but the same flickering problem happened on the right indicator. I then did the same for the outputs (pins 5 and 6) to see if that made any difference but unfortunately it did the same thing so I'm fairly sure its a problem with the code.

am I making sense? :confused:

Hi its just a small error in your code.

else if (leftIndOn == true && leftIndIn >= 100){       /////// if nothing is on
          leftIndOn = false;

you should change this to:

else if (leftIndOn == true && leftIndIn < 100){       /////// if nothing is on
          leftIndOn = false;

ie. leftIndIn >= 100 should be changed to leftIndIn < 100

Hi,

Little £0.99 cigarette lighter usb chargers from ebay aha

Are you still using these to convert your 12v form the light circuits to the 5V for the arduino inputs?
If so, before stuffing around with your program, use a proper circuit, either a potential divider or an opto-coupler.

Tom.... :slight_smile:
See and read post #9, please!!!!!!!!!!!!!!!!!!!!!!!!!!!

Chaitanya1:
Hi its just a small error in your code.

else if (leftIndOn == true && leftIndIn >= 100){       /////// if nothing is on

leftIndOn = false;





you should change this to:



else if (leftIndOn == true && leftIndIn < 100){      /////// if nothing is on
          leftIndOn = false;




ie. leftIndIn >= 100 should be changed to **leftIndIn < 100**

I just noticed about 15 minutes after posting! thanks :slight_smile:

TomGeorge:
Hi,Are you still using these to convert your 12v form the light circuits to the 5V for the arduino inputs?
If so, before stuffing around with your program, use a proper circuit, either a potential divider or an opto-coupler.

Tom.... :slight_smile:
See and read post #9, please!!!!!!!!!!!!!!!!!!!!!!!!!!!

Hi Tom, I really appreciate the help!

unfortunately I still am, They seem to be working ok with an unnoticeable delay... but, They do look very sketchy. What would you recommend?

thought you lovely folks might like some pics for reference;



The big white plug is the OEM plug from the Triumph;

Black - Ground
Red - Power (used to be constant 12v for registration plate light)
Brown - 12v left indicator input
Blue - 12v brake input
Orange - 12v right indicator

Sure glad to help..