Go Down

Topic: Help controlling 3 LEDs based on analog output (Read 420 times) previous topic - next topic

mcart2jm

Dec 10, 2019, 07:35 pm Last Edit: Dec 10, 2019, 07:43 pm by mcart2jm
Hello,

I'm very new to using and programming Arduinos and am tasked with using one for a class project. My goal is to control three LEDs based on the output of an eTape Liquid Level Sensor (https://www.adafruit.com/product/2656).

I have a sketch that's outputing to the serial monitor and the values are adjusting as expected for variable water levels, but I'm having trouble using the analog read to control the LEDs. Below is my current sketch:

Code: [Select]


// the value of the 'other' resistor
#define SERIESRESISTOR 270   

// What pin to connect the sensor to
#define SENSORPIN A0

const int analogPin = A0;     // pin that the sensor is attached to
const int ledPinG = 13;       // pin that the LED is attached to
//const int ledPinY = 12;
//const int ledPinR = 11;
const int threshold = 962;   // threshold level that's in the range of the analog input


void setup(void) {
  // initialize the LED pin as an output:
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  pinMode(ledPinR, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop(void) {
 float reading;

  reading = analogRead(SENSORPIN);

  // convert the value to resistance
  reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Sensor resistance ");
  Serial.println(reading);

  delay(1000);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) //) && ((analogValue < threshold)){
    digitalWrite(ledPinG, HIGH);
  } else {
    digitalWrite(ledPinG, LOW);
  }

//    if ((analogValue > threshold)) && ((analogValue < threshold)) {
//    digitalWrite(ledPinY, HIGH);
//  } else {
//    digitalWrite(ledPinY, LOW);
//  }

//    if ((analogValue > threshold)) && ((analogValue < threshold)) {
//    digitalWrite(ledPinR, HIGH);
//  } else {
//    digitalWrite(ledPinR, LOW);
//  }

  print the analog value:
Serial.println(analogValue);
  delay(1000);        // delay in between reads for stability
}


 
There is a fair amount currently commented out as I'm trying to start small and get one LED turning on based on a value greater that the threshold value.

I'll be more than happy to answer any questions.

thank you for any help!

Josh

slipstick

Well that doesn't even compile because, among other things, you have 2 loop()s and you can't have 2 functions with the same name. So you haven't been seeing any results from that code.

How do you think that the first loop(), which is fiddling about with "sensor resistance", will help with your requirements?

Presumably if the LEDs are supposed to switch on at different levels you'll need more than one threshold?

Steve

mcart2jm

Apologies, I was not given much guidance on this when given the project. What I was observing was using the serial monitor.

To fix the issue with the 2 loop()s will I need to create a new void setup(*name*) for the second loop() as well as giving the first a unique name?

Thank you!

cattledog

Welcome to the forum. Good job using code tags on you first post.

Not such a good job on posting code which does not compile.

Quote
To fix the issue with the 2 loop()s will I need to create a new void setup(*name*) for the second loop() as well as giving the first a unique name?
No. Arduino sketches have only one setup() and one loop() function.

Why do you think you need more than one of each?

slipstick

Let me ask again...why do you need that first loop()? What does it give you that you need for the job of switching LEDs on and off?

It may be that I'm just being dim but I can't see that it adds anything useful. If you just delete (or comment out) everything from
void loop(void)
to
delay(1000);
;
and test the code the one LED you have should switch according to the output of whatever is on A0 which you have defined as both SENSORPIN and analogPin. Isn't that what you want to happen?

Steve

mcart2jm

Welcome to the forum. Good job using code tags on you first post.

Not such a good job on posting code which does not compile.

No. Arduino sketches have only one setup() and one loop() function.

Why do you think you need more than one of each?
Let me ask again...why do you need that first loop()? What does it give you that you need for the job of switching LEDs on and off?

It may be that I'm just being dim but I can't see that it adds anything useful. If you just delete (or comment out) everything from
void loop(void)
to
delay(1000);
;
and test the code the one LED you have should switch according to the output of whatever is on A0 which you have defined as both SENSORPIN and analogPin. Isn't that what you want to happen?

Steve
To answer both of these initial questions, it's entirely lack of knowledge on my part. I do apologize for jumping straight into the forums before getting a better grasp on the fundamentals, I'm under time constraints that guided my decision to do so. The first block of code was to get and format the read from the sensor, and the second was to control the LED. You are correct on what my basic goal is Steve, to control the turning on of an LED based on the value of the analogValue variable being greater than the threshold.

Thanks to your help I've been able to fix my code into something that actually compiles and uploaded it to the Arduino UNO. I've also switched from digital output pin 13 to 12 because my understanding is that there's a built in LED connected to pin 13 and I want to be sure that I'm controlling the LED properly.

It still does not appear to be switching on the LED despite the read from the analog pin being greater than the threshold.

Below is the properly compiling sketch:

Code: [Select]

// the value of the 'other' resistor
#define SERIESRESISTOR 270   

// What pin to connect the sensor to
#define SENSORPIN A0

const int analogPin = A0;     // pin that the sensor is attached to
const int ledPin = 12;        // pin that the LED is attached to
const int threshold = 962;   // threshold level that's in the reading range of the analog input

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  float reading;

  reading = analogRead(SENSORPIN);

  // convert the value to resistance
  reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Sensor resistance ");
  Serial.println(reading);

// read the value of the sensor:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  delay(1000);
}


Thank you again for your help and patience!

Josh

mcart2jm

I stand corrected, I noticed that my LED was on, only dimly. I swapped out the resistor for one of a lower value and it is more apparent.

Thank you both for your help and getting me back off the ground on this project!

I'm going to continue to work on this and see if I can integrate more LEDs as originally intended to mark different levels of fullness.

To make sure I understand something previously said in one of the replies:

 
Presumably if the LEDs are supposed to switch on at different levels you'll need more than one threshold?

Steve
I should create upper and lower threshold variables for my if comparison statements?

Thank you both again!

cattledog

Good progress.

Why are you working with both the calculated resistance value of the sensor and the direct analogRead() value for the threshold?

If you really want to use both, you should only use one value for the analogRead().

cattledog

Quote
I noticed that my LED was on, only dimly
You need to set the pinMode of the led pin to OUTPUT in setup. Resistance should be chosen for 10-20ma through the led. With a 5v Arduino and a red, yellow, or green led 200-300 ohms is about right given the forward voltage drop.

Code: [Select]

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



mcart2jm

Good progress.

Why are you working with both the calculated resistance value of the sensor and the direct analogRead() value for the threshold?

If you really want to use both, you should only use one value for the analogRead().
My initial final goal for this sensor project was to have it act as a liquid level sensor for a variable use manufacturing material, such as cutting fluid in machining, and to have the data from the sensor formatted into values that that would be written to a MariaDB database to import into SAP HANA for analytics and to also trigger an automated resource allocation once the tank hit a critical level (also what will be indicated by the single or one of the LEDs, depending on the progress I'm able to make.

Given the time constraints on the project my professor and I may have to settle for proof of concept given by the ability to trigger a desired output, LED, at the designated level.

mcart2jm

You need to set the pinMode of the led pin to OUTPUT in setup. Resistance should be chosen for 10-20ma through the led. With a 5v Arduino and a red, yellow, or green led 200-300 ohms is about right given the forward voltage drop.

Code: [Select]

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



Thank you! It should have been obvious that this was left out when I was combining the code to make something that would compile, It's now working beautifully with my original 220 ohm resistor.

slipstick

#11
Dec 11, 2019, 12:09 am Last Edit: Dec 11, 2019, 12:10 am by slipstick
So I guess to get the other LEDs working you mainly need to pick two more threshold values, maybe midThreshold = 700 and lowThreshold = 400. You'll need to choose the value to suit your needs.
Then you can do something like (NOT real code you'll need to sort the details out yourself)
Code: [Select]

if (analogValue > lowThreshold && value < midThreshold)  // between 400 and 700
{ switch on LED that means low level
}
if (analogValue < midThreshold && value < threshold) //between 700 and 962
{ switch the mid level LED on
}
if (analogValue > threshold)          // over 962
{ switch the high level LED on
}


Hope that makes some sense - Steve

mcart2jm

I was able to get all three working correctly very similarly to your reply, I made three new threshold variables, one for the upper level limit, and two for the mid level upper and lower limits combined in an if statement with the && operator.

Can't thank you enough for helping me sort this out!

cattledog

Good progress.

Quote
My initial final goal for this sensor project was to have it act as a liquid level sensor for a variable use manufacturing material, such as cutting fluid in machining
The e tape sensor function is based on the hydrostatic pressure of the fluid. This pressure depends on the density of the fluid.

Given the progress you have made, you may have the time to test some alternative fluids and
document any differences. You may wind up needing a calibration routine dependent on the fluid being measured.

Then you can move on to all your fancy data handling.

Go Up