Hey everyone, I'm kind of in a bind. I'm a huge newb when it comes to programming but I have a issue that can probably be resolved by one of you guys in like 5 minutes.
I was trying to power a device for a set interval on/off and I realized I can't use the delay function because I have a temperature sensor that needs to constantly getting data.
I have been looking at the BlinkWithoutDelay code and I can't figure out how to make it so that I can use millis() to turn on a device for 16 hours, turn it off for 8 hours, and repeat.
Would anyone possible point me in the right direction or write this simple code for me?
In what way does that example not help? You need a longer interval (bearing in mind 1000 milliseconds is one second) and inside the loop, while you are waiting for the time to elapse, you check the temperature sensor.
Because I don't know what to write when I want it to stay off for a set interval. From playing around the example, I can only get it to stay on for the desired period, but then it stays off for the same time period. Ex. I can get it to stay on for 10 seconds but it stays off for 10 seconds, then repeats.
I do not have the time to order a RTC, I was thinking about that also but this is due in a week.
Here's some code of mine that will give you the idea.
Ignore the commented out stuff, just look at the variables 'present' and 'var2'
the variables have to be 'unsigned long' to hold the high numbers
This code makes a buzzer buzz for 3.5 minutes
// The circuit:
//* LED attached from pin 13 to ground
//* pushbutton attached to pin 2 from +5V
//* 10K resistor attached to pin 2 from ground
//Buzzer on Pin 9
//LED on Pin 14, analog 0
int Touch;
int speaker = 9;
int tone = 1500;
unsigned long var1 = 210000;
unsigned long var2 = 210200;
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
// the number of the LED pin
long unsigned Present = 0;
void setup() {
// initialize the LED pin as an output:
/*
pinMode(3, OUTPUT);
pinMode(4,INPUT);
pinMode (5, OUTPUT);
pinMode(6,INPUT);
pinMode (7, OUTPUT);
pinMode(8,INPUT);
pinMode (12, OUTPUT);
*/
Serial.begin(9600);
// initialize the pushbutton pin as an input:
// pinMode(buttonPin, INPUT);
}
void loop(){
// buttonState = digitalRead(buttonPin);
/*
if (buttonState == HIGH) {
Serial.println("ON");
var1= Present + 210000;
var2= Present + 210200; //buzz for 200 ms
}
*/
//BUZZER
Present = millis();
if (Present > 0 && Present < var2){ // 3.5 MINUTES
digitalWrite (speaker, HIGH);
delayMicroseconds (tone / 2);
digitalWrite (speaker, LOW);
delayMicroseconds (tone / 2);
tone = 1500;
tone = map(tone, 0, 1023, 1000, 5000);
//outputs LEDs = 3,5,7,12
} else {
digitalWrite(speaker, LOW);
}
/*
if (Present > var2){
Present=millis();
} else {
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
digitalWrite(12,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
digitalWrite(8,LOW);
}
*/
}
You use the BlinkWithoutDelay example, and just change the times when you blink. Like this:
const int ledPin = 13; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 16000L; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT);
} // end of setup
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval = 16000L; // turn on for 16000ms
} // end if was low (and is now high)
else
{
ledState = LOW;
interval = 8000L; // turn off for 8000ms
} // end of was high (and is now low)
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} // end of loop
This turns the LED on for 16 seconds and off for 8. Now changes seconds to hours and you have it.
Okay, this is actually a very straightforward issue, but I can see how its a but confusing to someone new to programming.
Do this. Push the computer aside and break out a paper and pencil. Think about what the program needs to do. Turn something on for a period of time, then off for a different period of time.
So outline it without using code syntax. Something like:
1 - turn the device on
2 - set a flag saying it is on
3 - get the time now and store it
4 - read my data and do something with it
5 - does the flag say it is on?
6 - If so, how much time has passed? do we turn off yet?
7 - if so, turn off, make a note of the time we turned it off and set that flag to off
8 - does the flag say it is off?
9 - If so, how much time has passed? do we turn on yet?
10 - if so, turn on, make a note of the time we turned it on and set that flag to on
11 - go back to step 4
Does this help you understand what you need to do? Once you have it sketched out codeing it is much easier.