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?
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.
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();
}
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.
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
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
/*
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 !