My class is working on a fun robot project utilizing kool-aide containers. I have changed my code from using "delays" to "millis". The robot has (7) LEDs and (3) SERVOS. The code works great, and I really like how I have control of the lighting cycles (I have pieced together a number of different codes to get to where I am at). The problem I am running into is that when I attempt to add the other (2) servos, the code creates errors. I am not proficient at coding by any stretch, so I tried to copy the code for the first servo, and simply paste and rename the servo (myservo - myservo1, etc).
My question is how do I go about inserting code for (2) more servos and be able to determing their range of movement. THANK YOU IN ADVANCE! Below is the current code, as well as a picture of the error code (including my attempt to insert the modified "copied" code.
unsigned long ms_from_start = 0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=1000;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval = 500;
unsigned long ms_previous_read_LED3 = 0;
unsigned long LED3_interval=3;
unsigned long ms_previous_read_LED4 = 0;
unsigned long LED4_interval=100;
unsigned long ms_previous_read_LED5 = 0;
unsigned long LED5_interval=3;
unsigned long ms_previous_read_LED6 = 0;
unsigned long LED6_interval=1500;
unsigned long ms_previous_read_LED7 = 0;
unsigned long LED7_interval=2000;
#define LED1 11
#define LED2 7
#define LED3 6
#define LED4 10
#define LED5 9
#define LED6 13
#define LED7 12
#include <Servo.h>
Servo myservo;
Servo myservo1;
Servo myservo2;
int LED1_state=0;
int LED2_state=0;
int LED3_state=0;
int LED4_state=0;
int LED5_state=0;
int LED6_state=0;
int LED7_state=0;
int myservo_pin = 3; // pin that controls the servo
int myservo1_pin = 4;
int myservo2_pin = 2;
int myservo_speed = 5; // how fast the servo moves, 1 = very fast, 10 = very slow
int myservo1_speed = 5;
int myservo2_speed = 5;
long myservo_movetime = 0; // next time in millis servo next moves
long myservo1_movetime = 0;
long myservo2_movetime = 0;
int myservo_gPos = 0; // target position to move towards
int myservo1_gPos = 0;
int myservo2_gPos = 0;
int myservo_cPos = 0; // current postion of servo
int myservo1_cPos = 0;
int myservo2_cPos = 0;
int pos = 0;
int cPos;
int gPos;
int tDelay = 5;
void setup (){
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
pinMode (LED6, OUTPUT);
pinMode (LED7, OUTPUT);
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo1.attach(4);
myservo2.attach(2);
Serial.println("setup complete : smooth servo movment without delay v1");
}
void loop() {
ms_from_start = millis();
Leds();
cPos = myservo.read();
if (cPos == 0) gPos = 180;
if (cPos == 180) gPos = 0;
if (cPos != gPos && millis() >= myservo_movetime) {
moveServo();
}
if (Serial.available() > 0) { GetCommand(); }
}
void moveServo() {
if (cPos < gPos) myservo.write(cPos+1);
if (cPos > gPos) myservo.write(cPos-1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
}
void GetCommand() {
int command = Serial.read() - '0';
int mVal = command;
if (mVal == 'x') {
tDelay = tDelay * 10;
} else {
tDelay = mVal;
}
Serial.print("Pauses changed to : "); Serial.print(tDelay); Serial.println(" mSeconds");
Serial.flush();
}
void Leds(){
if (ms_from_start-ms_previous_read_LED1> LED1_interval){
ms_previous_read_LED1=ms_from_start;
if (LED1_state==0) LED1_state=1; else LED1_state=0;
digitalWrite(LED1, LED1_state);
}
if (ms_from_start-ms_previous_read_LED2> LED2_interval){
ms_previous_read_LED2=ms_from_start;
if (LED2_state==0) LED2_state=1; else LED2_state=0;
digitalWrite(LED2, LED2_state);
}
if (ms_from_start-ms_previous_read_LED3> LED3_interval){
ms_previous_read_LED3=ms_from_start;
if (LED3_state==0) LED3_state=1; else LED3_state=0;
digitalWrite(LED3, LED3_state);
}
if (ms_from_start-ms_previous_read_LED4> LED4_interval){
ms_previous_read_LED4=ms_from_start;
if (LED4_state==0) LED4_state=1; else LED4_state=0;
digitalWrite(LED4, LED4_state);
}
if (ms_from_start-ms_previous_read_LED5> LED5_interval){
ms_previous_read_LED5=ms_from_start;
if (LED5_state==0) LED5_state=1; else LED5_state=0;
digitalWrite(LED5, LED5_state);
}
if (ms_from_start-ms_previous_read_LED6> LED6_interval){
ms_previous_read_LED6=ms_from_start;
if (LED6_state==0) LED6_state=1; else LED6_state=0;
digitalWrite(LED6, LED6_state);
}
if (ms_from_start-ms_previous_read_LED7> LED7_interval){
ms_previous_read_LED7=ms_from_start;
if (LED7_state==0) LED7_state=1; else LED7_state=0;
digitalWrite(LED7, LED7_state);
}
}
...but you forgot to share them.
Please remember to use code tags when posting code.
Using the IDE's auto-format tool before posting is useful too.
Hi, @jbycznski
Welcome to the forum.
Please read the post at the start of any forum , entitled "How to use this Forum".
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
This will help with advice on how to present your code and problems.
Thanks.. Tom...
My class is working on a fun robot project utilizing kool-aide containers. I have changed my code from using "delays" to "millis". The robot has (7) LEDs and (3) SERVOS. The code works great, and I really like how I have control of the lighting cycles (I have pieced together a number of different codes to get to where I am at). The problem I am running into is that when I attempt to add the other (2) servos, the code creates errors. I am not proficient at coding by any stretch, so I tried to copy the code for the first servo, and simply paste and rename the servo (myservo - myservo1, etc).
My question is how do I go about inserting code for (2) more servos and be able to determing their range of movement. THANK YOU IN ADVANCE! Below is the current code, as well as a picture of the error code (including my attempt to insert the modified "copied" code.
unsigned long ms_from_start = 0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=1000;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval = 500;
unsigned long ms_previous_read_LED3 = 0;
unsigned long LED3_interval=3;
unsigned long ms_previous_read_LED4 = 0;
unsigned long LED4_interval=100;
unsigned long ms_previous_read_LED5 = 0;
unsigned long LED5_interval=3;
unsigned long ms_previous_read_LED6 = 0;
unsigned long LED6_interval=1500;
unsigned long ms_previous_read_LED7 = 0;
unsigned long LED7_interval=2000;
#define LED1 11
#define LED2 7
#define LED3 6
#define LED4 10
#define LED5 9
#define LED6 13
#define LED7 12
#include <Servo.h>
Servo myservo;
Servo myservo1;
Servo myservo2;
int LED1_state=0;
int LED2_state=0;
int LED3_state=0;
int LED4_state=0;
int LED5_state=0;
int LED6_state=0;
int LED7_state=0;
int myservo_pin = 3; // pin that controls the servo
int myservo1_pin = 4;
int myservo2_pin = 2;
int myservo_speed = 5; // how fast the servo moves, 1 = very fast, 10 = very slow
int myservo1_speed = 5;
int myservo2_speed = 5;
long myservo_movetime = 0; // next time in millis servo next moves
long myservo1_movetime = 0;
long myservo2_movetime = 0;
int myservo_gPos = 0; // target position to move towards
int myservo1_gPos = 0;
int myservo2_gPos = 0;
int myservo_cPos = 0; // current postion of servo
int myservo1_cPos = 0;
int myservo2_cPos = 0;
int pos = 0;
int cPos;
int gPos;
int tDelay = 5;
void setup (){
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
pinMode (LED6, OUTPUT);
pinMode (LED7, OUTPUT);
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo1.attach(4);
myservo2.attach(2);
Serial.println("setup complete : smooth servo movment without delay v1");
}
void loop() {
ms_from_start = millis();
Leds();
cPos = myservo.read();
if (cPos == 0) gPos = 180;
if (cPos == 180) gPos = 0;
if (cPos != gPos && millis() >= myservo_movetime) {
moveServo();
}
if (Serial.available() > 0) { GetCommand(); }
}
void moveServo() {
if (cPos < gPos) myservo.write(cPos+1);
if (cPos > gPos) myservo.write(cPos-1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
}
void GetCommand() {
int command = Serial.read() - '0';
int mVal = command;
if (mVal == 'x') {
tDelay = tDelay * 10;
} else {
tDelay = mVal;
}
Serial.print("Pauses changed to : "); Serial.print(tDelay); Serial.println(" mSeconds");
Serial.flush();
}
void Leds(){
if (ms_from_start-ms_previous_read_LED1> LED1_interval){
ms_previous_read_LED1=ms_from_start;
if (LED1_state==0) LED1_state=1; else LED1_state=0;
digitalWrite(LED1, LED1_state);
}
if (ms_from_start-ms_previous_read_LED2> LED2_interval){
ms_previous_read_LED2=ms_from_start;
if (LED2_state==0) LED2_state=1; else LED2_state=0;
digitalWrite(LED2, LED2_state);
}
if (ms_from_start-ms_previous_read_LED3> LED3_interval){
ms_previous_read_LED3=ms_from_start;
if (LED3_state==0) LED3_state=1; else LED3_state=0;
digitalWrite(LED3, LED3_state);
}
if (ms_from_start-ms_previous_read_LED4> LED4_interval){
ms_previous_read_LED4=ms_from_start;
if (LED4_state==0) LED4_state=1; else LED4_state=0;
digitalWrite(LED4, LED4_state);
}
if (ms_from_start-ms_previous_read_LED5> LED5_interval){
ms_previous_read_LED5=ms_from_start;
if (LED5_state==0) LED5_state=1; else LED5_state=0;
digitalWrite(LED5, LED5_state);
}
if (ms_from_start-ms_previous_read_LED6> LED6_interval){
ms_previous_read_LED6=ms_from_start;
if (LED6_state==0) LED6_state=1; else LED6_state=0;
digitalWrite(LED6, LED6_state);
}
if (ms_from_start-ms_previous_read_LED7> LED7_interval){
ms_previous_read_LED7=ms_from_start;
if (LED7_state==0) LED7_state=1; else LED7_state=0;
digitalWrite(LED7, LED7_state);
}
}
I don't get that error. The code you posted compiles for me.
Sketch uses 4450 bytes (13%) of program storage space. Maximum is 32768 bytes. Global variables use 377 bytes (18%) of dynamic memory, leaving 1671 bytes for local variables. Maximum is 2048 bytes.
It does upload fine in its current state. But when I try to add the code for the other (2) servos, I begin getting the error message.
Please do not cross post. Reported to moderator.
I don't even know what "cross-post" means.....
Which code? The code that is currently running my lights and the (1) servo was posted in the original post. I cannot figure out how to code the other two.
It means your other thread that already had several people trying to help. Don't do that. It just causes confusion. Hopefully a mod will assemble to a single thread and delete the extra.
OK - Thank you. I recieved a message about the format of my original post, and tried to change. It made the "subject line" on the forum page light grey instead of black like the others, so I thought it had created a problem. So I tried to delete and edit.
You had an attempt that resulted in the error. Post that... get help fixing that. "LEDs not declared" probably means your missing a brace. Try using autoformat from the tools menu.
pert
May 20, 2022, 5:09pm
15
I have merged your cross-posts @jbycznski .
Cross-posting is against the Arduino forum rules . The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
In the future, please just add additional information to your existing topic as the discussion progresses.
Thanks in advance for your cooperation.
Per
I did post my attempt at adding the additional code (see screenshot of my attempt).
With just the (1) servo, everything works great, it is when I try to add code for the other (2) servos, error messages happen.
THANK YOU.
That is not the same as posting your actual code. I cannot copy your screenshot into my IDE and replicate your error.
Complete waste of time - we can't edit pictures or compile them
Here is my attempt at adding the code, and screenshot of error message
unsigned long ms_from_start = 0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=1000;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval = 500;
unsigned long ms_previous_read_LED3 = 0;
unsigned long LED3_interval=3;
unsigned long ms_previous_read_LED4 = 0;
unsigned long LED4_interval=100;
unsigned long ms_previous_read_LED5 = 0;
unsigned long LED5_interval=3;
unsigned long ms_previous_read_LED6 = 0;
unsigned long LED6_interval=1500;
unsigned long ms_previous_read_LED7 = 0;
unsigned long LED7_interval=2000;
#define LED1 11
#define LED2 7
#define LED3 6
#define LED4 10
#define LED5 9
#define LED6 13
#define LED7 12
#include <Servo.h>
Servo myservo;
Servo myservo1;
Servo myservo2;
int LED1_state=0;
int LED2_state=0;
int LED3_state=0;
int LED4_state=0;
int LED5_state=0;
int LED6_state=0;
int LED7_state=0;
int myservo_pin = 3; // pin that controls the servo
int myservo1_pin = 4;
int myservo2_pin = 2;
int myservo_speed = 5; // how fast the servo moves, 1 = very fast, 10 = very slow
int myservo1_speed = 5;
int myservo2_speed = 5;
long myservo_movetime = 0; // next time in millis servo next moves
long myservo1_movetime = 0;
long myservo2_movetime = 0;
int myservo_gPos = 0; // target position to move towards
int myservo1_gPos = 0;
int myservo2_gPos = 0;
int myservo_cPos = 0; // current postion of servo
int myservo1_cPos = 0;
int myservo2_cPos = 0;
int pos = 0;
int cPos;
int gPos;
int tDelay = 5;
void setup (){
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
pinMode (LED6, OUTPUT);
pinMode (LED7, OUTPUT);
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo1.attach(4);
myservo2.attach(2);
Serial.println("setup complete : smooth servo movment without delay v1");
}
void loop() {
ms_from_start = millis();
Leds();
cPos = myservo.read();
if (cPos == 0) gPos = 180;
if (cPos == 180) gPos = 0;
if (cPos != gPos && millis() >= myservo_movetime) {
moveServo();
};
cPos = myservo1.read();
if (cPos == 0) gPos = 90;
if (cPos == 90) gPos = 0;
if (cPos != gPos && millis() >= myservo1_movetime) {
moveServo();
}
if (Serial.available() > 0) { GetCommand(); }
}
void moveServo() {
if (cPos < gPos) myservo.write(cPos+1);
if (cPos > gPos) myservo.write(cPos-1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
}
if (cPos < gPos) myservo1.write(cPos+1);
if (cPos > gPos) myservo1.write(cPos-1);
//if (cPos == gPos) // nothing
myservo1_movetime = millis() + tDelay;
}
void GetCommand() {
int command = Serial.read() - '0';
int mVal = command;
if (mVal == 'x') {
tDelay = tDelay * 10;
} else {
tDelay = mVal;
}
Serial.print("Pauses changed to : "); Serial.print(tDelay); Serial.println(" mSeconds");
Serial.flush();
}
void Leds(){
if (ms_from_start-ms_previous_read_LED1> LED1_interval){
ms_previous_read_LED1=ms_from_start;
if (LED1_state==0) LED1_state=1; else LED1_state=0;
digitalWrite(LED1, LED1_state);
}
if (ms_from_start-ms_previous_read_LED2> LED2_interval){
ms_previous_read_LED2=ms_from_start;
if (LED2_state==0) LED2_state=1; else LED2_state=0;
digitalWrite(LED2, LED2_state);
}
if (ms_from_start-ms_previous_read_LED3> LED3_interval){
ms_previous_read_LED3=ms_from_start;
if (LED3_state==0) LED3_state=1; else LED3_state=0;
digitalWrite(LED3, LED3_state);
}
if (ms_from_start-ms_previous_read_LED4> LED4_interval){
ms_previous_read_LED4=ms_from_start;
if (LED4_state==0) LED4_state=1; else LED4_state=0;
digitalWrite(LED4, LED4_state);
}
if (ms_from_start-ms_previous_read_LED5> LED5_interval){
ms_previous_read_LED5=ms_from_start;
if (LED5_state==0) LED5_state=1; else LED5_state=0;
digitalWrite(LED5, LED5_state);
}
if (ms_from_start-ms_previous_read_LED6> LED6_interval){
ms_previous_read_LED6=ms_from_start;
if (LED6_state==0) LED6_state=1; else LED6_state=0;
digitalWrite(LED6, LED6_state);
}
if (ms_from_start-ms_previous_read_LED7> LED7_interval){
ms_previous_read_LED7=ms_from_start;
if (LED7_state==0) LED7_state=1; else LED7_state=0;
digitalWrite(LED7, LED7_state);
}
}
void moveServo() {
if (cPos < gPos) myservo.write(cPos + 1);
if (cPos > gPos) myservo.write(cPos - 1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
}// comment out this brace
if (cPos < gPos) myservo1.write(cPos + 1);
if (cPos > gPos) myservo1.write(cPos - 1);
//if (cPos == gPos) // nothing
myservo1_movetime = millis() + tDelay;
}
or perhaps you mean
void moveServo() {
if (cPos < gPos) myservo.write(cPos + 1);
if (cPos > gPos) myservo.write(cPos - 1);
//if (cPos == gPos) // nothing
myservo_movetime = millis() + tDelay;
}
void moveServo1() {
if (cPos < gPos) myservo1.write(cPos + 1);
if (cPos > gPos) myservo1.write(cPos - 1);
//if (cPos == gPos) // nothing
myservo1_movetime = millis() + tDelay;
}
See how fast that could have been if you'd started with this?
PS: Welcome to the forum and good luck and have fun with your kool-aide robot.