Ashraf_Zolkopli:
@ ido8780:
I feel that you are using this as a feeding device,
first of if you could use an RTC would great for you.
you could also use Time library and TimeAlarm library to work out the Timing for each day.
just in case you want to still use millis();
the way I would approach this is by understanding blink without delay example.
/* Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
- LED attached from pin 13 to ground.
- Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number :
const int ledPin = 13; // the number of the LED pin
// Variables will change :
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
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;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
now what happen if I just have this ?
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
Serial.begin ( 9600 );
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
Serial.print ( interval / 1000 );
Serial.println ( " second had passed since last update" );
}
}
amazing what possibility that we could now do;
what if we change the Serial.print into a function?
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
Serial.begin ( 9600 );
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
doSomething ();
}
}
void doSomething ()
{
Serial.print ( interval / 1000 );
Serial.println ( " second had passed since last update" );
}
now the function could be doing anything you like, This is just an example...
now how about we add some definition on top of the code, thing that define whats a day, hour, minute, second is.
#define second(x) x1000
#define minute(x) x60000
#define hour(x) x3600000
#define day(x) x86400000
with this definition on top of my code, I think I could do something simple like
#define second(x) x1000
#define minute(x) x60000
#define hour(x) x3600000
#define day(x) x86400000
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = second(1); // interval at which to blink
void setup() {
Serial.begin ( 9600 );
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
doSomething ();
}
}
void doSomething ()
{
Serial.print ( interval / 1000 );
Serial.println ( " second had passed since last update" );
}
I think you'll see the functionality you wanted with the last example
lets make it more interesting?
#define second(x) x1000
#define minute(x) x60000
#define hour(x) x3600000
#define day(x) x86400000
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = second(1); // interval at which to blink
void setup() {
Serial.begin ( 9600 );
}
void loop()
unsigned long currentMillis = millis();
if (timeElapse(timeNow, previousMillis, interval) )doSomething ();
}
void doSomething ()
{
Serial.print ( interval / 1000 );
Serial.println ( " second had passed since last update" );
}
boolean timeElapse( unsigned long timeNow, unsigned long &timePrevious, unsigned long timeDelay) {
if ( timeNow - timePrevious >= timeDelay ) {
timePrevious = timeNow;
return 1;
}
return 0;
}
So now what do you think??
Hi listen I did what you say and its worked for me but when I put it in my code its not work, the problem its that the servo not repeat 7 time on the process
Here my code, I didnt notice everything yet because i didnt succes and i noitce only when i succes:
#include <IRremote.h>//Include IRremote library
#include <Servo.h>//Include Servo library
#define second(x) x*1000
#define minute(x) x*60000
#define hour(x) x*3600000
#define day(x) x*86400000
//Time
unsigned long previousMillis = 0;
const long bettwenTime = second(5);
//setting pins
//HIGH == LOW in ledone and ledt because i wired opsiate
int ledone = 12;//Set led one pin is 12
int ledt = 8;//Set led two pin is 8
int RECV_PIN = 11;//set reciver pin is 11
int servopin = 9;
Servo Mikeyservo;
IRrecv irrecv(RECV_PIN);//Set that reciver is from reciver group
decode_results results;
//Setup
void setup()
{
Mikeyservo.attach(servopin);//Attach the servo to pin 9
Mikeyservo.write(0);//Rest the servo
Serial.begin(9600);//Turn on Serial
irrecv.enableIRIn(); //Start the receiver
pinMode(ledone,OUTPUT);//Set led one pin as Output
pinMode(ledt,OUTPUT);//Set led two pin as Output
digitalWrite(ledone, HIGH);//Turn off the leds Look up
digitalWrite(ledt, HIGH);// ^
}
void feedingMikey()
{
functionGo();
Serial.println("happened");
}
void functionGo(){
delay(1000);
Mikeyservo.write(185);//Switch the servo to feed Mikey
delay(1000);//Wait 1 seconed
Mikeyservo.write(0);//Rest the servo
}
void loop() {
unsigned long currentMillis = millis();
//Reciver
if (irrecv.decode(&results))//Check if the reciver get results
{
Serial.println(results.value,DEC);
if(results.value == 557583603)//557583603 = button 1
{
Serial.println("Button One has been pressed");
delay(20);
if(digitalRead(ledone) == HIGH)
{
digitalWrite(ledone, LOW);
delay(20);//Debounce
}
else
{
digitalWrite(ledone, HIGH);
delay(20);//Debounce
}
}
irrecv.resume();//Resume the reciver
if(results.value == 557618283)//557618283 = button 2
{
Serial.println("Button two has benn pressed");
if(digitalRead(ledt) == HIGH)
{
digitalWrite(ledt,LOW);
delay(20);//Debounce
}
else
{
digitalWrite(ledt,HIGH);
delay(20);//Debounce
}
}
irrecv.resume();//Resume the reciver
if(results.value == 557620323)//557620323 = Button 3
{
for(int i=0;i<=7;i++)
{
if(currentMillis - previousMillis >= bettwenTime)
{
previousMillis = currentMillis;
Serial.println("Button three has been pressed");
feedingMikey();
}
}
}
}
}