Fun Robot Project - Code Issue

This is not recommended code. I think you need to learn to code. That is step one. You are supposed to be learning in this process, seeing how the code is changed and why and where, not just copy paste.

I would agree 100% that I should learn code, and I am in the very early stages of doing just that. In my profession, when someone reaches out for help with their molds or figure fabrication, I dive in to help with the problem, without judgement of where they are at in the process, and regardless of where they are at in their level of understanding. When it comes to my problem, my "copying and pasting" has worked from the start, it has been the "suggested", "recommended", "here you go" code that has failed every time. It took me a little over a week to find code I thought would work, watched videos to better understand, utilized the Arduino library to help make sense of what I was doing, and trial and errored my problem. It appears that the problem is far more complexed than I thought, because even those in the know are having problems.

I don't have full visibility of your project. I can only go on what you say. I can write code, and make sure it compiles, but I don't have a kool-aide containers, leds and servos. I don't know what LED1 means from LED6 or which servo is the turret or what the other two servos do. You brought to the discussion a fairly thought out code, and I'm willing to help, but I just want to be clear going forward, this isn't recommended code, but your code with recommended changes. At the end of the day, it's your code. If there is a change I made you can't own, or you don't understand, I encourage you to ask questions. I also encourage you to be detailed in your responses. How has the lighting sequence changed from the code that was running before?

In the folder included, there are (2) videos. One has the lighting sequence I want and which works perfectly, the other video is what happens to the lighting sequence when the servo code is added (which shows the problems with the lighting sequence, and poor servo movement). The servos work just fine when tested by themselves.

This is the second of these "kool-aide" container robots. The first robots worked off of code that used "delays". Through my research, and conversations with others, it was determined that I could get much smoother movements, and greater control using "millis" and not delays. So, the code I am using now creates a awesome sequence for the LEDs, but I have been unable to incorporate (3) servos into the code without issues.

In my code, LED1, LED2, LED3 etc, refers to LED lights, which there are (7) of them. Again, the LED code works perfect. I need help incorporating (3) servos. Suggestions that have been made so far - do not work. The lighting changes (see videos), and the servos do not work. I can adjust the (degree) of the movements of the servos once I get a code that fits with what I currently have. So servo code that will rotate each is what I am seeking help for.

Here is the folder with lighting videos: Lighting Videos

Also, here is my original code I posted to start this thread (again, there is nothing wrong with this. The lighting works perfectly).

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
 



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;


void setup (){
  pinMode (LED1, OUTPUT);
   pinMode (LED2, OUTPUT);
    pinMode (LED3, OUTPUT);
     pinMode (LED4, OUTPUT);
      pinMode (LED5, OUTPUT);
       pinMode (LED6, OUTPUT);
        pinMode (LED7, OUTPUT);
         
} 
void loop() {
  ms_from_start= millis();

  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);
        }
}
 
  
 
 

Perhaps the servo library you are using contains "blocking" code that prevents the LEDs timing while the servos are in motion. Did you have any problem with the LED lighting with just the one servo?

I currently do not have a servo code that works, nor do I have a servo library. I do not know what a "blocking" code is. The LED sequence code that I presented works perfectly. Every attempt at adding servo code creates error messages (as shown in this thread).

When a servo has been added - the lighting is affected.

Reaching out to this forum was to get help with the addition of servo code. My attempts have all failed, and have some of the suggestions so far.

Alone, the servos work just fine. When the servo and LED code are combined, then things do not work. I looked at the tutorial on "several things at one time", and tried to incorporate those ideas - no luck.

Thank you for helping with this problem.

I took a look at the servo library, and while it does not contain blocking code, servo->read() does contain division, which can create problems with your timing. I have moved servo->read() for each servo to setup, so upon power up only you will read the position one time, and then make your incremental adjustments from there...

The way you are using servo.write() is to move the servos 1 degree every tDelay milliseconds. This allows you to adjust the speed of rotation, but if you don't need to do this, moving the servo from 0-180 in a single move or 0-90 and 90-180 to prevent backward movement, would also insure you return to the LED conditionals more quickly.

I have tried minor speed improvements with the following code. Let me know if it make any improvement on your problem.

#include <Servo.h>
const unsigned long LED1_interval = 1000,// intervals for millis()
                    LED2_interval = 500,
                    LED3_interval = 3,
                    LED4_interval = 100,
                    LED5_interval = 3,
                    LED6_interval = 1500,
                    LED7_interval = 2000;

unsigned long ms_from_start,// timestamps for millis()
         LED1_Timestamp,
         LED2_Timestamp,
         LED3_Timestamp,
         LED4_Timestamp,
         LED5_Timestamp,
         LED6_Timestamp,
         LED7_Timestamp,
         myservo_Timestamp,
         myservo1_Timestamp,
         myservo2_Timestamp,
         tDelay = 5;// variable interval for millis()

const int LED1_Pin = 11,// Arduino Pin Numbers
          LED2_Pin = 7,
          LED3_Pin = 6,
          LED4_Pin = 10,
          LED5_Pin = 9,
          LED6_Pin = 13,
          LED7_Pin = 12,
          myservo_pin = 3,
          myservo1_pin = 4,
          myservo2_pin = 2;

Servo myservo;
Servo myservo1;
Servo myservo2;

int LED1_state,
    LED2_state,
    LED3_state,
    LED4_state,
    LED5_state,
    LED6_state,
    LED7_state,
    myservo_gPos, // target position to move towards
    myservo1_gPos,
    myservo2_gPos,
    myservo_cPos, // current postion of servo
    myservo1_cPos,
    myservo2_cPos;

void setup () {
  pinMode (LED1_Pin, OUTPUT);
  pinMode (LED2_Pin, OUTPUT);
  pinMode (LED3_Pin, OUTPUT);
  pinMode (LED4_Pin, OUTPUT);
  pinMode (LED5_Pin, OUTPUT);
  pinMode (LED6_Pin, OUTPUT);
  pinMode (LED7_Pin, OUTPUT);
  Serial.begin(9600);
  myservo.attach(myservo_pin);
  myservo1.attach(myservo1_pin);
  myservo2.attach(myservo2_pin);
  Serial.println("setup complete : smooth servo movment without delay v1");
  myservo_cPos = myservo.read();
  if (myservo_cPos <= 0) myservo_gPos = 180;
  myservo1_cPos = myservo1.read();
  if (myservo1_cPos <= 0) myservo1_gPos = 90;
  myservo2_cPos = myservo2.read();
  if (myservo2_cPos <= 0) myservo2_gPos = 90;
}

void loop() {
  ms_from_start = millis();
  if (ms_from_start - LED1_Timestamp >= LED1_interval) {
    LED1_Timestamp = ms_from_start;
    LED1_state ^= 1;
    digitalWrite(LED1_Pin, LED1_state);
  }
  if (ms_from_start - LED2_Timestamp >= LED2_interval) {
    LED2_Timestamp = ms_from_start;
    LED2_state ^= 1;
    digitalWrite(LED2_Pin, LED2_state);
  }
  if (ms_from_start - LED3_Timestamp >= LED3_interval) {
    LED3_Timestamp = ms_from_start;
    LED3_state ^= 1;
    digitalWrite(LED3_Pin, LED3_state);
  }
  if (ms_from_start - LED4_Timestamp >= LED4_interval) {
    LED4_Timestamp = ms_from_start;
    LED4_state ^= 1;
    digitalWrite(LED4_Pin, LED4_state);
  }
  if (ms_from_start - LED5_Timestamp >= LED5_interval) {
    LED5_Timestamp = ms_from_start;
    LED5_state ^= 1;
    digitalWrite(LED5_Pin, LED5_state);
  }
  if (ms_from_start - LED6_Timestamp >= LED6_interval) {
    LED6_Timestamp = ms_from_start;
    LED6_state ^= 1;
    digitalWrite(LED6_Pin, LED6_state);
  }
  if (ms_from_start - LED7_Timestamp >= LED7_interval) {
    LED7_Timestamp = ms_from_start;
    LED7_state ^= 1;
    digitalWrite(LED7_Pin, LED7_state);
  }
  if (ms_from_start - myservo_Timestamp >= tDelay) {
    if (myservo_cPos <= 0) myservo_gPos = 180;
    else if (myservo_cPos >= 180) myservo_gPos = 0;
    (myservo_cPos < myservo_gPos) ? myservo_cPos++ : myservo_cPos--;
    myservo.write(myservo_cPos);
    myservo_Timestamp = ms_from_start;
  }
  if (ms_from_start - myservo1_Timestamp >= tDelay) {
    if (myservo1_cPos <= 0) myservo1_gPos = 90;
    else if (myservo1_cPos >= 90) myservo1_gPos = 0;
    (myservo1_cPos < myservo1_gPos) ? myservo1_cPos++ : myservo1_cPos--;
    myservo1.write(myservo1_cPos);
    myservo1_Timestamp = ms_from_start;
  }
  if (ms_from_start - myservo2_Timestamp >= tDelay) {
    if (myservo2_cPos <= 0) myservo2_gPos = 90;
    else if (myservo2_cPos >= 90) myservo2_gPos = 0;
    (myservo2_cPos < myservo2_gPos) ? myservo2_cPos++ : myservo2_cPos--;
    myservo.write(myservo2_cPos);
    myservo2_Timestamp = ms_from_start;
  }
  if (Serial.available() > 0) {
    int command = Serial.read();
    if (command == 'x') tDelay = tDelay * 10;
    else if (command == 'y' && tDelay >= 10) tDelay = tDelay / 10;
    else tDelay = command - '0';
    Serial.print("Pauses changed to : "); Serial.print(tDelay); Serial.println(" mSeconds");
    Serial.flush();
  }
}

Good Morning,

I uploaded the code into the simulator and the robot and it still does not work correctly. When loaded onto the robot's arduino, not all of the lights turn on, the others "flicker" and only (2) of the (3) servos move. I will continue to search for solutions.

Thank you for your continued help.

Hi,
Can you please post a schematic of your project?
Please do not use Fritzy, a hand drawn image will be fine.
Include ALL power supplies, component names and pin labels.

What is your power supply?

Have you written your code in stages and tested it in real life?
Have you written code JUST for each servo to prove you have control?
Have you written code JUST for each LED to prove you have control?

Do you have a DMM?

Tom... :smiley: :+1: :coffee: :australia:

Good morning.

Attached is a picture of the robot layout, and the pin location for the various LEDs and Servos.

The power is supplied when the robot is either plugged into a computer, or using charging cable.

The code for the (LEDs) works 100% of the time. There are no issues with the lighting code.

Every time servo code has been incorporated (without changing the LED code), the lighting changes, and the servos do not work properly.

ROBOT PICTURE

Here is a video of the first robot. This was built the same way as the second one. The difference here is that the coding used for this robot uses "delays", which we wanted to avoid with the second.
1st robot video

Hi,
@jbycznski picture;

Now I hope a schematic to follow.
What is your power supply?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Here is how to robot is set up. This is the simulator view (which is how I test the code before uploading to Arduino). This has been accurate 100% of the time.
Screen Shot 2022-05-27 at 7.49.56 AM

As mentioned before, I am using the 5V pin on the Arduino for power. It is either plugged into a computer, or through a charging cord. This works for the 1st generation robot described above.

Hi,
Thanks for the circuit, but it needs to be a proper schematic.
Sorry but those Fritzy pictures are very confusing when you cross coloured wires and no components are labeled or pins named.

Can you please post a hand drawn circuit, with component names and pin labels.
You can draw a better layout and route your wires better than Fritzy can.

Where are your current limit resistors for each of the LEDs, you need them because at the moment you will be drawing excessive current from each output pin of the UNO.
This could possibly damage your controller beyond repair.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

There are no resistors currently.

Hi,
Thanks for the circuit, much easier to read, yes you will have to add resistors to all those LEDs.
If you are using the hardware without resistors, then stop and add them, you are overloading the outputs of the UNO.

Not sure if you screen captured it or EXPORTED from your CAD, the resolution is a bit low but readable.

Just keep an eye on the 5V, if it dips during servo operation it will usually make your code reset.

Tom... :smiley: :+1: :coffee: :australia:

Thanks.

There have been no issues with Robot#1 and its code. The LEDs work fine, as do the servos. The main difference between the Robot#1 and Robot#2 (black one), is that Robot#1's code has delays in it. Robot#2 does not.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.