Guys, I am new as are most of the posters starting topics here.
I am a little stuck, and for some reason my brains seem to have given up on me for this issue.
I have a variable (HM) that is the hours+min pulled from a RTC and stored as a variable.
What i would like to be able to do is evaluate this variable (HM) and if it is between say 1 and 800, have the program stop and wait for the variable to increase to 801. between 801 and 1730 have the program do loop A. Lastly if it is between 1730 and 2400 have the program do loop B.
I can't figure out how to get the program to deal with three possible choices based on the evaluation of the variable. I did get all of my hardware working, and I have loop A and loop B working as independent sketches, but i can't seem to bring it all together without breaking everything.
Whats my best bet? I assume it is an if and else statement but that would only let me account for two of the ranges, right? GOTO is evil from what everyone said so I am trying to stick with loops. Can anyone point me to a direction with a good example I could learn from?
/* Sweep
simulated walking
*/
#include <Servo.h> // servo lib
#include <Wire.h> // wire lib
#include <Time.h> // time lib
#include <DS1307RTC.h> // clock lib
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int count = 0; // counter of cycles
long randdelay; // delay on movement
long randomnumber; // delay between cycles
long t;
//int HM; // hours and min added // remarked out
void setup()
{
myservo.attach(9); // attaches servro
Serial.begin(9600); // open serial
while (!Serial) ;
setSyncProvider(RTC.get); // sets RTC clock reader
}
void loop() {
int HM;
time_t t= now(); // remarked out
HM = hour(t)*100 + minute(t); //Make time into a real number
Serial.print(HM);
if (HM <= 1623){//evaluate time
for(pos = 90; pos <= 100; pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos); // move servo
randdelay = random(5,50); // roll a random number
delay(randdelay); // wait that number
{if(pos==100) count+=1; // increment the counter when reaching peak
}
{if(count==20) // determin if counter is 20
randomnumber = random(100,10000); // roll large random number
}
{if(count==20) // determine if counter is 20 check
delay (randomnumber); // wait large number
}
{if(count==20) //check counter again
count=0; // reset to 0
}
}
for(pos = 100; pos>=90; pos-=1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos); // move servo
delay(randdelay); // wait short delay
}
} // end of loop
}
That is what i have now. I am going to try to shove this into the framework MorganS gave me, thank you both. Its a small amount of math so it runs a billion times it shouldn't kill everything i don't think. I am just happy I got it to stop with an IF statement. Ill keep working on it tonight and post back.
PS If i messed up anything really bad let me know.
Okay guys. I am getting there. Sorry about the bracket thing, as I said I am a noob at this, and so somewhere along editing and re-uploading my code 100s of times, the brackets and indenting fell apart. I do intend to fix it though when I am getting closer to the working result.
I am very nearly there.
/* Sweep
simulated walking
*/
#include <Servo.h> // servo lib
#include <Wire.h> // wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int count = 0; // counter of cycles
long t;
long fastrandstepcount = 1;
long fastranddelay;
long workrandstepcount = 1;
long workranddelay;
void setup()
{
myservo.attach(9); // attaches servro
Serial.begin(9600); // open serial
Wire.begin();
randomSeed(analogRead(0));
}
void loop() {
int HM;
DateTime now = rtc.now();
//myservo.detach();
HM = (now.hour())*100 + (now.minute()); //Make time into a real number
if (HM >= 1730 && HM <= 2359){
{
myservo.attach(9);
if(fastrandstepcount == 1) {
fastrandstepcount = random(10,75);
Serial.print("running day fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
workrandstepcount = 1;
workrandstepcount = 1;
}
}
for(pos = 90; pos <= 100; pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos); // move serv
delay(15); // wait that number
{
if(pos==100) count+=1; // increment the counter when reaching peak
}
{ if(count==fastrandstepcount) { // determine if counter is 20 check
fastranddelay= random(2000,150000);
Serial.print("FRD ");
Serial.print (fastranddelay);
Serial.println();
delay(fastranddelay); // wait large number
}
}
{
if(count==fastrandstepcount)
{
count=0; // reset to 0
fastrandstepcount=1;
}
}
}
for(pos = 100; pos>=90; pos-=1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos); // move servo
delay(15); // wait short delay
}
}
else if (HM >= 800 && HM <= 1730)
{
{
if(workrandstepcount == 1) {
myservo.attach(9);
workrandstepcount = random(10,75);
Serial.print("running work fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
fastrandstepcount = 1;
fastranddelay = 1;
}
}
for(pos = 90; pos <= 100; pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos); // move serv
delay(15); // wait that number
{
if(pos==100) count+=1; // increment the counter when reaching peak
}
{ if(count==workrandstepcount) { // determine if counter is 20 check
fastranddelay= random(2000,150000);
Serial.print("WRD ");
Serial.print (workranddelay);
Serial.println();
delay(workranddelay); // wait large number
}
}
{
if(count==workrandstepcount)
{
count=0; // reset to 0
workrandstepcount=1;
}
}
}
for(pos = 100; pos>=90; pos-=1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos); // move servo
delay(15); // wait short delay
}
}
}
I need some advice though. I structured this out as MorganS had stated, and it is working. What is happening though is that when checking the time thousands of times a second when the HM variable doesn't fit in one of my ranges, I think it is causing some issue with the servo. When it is idle (HM = 0-800) the servo clicks back and forth a degree ever 1/2second or so. It sounds like a coffee maker. I believe this is caused by the amount of interrupts and the amount of code that it is running repetitively. This only occurs when the it loops the time over and over with no other function. I found if i detach the servo with script it no longer clicks.
What i am wondering is:
Is the servo click indeed do to the insane number of times it is checking the time when HM=0-800?
Is detaching and re attaching the servo the right way to solve this? Is there harm to it? I realize that when HM fits a range I would be detaching and reattaching this every second or so.
Is allowing the time to check thousands of times a second going to cause me issues later?
I do intend to fix it though when I am getting closer to the working result.
Sorry, that is the wrong attitude. Fix the mess NOW, so YOU have an easier time seeing the structure and the flow. It takes next to no time to use Tools + Auto Format (Ctrl-t).
As it is, it is nearly impossible to tell the intent of your code.
I believe this is caused by the amount of interrupts and the amount of code that it is running repetitively.
It is more likely because you keep attaching and detaching the servo. Leave the servo attached.
Sorry, I was trying to manually clean this up. I didn't realize that auto cleanup was in tools. I tried "control T" as suggested before didn't notice it was "command T" on a mac since I was not clear what I was looking for in the menu.
Let me clarify a little my original code looked like this:
/* Sweep
simulated walking
*/
#include <Servo.h> // servo lib
#include <Wire.h> // wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
Servo myservo;
// create servo object to control a servo
int pos = 0;
// variable to store the servo position
int count = 0;
// counter of cycles
long t;
long fastrandstepcount = 1;
long fastranddelay;
long workrandstepcount = 1;
long workranddelay;
void setup()
{
myservo.attach(9);
//attaches servro
Serial.begin(9600);
// open serial
Wire.begin();
randomSeed(analogRead(0));
}
void loop() {
int HM;
DateTime now = rtc.now();
HM = (now.hour()) * 100 + (now.minute());
//Make time into a real number
if (HM >= 0 && HM <= 2359) {
{
if (fastrandstepcount == 1) {
fastrandstepcount = random(10, 75);
Serial.print("running day fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
workrandstepcount = 1;
workrandstepcount = 1;
}
}
for (pos = 90;
pos <= 100;
pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos);
// move serv
delay(15);
// wait that number
{
if (pos == 100) count += 1;
// increment the counter when reaching peak
}
{
if (count == fastrandstepcount) {
// determine if counter is 20 check
fastranddelay = random(2000, 150000);
Serial.print("FRD ");
Serial.print (fastranddelay);
Serial.println();
delay(fastranddelay);
// wait large number
}
}
{
if (count == fastrandstepcount)
{
count = 0;
// reset to 0
fastrandstepcount = 1;
}
}
}
for (pos = 100;
pos >= 90;
pos -= 1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos);
// move servo
delay(15);
// wait short delay
}
}
else if (HM >= 800 && HM <= 1730)
{
{
if (workrandstepcount == 1) {
workrandstepcount = random(10, 75);
Serial.print("running work fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
fastrandstepcount = 1;
fastranddelay = 1;
}
}
for (pos = 90;
pos <= 100;
pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos);
// move serv
delay(15);
// wait that number
{
if (pos == 100) count += 1;
// increment the counter when reaching peak
}
{
if (count == workrandstepcount) {
// determine if counter is 20 check
fastranddelay = random(2000, 150000);
Serial.print("WRD ");
Serial.print (workranddelay);
Serial.println();
delay(workranddelay);
// wait large number
}
}
{
if (count == workrandstepcount)
{
count = 0;
// reset to 0
workrandstepcount = 1;
}
}
}
for (pos = 100;
pos >= 90;
pos -= 1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos);
// move servo
delay(15);
// wait short delay
}
}
}
I was attaching the servo once in the setup loop and everything was fine and functioning until it hit 0-800 and that is when it started clicking. After messing with it it is only clicking in that 0-800 range and only when attached.
So I made the code look like:
/* Sweep
simulated walking
*/
#include <Servo.h> // servo lib
#include <Wire.h> // wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
Servo myservo;
// create servo object to control a servo
int pos = 0;
// variable to store the servo position
int count = 0;
// counter of cycles
long t;
long fastrandstepcount = 1;
long fastranddelay;
long workrandstepcount = 1;
long workranddelay;
void setup()
{
myservo.attach(9);
//attaches servro
Serial.begin(9600);
// open serial
Wire.begin();
randomSeed(analogRead(0));
}
void loop() {
int HM;
DateTime now = rtc.now();
// myservo.detach();
HM = (now.hour()) * 100 + (now.minute());
//Make time into a real number
if (HM >= 0 && HM <= 2359) {
{
// myservo.attach(9);
if (fastrandstepcount == 1) {
fastrandstepcount = random(10, 75);
Serial.print("running day fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
workrandstepcount = 1;
workrandstepcount = 1;
}
}
for (pos = 90;
pos <= 100;
pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos);
// move serv
delay(15);
// wait that number
{
if (pos == 100) count += 1;
// increment the counter when reaching peak
}
{
if (count == fastrandstepcount) {
// determine if counter is 20 check
fastranddelay = random(2000, 150000);
Serial.print("FRD ");
Serial.print (fastranddelay);
Serial.println();
delay(fastranddelay);
// wait large number
}
}
{
if (count == fastrandstepcount)
{
count = 0;
// reset to 0
fastrandstepcount = 1;
}
}
}
for (pos = 100;
pos >= 90;
pos -= 1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos);
// move servo
delay(15);
// wait short delay
}
}
else if (HM >= 800 && HM <= 1730)
{
{
if (workrandstepcount == 1) {
myservo.attach(9);
workrandstepcount = random(10, 75);
Serial.print("running work fucntion ");
Serial.println();
Serial.print("Walking to ");
Serial.print(fastrandstepcount);
Serial.println();
fastrandstepcount = 1;
fastranddelay = 1;
}
}
for (pos = 90;
pos <= 100;
pos += 1) // goes from 90 degrees to 100 degrees
{
myservo.write(pos);
// move serv
delay(15);
// wait that number
{
if (pos == 100) count += 1;
// increment the counter when reaching peak
}
{
if (count == workrandstepcount) {
// determine if counter is 20 check
fastranddelay = random(2000, 150000);
Serial.print("WRD ");
Serial.print (workranddelay);
Serial.println();
delay(workranddelay);
// wait large number
}
}
{
if (count == workrandstepcount)
{
count = 0;
// reset to 0
workrandstepcount = 1;
}
}
}
for (pos = 100;
pos >= 90;
pos -= 1) // goes from 100 degrees to 90 degrees
{
myservo.write(pos);
// move servo
delay(15);
// wait short delay
}
}
}
This is detaching the servo and then reattaching it, as it is the best way I can see to make the servo not active in the 0-800 range for the HM variable. When running it with the detach/attach stuff it does not click.
I know it is by far best practice to attach once per loop, is there any harm in attaching and detaching as it seems to solve the clicking issue? Or do you guys have any suggestion for a better means of troubleshooting this?