Pages: [1]   Go Down
Author Topic: analogread and lcd.print issue  (Read 1178 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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("%");
  }
}
   
   
Logged

Northern Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.  
« Last Edit: April 23, 2012, 11:44:46 pm by Wazzled » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Northern Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 24, 2012, 12:12:46 am by Wazzled » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 0
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 24, 2012, 12:54:53 am by iyahdub » Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Sr. Member
****
Karma: 0
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
  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 !
Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Sr. Member
****
Karma: 0
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Only difference in his example is the float calculation for Ohms, right ?!
Logged

10 LET Loop=Infinite
20 GO TO 10

Northern Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 24, 2012, 07:42:27 pm by Wazzled » Logged

Pages: [1]   Go Up
Jump to: