I have a simple program that I need to write. I say simple because I think I can do it by just modifying the blinker and the servo sweep sketches. I am a beginner so I have yet to write my own program. I have 4 tasks that I would like to incorporate in to the program. It should run on a continuous loop. I will be doing it through trial and error but I really want to have this done by tomorrow (for work) so I would appreciate it if anyone would help me by advising on some hurdles that I might encounter today/tonight. I'll be continuously checking this post as I work through my program.
My tasks:
1. Rotate a servo 180 degrees, after 10 minutes rotate the servo back (to original position) 180 degrees.
2. When the servo is in its original position, one small speaker will produce a heartbeat (I originally did this with a simple 555 timer, 2 resistor, capacitor circuit. It just pulsed the raw 6 volts from my battery, works perfectly.) After the servo moves 180 degrees (10 minutes later), a different speaker will begin producing the same beat. I'm thinking of just having (2) 555 timer+speaker circuits that will be turned on at the same time the PWM signal is sent to the servo, then turned off.
3. Every 4 minutes, 1 green LED turns off and another green LED turns on (a flip flop). At least one of the LEDs is always on.
4. (I may not put this in if I can't figure it out tonight). Task number 3 (I originally had this as task 2 but that was a typo) above will only run if it is dark (I have a little photocell) BUT a 2 position switch will force task number 2 to run, regardless of what the photocell is showing.
Some specs: Outputs: 2 555 timer circuits, 2 LEDs, 1 servo Inputs: 1 photocell Arduino board: SparkFun Pro Micro 5V/16MHz Power source: 6V, 10AH battery
Thank you all for your help! It is much appreciated. I know I will be able to get through this tonight!
No, I am not getting paid for this project. It's more for my own personal edification
I am working off the "sweep" example code. I got my delay to work properly after learning the basics (first eureka moment!).
#include <Servo.h>
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15ms for the servo to reach the position
}
{
delay(600000);
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15ms for the servo to reach the position
}
{
delay(600000);
}
}
My next hurdle is turning on my 555 timer circuit when the servo reaches position 0 then turning on the 2nd 555 timer circuit when the servo reaches 180 (and turning the other one off).
You want two timing loops, one 10 minutes (for the servo) and the other 4 minutes (for the green LEDs).
That's not possible using delay().
Read, understand and embrace the principle of the 'blink without delay' example in the IDE.
I also suggest that you look up 'Finite State Machine'.
I was bored so I put this together. Granted I am a bit of a novice this should be able to get you in the right direction. If you want to use timers you need to use the arduino internal clock "millis()" or other external clock.
#include <Servo.h>
Servo servo; //Initializes Servo
const int ledOne = 5; //sets LED 1 input to 5
const int ledTwo = 6; //sets LED 2 input to 6
const int spkrOne = 3; //Connect Speaker 1 to output 3
const int spkrTwo = 4; //Connect Speaker 2 to outpu 4
const int photoCell = 0; //Connect Photo Voltaic to analog 0
int photoValue = 512; //Adjust this to equal the value read from the cell when dark 512 = 2.5 volts
int currentLed = 1; //LED toggle
unsigned long currentTime;
long previousTimeS;
long previousTimeL;
long ledInterval = 240000; //4 minute LED timer
long servoInterval = 600000; //10 minute LED timer
void setup(){
servo.attach(9); //Initializes the servo
pinMode(spkrOne, OUTPUT); //Setting all outputs
pinMode(spkrTwo,OUTPUT);
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
digitalWrite(ledOne, HIGH); //Starts LED sequence with one on.
servo.write(0); //starts servo at 0 degrees
}
void loop(){
while(analogRead(photoCell) > photoValue){ //Suspends program while the photocell is in light
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, LOW);
digitalWrite(spkrOne, LOW);
digitalWrite(spkrOne, LOW);
previousTimeS = millis() - 600000;
previousTimeL = millis() - 240000;
}
currentTime = millis(); //sets current time based on how long the program has been running
if(currentTime - previousTimeL > ledInterval){ //Determines if enough time has lapsed to switch LEDS
if(currentLed == 1){ //Determines which state the LEDs are in and switches them
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
currentLed = 2;
}
else{
digitalWrite(ledOne, HIGH);
digitalWrite(ledTwo, LOW);
currentLed =1;
}
previousTimeL = currentTime;
}
if(currentTime - previousTimeS > servoInterval){
if(servo.read() == 0){
servo.write(179);
}
else{
servo.write(0);
}
previousTimeS = currentTime;
}
if(servo.read() == 0){
digitalWrite(spkrOne, HIGH);
digitalWrite(spkrTwo, LOW);
}
if(servo.read() == 179){
digitalWrite(spkrOne, HIGH);
digitalWrite(spkrTwo, LOW);
}
}
Thanks everyone for the assistance. The links you provided (blink w/o delay, finite state machine and severalthingsatatime) were certainly helpful. pguerra75, I tried out your code as well. It works pretty well, good job, I wouldn't consider you a novice if you did that on a whim! I had to modify it a bit and work through a few corrections and put a pulldown resistor on my photocell. Here is what I have:
#include <Servo.h>
Servo servo; //Initializes Servo
const int ledOne = 5; //sets LED 1 input to 5
const int ledTwo = 6; //sets LED 2 input to 6
const int spkrOne = 3; //Connect Speaker 1 to output 3
const int spkrTwo = 4; //Connect Speaker 2 to outpu 4
const int photoCell = 18; //Connect Photo Voltaic to analog 0
int photoValue = 512; //Adjust this to equal the value read from the cell when dark 512 = 2.5 volts
int currentLed = 1; //LED toggle
unsigned long currentTime;
long previousTimeS;
long previousTimeL;
long ledInterval = 240000; //4 minute LED timer
long servoInterval = 600000; //10 minute Servo timer
void setup(){
servo.attach(9); //Initializes the servo
pinMode(spkrOne, OUTPUT); //Setting all outputs
pinMode(spkrTwo,OUTPUT);
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
digitalWrite(ledOne, HIGH); //Starts LED sequence with one on.
servo.write(0); //starts servo at 0 degrees
}
void loop(){
while(analogRead(photoCell) > photoValue){ //Suspends program while the photocell is in light
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, LOW);
digitalWrite(spkrOne, LOW);
digitalWrite(spkrTwo, LOW);
previousTimeS = millis() - 600000;
previousTimeL = millis() - 240000;
}
currentTime = millis(); //sets current time based on how long the program has been running
if(currentTime - previousTimeL > ledInterval){ //Determines if enough time has lapsed to switch LEDS
if(currentLed == 1){ //Determines which state the LEDs are in and switches them
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
currentLed = 2;
}
else{
digitalWrite(ledOne, HIGH);
digitalWrite(ledTwo, LOW);
currentLed =1;
}
previousTimeL = currentTime;
}
if(currentTime - previousTimeS > servoInterval){
if(servo.read() == 0){
servo.write(180);
}
else{
servo.write(0);
}
previousTimeS = currentTime;
}
if(servo.read() == 0){
digitalWrite(spkrOne, HIGH);
digitalWrite(spkrTwo, LOW);
}
if(servo.read() == 180){
digitalWrite(spkrOne, LOW);
digitalWrite(spkrTwo, HIGH);
}
}
I would like the speakers to run regardless of what the photocell is reading. To do that I should delete this:
I still haven't quite grasped how to use the braces: { }
the {} define a scope
the { is the begin or opening
the } is the end or closure
it defines the visibility of variables declared inside the { }
and it defines the part that belongs to an IF or a FOR or a WHILE statement
and they are used in array declarations/initializations e.g. int fibo[8] = { 1, 1, 2 ,3 ,5 ,8 , 13. 21 };
using CTRL_T you can autoindent the code to get the {} right
placing the cursor on one { or } highlights the corresponding } or {