LED State High and Low

Hello,

I posted this over on the Sensors forum and I am posting here since it is more of an LED question than a Sensor question.

I have several ACS712 sensors which monitor the current of 4 DC motors which are controlled outside of the Arduino. The arduino only controls the sensors and leds. What I need is when #1 motor is turned off the Red LED needs to stay on until #1 motor is energized again. If it is energized the LED will stay Red if not the Led will turn green. My code is below. Right now the LEDS are controlled when the motors are energized or not. Not all the motors will be on at the same time as they run in cycles. Sorry if this sounds confusing and thanks in advance for the help.

if(energized){
digitalWrite(12, HIGH); // Red Led Turns On When Energized
digitalWrite(11, LOW); // Green Led Is Off
} else {
digitalWrite(12, LOW); // Red Led Is Off
digitalWrite(11, HIGH); // Green Led Turns On When Not Energized
}

Right now when none of the motors are energized all Leds turn Green. I guess I need to know the state of the LED when the motors are cycled again.

To reverse the led indicators.

Instead of:
digitalWrite(12, HIGH); // Red Led Turns On When Energized
digitalWrite(11, LOW); // Green Led Is Off
Use:
digitalWrite(12, LOW); // Red Led Turns On When Energized
digitalWrite(11, HIGH); // Green Led Is Off

.

Reversing the LED indicators is not what I need. Let me explain in greater detail:

Motors not energized LED is Green, Motors when energized LED turns Red. I am using Bi-Color leds. What I would like to accomplish is this: When the motors de-energize I would like the RED LED to remain on as the last state the motor was in. IF the motor does not energize during cycle 2 the LED turns Green. If the motor energizes LED stays Red. I have 4 motors and they run at different cycles so not every motor is on at the same time.

Baffling! Cycles?

BTW are the leds always either red or green while the circuit is running? Are they ever off? Do they have 2 leads or 3?

Cycle #1: Motor #1 when energized Red Led lights, de-energized Green Led lights...however I want the Red Led too stay lit until
Cycle #2: IF Motor #1 does not energize the Red Led turns off and the Green Led comes on, IF Motor #1 energized the Red Led stays lit.

Right now the LEDS go on and off based on the current being sensed by the ACS712 current sensors.

The motors are NOT being controlled by the Arduino....just the LEDS and ACS712 sensors.

Yes the LEDS are either Red or Green at all times giving status of the current....they are never off...2 leads as they are bi-polor leds

There's nothing in the tiny fragment of code you posted that is doing anything which could even possibly be taking any notice of whatever a "Cycle" means to you. Why not post the full code and also explain how you define "energised" and the start and end of "Cycle#1" and "Cycle#2".

I'm sure you know what you're talking about but you're not communicating it clearly.

Steve

If the leds had 3 leads and were never off, was going to point out that you could control each one with a single Arduino pin, but never mind.

Can you draw us some kind of time-line diagram that would help us understand the logic you are trying to describe?

@PilotinControl, please stop cross-posting.

as I said earlier did not mean to cross post...one thread had to do with ACS712 sensors and this thread has to do with LEDs. My code is below:

const int adcpin  = A0; // SENSOR #1
const int adcpin2 = A1; // SENSOR #2

// SAMPLING PARAMETERS

const unsigned long sampleTime = 58000UL; 
const unsigned long numSamples = 200UL; 
const unsigned long sampleInterval = sampleTime/numSamples; 

#define SENSITIVITY 185            // 185
#define DETECTION_MULTIPLIER 3.095 // 1.095
#define CALIBRATION_READS 300

// variables to hold sensor quiescent readings

float aqv;  // Average Quiescent Voltage; e.g. ADC Zero
float aqc;  // Average Quiescent Current; 
float aqv2;  // Average Quiescent Voltage; e.g. ADC Zero
float aqc2;  // Average Quiescent Current; 

void setup(){
  
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9,  OUTPUT);
  
Serial.begin(9600);

// SENSOR #1
  
Serial.print(adcpin);
Serial.print("   ");
Serial.println(adcpin2);
aqv = determineVQ(adcpin); 
Serial.print("AQV: ");
Serial.print(aqv * 1000, 4);
Serial.print(" mV\t");
aqc = determineCQ(adcpin, aqv);
Serial.print("AQC: ");
Serial.print(aqc * 1000, 4);
Serial.print(" mA\t");
float sense = (aqc * DETECTION_MULTIPLIER) - aqc;
Serial.print("DETECTION SENSITIVITY: ");
Serial.print(sense * 1000, 4);
Serial.println(" mA");

// SENSOR #2

aqv2 = determineVQ(adcpin2); 
Serial.print("AQV: ");
Serial.print(aqv2 * 1000, 4);
Serial.print(" mV\t");
aqc2 = determineCQ(adcpin2, aqv2);
Serial.print("AQC: ");
Serial.print(aqc2 * 1000, 4);
Serial.print(" mA\t");
float sense2 = (aqc2 * DETECTION_MULTIPLIER) - aqc2;
Serial.print("DETECTION SENSITIVITY: ");
Serial.print(sense2 * 1000, 4);
Serial.println(" mA\n\n");

delay(3000);
}

void loop(){
float current = readCurrent(adcpin, aqv);
float delta = abs(aqc - current);
bool energized = delta > ((aqc * DETECTION_MULTIPLIER) - aqc);

float current2 = readCurrent(adcpin2, aqv2);
float delta2 = abs(aqc2 - current2);
bool energized2 = delta2 > ((aqc2 * DETECTION_MULTIPLIER) - aqc2);

Serial.print("CURRENT SENSED: ");
Serial.print(current * 1000,3);
Serial.print("   ");
Serial.print(current2 * 1000,3);
Serial.print(" mA\t");
  
if(energized){
Serial.print("ENERGIZED");
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
} else {
Serial.print("NOT ENERGIZED");
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
}
Serial.print("   ");

if(energized2){
Serial.println("ENERGIZED");
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
} else {
Serial.println("NOT ENERGIZED");
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
}
delay(100);
}

//////////////////////////////////////////
//
// CURRENT SENSOR FUNCTIONS
//
//////////////////////////////////////////

float readCurrent(int pin, float adc_zero){
float currentAcc = 0;
unsigned int count = 0;
unsigned long prevMicros = micros() - sampleInterval ;
while (count < numSamples){
if (micros() - prevMicros >= sampleInterval){
float adc_raw = (float) analogRead(pin) - adc_zero; // sensor reading in volts
adc_raw /= SENSITIVITY; // convert to amperes
currentAcc += (adc_raw * adc_raw); // sum the squares
count++;
prevMicros += sampleInterval;
}
}

float rms = sqrt((float)currentAcc / (float)numSamples);
return rms;
}

//////////////////////////////////////////
//
// POWER MUST BE OFF DURING
// CALIBRATION
//
//////////////////////////////////////////

float determineVQ(int pin) {
  float VQ = 0;
  //read a large number of samples to stabilize value
  for (int i = 0; i < CALIBRATION_READS; i++) {
    VQ += analogRead(pin);
    delayMicroseconds(sampleInterval);
  }
  VQ /= CALIBRATION_READS;
  return VQ;
}

float determineCQ(int pin, float aqv) {
  float CQ = 0;
  // set reps so the total actual analog reads == CALIBRATION_READS
  int reps = (CALIBRATION_READS / numSamples);
  for (int i = 0; i < reps; i++) {
    CQ += readCurrent(pin, aqv);
  }
  CQ /= reps;
  return CQ;
}

I need the RED LED to stay on when the current goes off to show last state of motor...if the motor starts up again the led will remain red if not the led will turn green.

I need the RED LED to stay on when the current goes off to show last state of motor...if the motor starts up again the led will remain red if not the led will turn green.

That statement is a nonsense. You can not have a state where the LED stays on and also turns off. You have not explained under what circumstances the LED will turn off. Are you thinking of maybe it turns off after a certain time of the current being off?

I would advise you to answer the questions you are asked instead of just stating nonsense. We are trying to help you but your poor communication skills are not helping.

Ok... I am trying here. Think of the LEDs as status lights and as you can see by the code the current sensors are being used as monitors if the current is ON the LED turns RED and if there is NO current the LED is GREEN. What I need to happen is for the RED LED to remain ON until the current is sensed again. If that sensor senses current the RED LED remains on if it does NOT the LED turns GREEN. I need to implement a state of change or a last status of current sensor?

Sorry but no you still have a logical paradox, can you not see is?

A sensor will sense all the time. Do you mean stay on until the sensor is read again and found no current? If so then turn the LED off after you read the sensor, then it will turn on immediately after if there is current. You will not notice the time when the LED is off. However only reading the sensor at intervals longer than it would take to notice an LED is still on is a bit of a rubbish code and not what you origional code does.

It sounds to me, as I think it does to Mike, that this is a "timeout" indicator. Red indicates that either the current is flowing now or has been flowing within the last time period T. Green indicates that no current has flowed within the last T period. Correct?

PilotinControl:
What I need to happen is for the RED LED to remain ON until the current is sensed again. If that sensor senses current the RED LED remains on if it does NOT the LED turns GREEN. I need to implement a state of change or a last status of current sensor?

When you say "until the current is sensed again." do you actually mean "until the current sensor is CHECKED or READ again". That might make some sort of sense.

So current is sensed- turn RED on. Some time later check sensor again - if any current now stay RED, if no current now go GREEN ?

But I'm just guessing.

Steve

Steve got it...until the sensor is checked again... if the current is flowing or not the LED needs to keep its last action active say RED....if current isn't flowing after so much time go Green... sorry thats what I was trying to portray....as my current code does not do that and I am looking for a way to do it. Thanks again for the help.

and I am looking for a way to do it.

And reply #11 told you exactly how to do it, and why you might probably think it was not working.