Current sensing and LED-bar - logic failure

Hey folks

Very first topic here, just registered. I’m also preeeeetty new to the Arduino-world, so please, polar-bear with me.

I’m trying to use an ACS712-module to measure the current pulled by a DC-motor. This is all fine and dandy.

Now I’d like to take this value and display it on one of those LED-bars

I’m using those modules not primarily for their controller they come with but because they’re easy to mount on a panel - anyway.

As I said, the current measuring works fine. I got it working up to a point where the current is measured and the desired range of LEDs are lit up.

Problem is - it only works ONCE. It’s not “looping” for a lack of a better word. Without the LED-part in the code, the ACS712 produces a steady amount of values posted in the serial monitor. As soon as I put in the code-bits for the LEDs and upload it, the current get’s measured once, I get one single value posted in the serial monitor and the LEDs light up accordingly to the value it measured. So, in principal it’s working, just not continuously.

I am very new to this and I’m quite impressed that I got this far already. But now I’ve hit a wall.

I am quite sure that I made something very basic very wrong.

Here’s the code. I’d be very happy if someone could point me in the right direction…

Thanks a lot guys!

/*
Measuring Current Using ACS712
*/
#include "LED_Bar.h"
LED_Bar myLED;
const int analogIn = A0;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
void setup(){
Serial.begin(9600);
}
void loop()
{
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
//Serial.print("Raw Value = " ); // shows pre-scaled value
//Serial.print(RawValue);
//Serial.print("\t mV = "); // shows the voltage measured
//Serial.print(Voltage,3);
// the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3);
// the '3' after voltage allows you to display 3 digits after decimal point
delay(200);

 if (Amps >2)
{for (int i=10;i<=10;)
   myLED.set_LED_Range(i);}
   
if (Amps <1.99 && Amps >1)
{for (int i=5;i<=10;)
   myLED.set_LED_Range(i);}

  
if (Amps <0.99 && Amps >0.1)
{for (int i=2;i<=10;)
   myLED.set_LED_Range(i);}

}
    for (int i = 10; i <= 10;)

An odd form of for loop as the loop variable is never incremented
Did you perhaps mean

    for (int i = 10; i <= 10; i++)

No... From what I understand, this just sets the range of LEDs that need to be switched on.

I got this from an example sketch the seller provided and it's working fine.

Except that the rest of the sketch seems flawed - as I explained...

Do I need to a different approach? Maybe like defining a variable based on the Amp-Value and link the LED-ranges to this variable?

so…

I got it working. I used a different method of choosing the LED-range, found this in another of supplied example codes.

Not sure why this works and makes to loop loop as it’s supposed to, but meh.

if (Amps <1.59 && Amps >1.4)
myLED.set_LED_Index(0b000001111111000);

No I need to figure out how to smooth the value to make this less jumpy.

I already searched for smoothing and came up with results, all of those use the input of an analog pin directly. I would like to use the value after the calculations are done, the “Amps”-value in my case.

Thanks for pointing me in the right direction - although I really don’t understand why it’s working now :smiley:

I got this from an example sketch the seller provided and it’s working fine.

I am surprised to hear that.

What would you expect to be printed by this program ?

void setup()
{
  Serial.begin(115200);
  for (int i = 10; i <= 10;)
  {
    Serial.println(i);
    delay(1000);
  }
}

void loop()
{
}
if (Amps <1.59 && Amps >1.4)

Do you think like that? Most people don’t. The compiler doesn’t care, but, for me, it is far easier to see that you are testing that a value is in a range by comparing to the low end first, and THEN the upper end.

if(Amps > 1.4 && Amps < 1.59)

Think about how you would express the statement in English, if you wanted to give Joey a car for his 16th, 17th, 18th, and 19th birthday.

if(age > 15 && age < 20)

You certainly wouldn’t think “if Joey’s age is less than 20 and more than 15, I’ll buy him a car…”. You’d think “if Joey’s age is more than 15 and less than 20, I’ll buy him a car”. (Feel free to insert Matchbox in front of car, if your budget is constrained, or if Joey’s a little shit.)