Hey guys!!
I'm new here!! Really nice stuff around! Congrats!
I've just started trying to learn to code for arduino uno so bear with me :P. You see I'm a biologist with coding aspirations! lol.
Anyhow, I'm trying to make a simple circuit to work which would be using a servo and the built in led. Basically the idea is, that a servo will work every x hours (around 5h) four times and in between the led will blink to identify which part of the cycle is happening. So, when a push button is pressed the loop begins and the servo will move back and forth once and de-attach. A led will start blinking once every second for 9h, then again the servo and the led will blink twice repeatedly for 5h; then servo again and the led 3x repeatedly for 5h and lastly servo again and detach. Here the loops ends and only restarts when the button is pressed again.
I could probably do with millis, but I can get may head around it yet. So, I've tried (in a probably dumb way) as follows (see code)
#include <Servo.h>
Servo myservo;
const int servoPin = 9;
const int buttonPin = 12;
const int ledPin = 13;void setup() {
myservo.attach(servoPin);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
myservo.write(180);
delay(1000);
myservo.detach();
}void loop() {
int buttonVal = digitalRead(buttonPin);
if(buttonVal == LOW) {
myservo.attach(servoPin);
myservo.write(30);
delay(575);
myservo.write(180);
delay(1500);
myservo.detach();
{
for (int x = 0; x < 6480000; x++) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(4000);
}
}
myservo.attach(servoPin);
myservo.write(30);
delay(575);
myservo.write(180);
delay(1500);
myservo.detach();
{
for (int x = 0; x < 3600000; x++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(4400);
}
}
myservo.attach(servoPin);
myservo.write(30);
delay(575);
myservo.write(180);
delay(1500);
myservo.detach();
{
for (int x = 0; x < 3600000; x++) {
digitalWrite(ledPin, HIGH);
delay(120);
digitalWrite(ledPin, LOW);
delay(120);
digitalWrite(ledPin, HIGH);
delay(120);
digitalWrite(ledPin, LOW);
delay(120);
digitalWrite(ledPin, HIGH);
delay(120);
digitalWrite(ledPin, LOW);
delay(4400);
}
}
myservo.attach(servoPin);
myservo.write(30);
delay(575);
myservo.write(180);
delay(1500);
myservo.detach();
}
delay(13);
}
The problem is:
I tried with low repetition numbers like up to 10 for the led and it works the whole thing, but when I put those big number to account for the 9 and 5 hours, the cycle starts fine but then the led just keeps blinking once repeatedly for more than 10h and it doesn't continue the rest of the loop. I guess the way I'm doing is no accurate (obviously) and logically a not classy way to work, but as a newby it was what I managed.
So, any ideas on how to work around that??
Many thanks!
PS: I calculated the repetitions considering that each cycle of the led is 5s, hence 6480000x5= 32 400 000ms = 9h
and 3600000x5= 18 000 000 = 5h