Go Down

Topic: analogread and lcd.print issue (Read 1 time) previous topic - next topic

oidium45

Hello, I am still pretty new to arduino, and C++ for that matter so please take that into consideration.
I have been trying out several different things to get comfortable and learn a bit.
Currently I am using the code from the "analoginput" sketch with a 1k POT adjusting the blink rate of an LED. I also have an OLED connected to display the potentiometer value. The problem that I am having is that the sensor value is only read and printed at each run of the loop. Since the POT adjusts the on/off delay of the LED, It also adjusts how long it takes for the loop to complete and restart. This in effect changes how frequently the sensor is measured and printed to the screen. This becomes a real problem if the delay set high...

My question..
Is there a way to run these two completely separately of one another? OR possibly a way to specify how frequently the sensor is read and the value is printed?

I have attached a copy of the code below.
Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(48, 49, 46, 47, 44, 45);

int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
float sensorValuePercentage;

void setup() {
  pinMode(ledPin, OUTPUT);
  lcd.clear();
  lcd.begin(20, 4);
  lcd.print("... loading");
  delay(200);
}
void loop() {
  {
    sensorValue = analogRead(sensorPin);
    sensorValuePercentage = (float)(sensorValue - 23) / 10;
   
    digitalWrite(ledPin, HIGH);
    delay(sensorValue);
    digitalWrite(ledPin, LOW);
    delay(sensorValue);
  }
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Potentiometer Value");
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.setCursor(6, 1);
  lcd.print("ohms");
  lcd.setCursor(0, 2);
  lcd.print(sensorValuePercentage);
  lcd.setCursor(6, 2);
  lcd.print("%");
  }
}
   
   

Wazzled

#1
Apr 24, 2012, 06:41 am Last Edit: Apr 24, 2012, 06:44 am by Wazzled Reason: 1
Rather than using delays (which basically stop the arduino for the given time) between switching LED state, you could use millis() and work out how long has passed since LED state was last changed.

Assuming the time you want the led on and off are equal, something like this might work.

Code: [Select]

int ledLastUpdate = 0;
int blinkTime = 500;

if(millis() - ledLastUpdate >= blinkTime)
{
//if led is on turn it off or if its off turn it on
ledLastUpdate=millis();
}


You can also use the pot to adjust blinkTime.  

oidium45

Actually, the LED is working great! It is the analogread and LCD.print that is a problem..

Wazzled

#3
Apr 24, 2012, 06:57 am Last Edit: Apr 24, 2012, 07:12 am by Wazzled Reason: 1
I thought your problem was rare printing to the LCD when the LED delay is high.

Its the delays used on your LED causing the problem.

My method replaces the LED delays with timing.  When you use a delay, you stop the arduino for whatever period of time.  By using timing the Arduino will check if it needs to switch the LED on or off and move on immediately.  This will allow you to read the pot and print to the LCD much quicker.

oidium45

Thank you! I will give it a try and see how it works

iyahdub

#5
Apr 24, 2012, 07:52 am Last Edit: Apr 24, 2012, 07:54 am by iyahdub Reason: 1
You might want to use his method and instead read the analog value to LCD. That would rid you of the issues you having as the delay will stop anything else until it ends
PS:DOH he had actually said the same lol
10 LET Loop=Infinite
20 GO TO 10

iyahdub

Actually you left me with a doubt now.
So if its not too much too ask, could you explain better ?!? Share your knowledge ?!?
Lets take the classic example from the tutorial
Code: [Select]

/*
  Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED)  connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().

The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground

* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.


Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/AnalogInput

*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
  // turn the ledPin on
  digitalWrite(ledPin, HIGH); 
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);         
  // turn the ledPin off:       
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                 
}

how would you implement your sugestion ?!?  Sorry if i sound DOH lol, but would appreciate it !
10 LET Loop=Infinite
20 GO TO 10

iyahdub

Only difference in his example is the float calculation for Ohms, right ?!
10 LET Loop=Infinite
20 GO TO 10

Wazzled

#8
Apr 25, 2012, 02:40 am Last Edit: Apr 25, 2012, 02:42 am by Wazzled Reason: 1
There is actually an example for this problem (delays).  Pretty much the same as I had explained but this is more detailed with a full example.

Take a look at it HERE

Go Up