Read Analog input at specified interval

Hi Guy's Any help or a point in the right direction on how to read my analog input once every 30 secs as opposed to all the time ??

Cheers

Using this code as an example…
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1289788172/11#11

Add this to the top of your Sketch…

unsigned long previousMillis;

Replace interval with (30L*1000L).

Replace this…

// do your actions here that you want done on the intervals

…with your code to read an analog input.

Thanks for your reply.
I’m trying to create a battery controller and only want to read analog 0 once every 30 secs to stop rapid switching i will attach my code as it didn’t seem to work but i’m sure i have done it wrong. Please correct for me I have been driven nuts by this problem for days now, I’m still very new at this.
Thanks So Much
*/
// includes the library code, so LCD diaplay works.
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 7;
int buttonState = 0;

int ledPin = 9; // Select pin for LED and Relay
int ledPin1 = 8; // Select pin for LED/override
int sensorPin = A0; // Selects Analog input 0 as sensor
int sensorValue = 0; // Variable to store value from sensor
int sensorPin1 = A1; // Selects Analog input 1 as sensor
int sensorValue1 = 0; // Variable to store value from sensor

void setup() {

lcd.begin(16, 2); // Sets the number of rows & columns on LCD
pinMode(ledPin, OUTPUT); // Sets LED as an output.
pinMode(ledPin1, OUTPUT); // Sets LED as an output.
pinMode(buttonPin, INPUT);
}

float voltage = 0; // setup some variables
float voltage1 = 0;
float sensor = 0;
float sensor1 = 0;
long interval = (30L*1000L);
long previousMillis= 0;

void loop() { // let’s get measurin’
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
previousMillis = currentMillis;
sensorValue = analogRead(sensorPin); // Reads analog input.
sensorPin = voltage; // Connects sensor value with analog input 0.
if (voltage >= 13.2) // Sets voltage level that LED and RELAY turns ON.
digitalWrite(ledPin, HIGH); // Turns on LED and RELAY

sensorValue = analogRead(sensorPin); // Reads analog input.
sensorPin = voltage; // Connects sensor value with analog input 0.
if (voltage <= 12.6) // Sets minimum voltage level that LED and RELAY turns ON.
digitalWrite(ledPin, LOW); // Turns off LED and RELAY disconnects batteries

sensorValue1 = analogRead(sensorPin1);
sensorPin1 = voltage1;
if (voltage1 <= 8.0)
digitalWrite(ledPin, LOW);

sensor = analogRead(0); // Analog input 0
sensor1 = analogRead(1); // Analog input 1
voltage = (sensor19.53)/1024; // convert raw sensor value to millivolts
voltage1 = (sensor1
19.53)/1024; // convert raw sensor value to millivolts

buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
digitalWrite (ledPin1, HIGH);
else
digitalWrite (ledPin1, LOW);

lcd.print(“MAIN BATT:”); // Prints “Main Batt” on LCD Display
lcd.setCursor(12,0);
lcd.print(voltage,1); // Prints voltage from Sensor 1 (Analog Input 0)
lcd.setCursor(0, 1); // Selects line 2 on LCD Display
lcd.print(“AUX BATT:”); // Prints “Aux Batt:” on LCD Display
lcd.setCursor(12,1);
lcd.print(voltage1,1); // Prints voltage from sensor 2 (Analog Input 1)

lcd.setCursor(0, 0); // Resets LCD display for next loop

}

Thanks i seem to have got it now Many Thanks!

with putting the { } around the script in the places suggested my program now seems to stop everything for 30 sec and my button don’t work i will try to fix it but if you can see stright up whats wrong please tell me

This Script has 2 analog inputs ( Volt Meters ).
Displays Dual battery voltage on LCD Display 2x16.

Turns on a Digital output to join Main and Aux Battery together, also lights an LED.
Controls charging and timing curcuit, joining and disconnecting batteries as required.

LCD Connections:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • Red to +5V
  • Black to ground
    */
    // includes the library code, so LCD diaplay works.
    #include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin = 7;
int buttonState = 0;

int ledPin = 9; // Select pin for LED and Relay
int ledPin1 = 8; // Select pin for LED/override
int sensorPin = A0; // Selects Analog input 0 as sensor
int sensorValue = 0; // Variable to store value from sensor
int sensorPin1 = A1; // Selects Analog input 1 as sensor
int sensorValue1 = 0; // Variable to store value from sensor
unsigned long previousMillis;
void setup() {

lcd.begin(16, 2); // Sets the number of rows & columns on LCD
pinMode(ledPin, OUTPUT); // Sets LED as an output.
pinMode(ledPin1, OUTPUT); // Sets LED as an output.
pinMode(buttonPin, INPUT);
long interval = (30L*1000L);
long previousMillis = 0;
}

float voltage = 0; // setup some variables
float voltage1 = 0;
float sensor = 0;
float sensor1 = 0;

void loop() { // let’s get measurin’
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 30L*1000L)
{
previousMillis = currentMillis;

sensorValue = analogRead(sensorPin); // Reads analog input.
sensorPin = voltage; // Connects sensor value with analog input 0.
if (voltage <= 12.6) // Sets minimum voltage level that LED and RELAY turns ON.
digitalWrite(ledPin, LOW); // Turns off LED and RELAY disconnects batteries

sensorValue1 = analogRead(sensorPin1);
sensorPin1 = voltage1;
if (voltage1 <= 8.0)
digitalWrite(ledPin, LOW);

sensor = analogRead(0); // Analog input 0
sensor1 = analogRead(1); // Analog input 1
voltage = (sensor19.53)/1024; // convert raw sensor value to millivolts
voltage1 = (sensor1
19.53)/1024; // convert raw sensor value to millivolts

buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
digitalWrite (ledPin1, HIGH);
else
digitalWrite (ledPin1, LOW);
}
sensorValue = analogRead(sensorPin); // Reads analog input.
sensorPin = voltage; // Connects sensor value with analog input 0.
if (voltage >= 13.2) // Sets voltage level that LED and RELAY turns ON.
digitalWrite(ledPin, HIGH); // Turns on LED and RELAY}

lcd.print(“MAIN BATT:”); // Prints “Main Batt” on LCD Display
lcd.setCursor(12,0);
lcd.print(voltage,1); // Prints voltage from Sensor 1 (Analog Input 0)
lcd.setCursor(0, 1); // Selects line 2 on LCD Display
lcd.print(“AUX BATT:”); // Prints “Aux Batt:” on LCD Display
lcd.setCursor(12,1);
lcd.print(voltage1,1); // Prints voltage from sensor 2 (Analog Input 1)

lcd.setCursor(0, 0); // Resets LCD display for next loop

}

Also the second analog input is now affected by the first only huh?
Help Me

You need to work this out on your own, I think. Your coding style is what is causing your problem. Try putting the { after the if statement on a new line, and indenting everything up to the } one extra level. You will quickly see where your blocks start and end, and will see that you are only reading the button state when the interval expires.

Changing the interval when you measure battery voltage, but printing the value on the LCD every pass through loop will keep the LCD going, killing battery life. Seems counter-productive to me to minimize the frequency of the low battery life impact function rather than the high battery life impact function.

I second what PaulS said about indentation, and can you start using the code # icon when posting sketches, please? My scrolling thumb is starting to hurt.