Servo control using serial port - delays with millis - problem

Hi,

I’m trying to modify a program I wrote using millis() counters to create a timed action (moving a servo, stopping, and then moving a servo) when receiving a single character over the serial port instead of using a windows batch file with a delay between characters to send multiple commands over the serial port.

I want to send a character (say “b”) over the serial port and I want the servo to move to position, wait there, and then move back to the same position - a timed action.

I need to do this without using a delay in the code - hence trying to use millis() - because I want to be able to receive other characters while it’s doing an action - multitasking in a sense.

I was using multiple batch files with multiple commands and delays and it worked fine under most condition - but I want to try something different.

My program isn’t working properly - I can’t get the servos to move.

I think some variable is not set correctly.

Any advice would be greatly appreciated.

Here’s a trimmed version of the program.

#include <Servo.h> 

Servo servo_01;
Servo servo_02;
Servo servo_03;
Servo servo_04;
Servo servo_05;
Servo servo_06;

int b_pressed = 0;
int c_pressed = 0;

long b_millis = 0;
long c_millis = 0;

void setup() {
  
  servo_01.attach(10);
  servo_02.attach(11);
  servo_03.attach(9);
  servo_04.attach(6);
  servo_05.attach(5);
  servo_06.attach(3);
  
  servo_01.write(68);  
  servo_02.write(70);
  servo_03.write(65);
  servo_04.write(75);
  servo_05.write(57);
  servo_06.write(83);
    
  Serial.begin(9600); 
} 

void loop() {
    
  if (Serial.available() > 0) {
    
    int keyboard_button = Serial.read();
    
    switch (keyboard_button) {
      
      case 'a': 
        servo_01.write(68);
        break;

      case 'b':
        servo_01.write(57);
        b_pressed = 1;
        b_millis = millis();
        break;
        
      case 'c':
        servo_01.write(56);
        c_pressed = 1;
        c_millis = millis();
        break;
        
      case 'd':
        servo_01.write(118);
        break;
      
      case 'e':
        servo_01.write(119);
        break;
        
       
    }  
  }
  
  if (b_pressed = 1) {
    if (millis() - b_millis > 5300) {
      servo_01.write(68);
      b_pressed = 0;
    }
  }
  
  if (c_pressed = 1) {
    if (millis() - c_millis > 5300) {
      servo_01.write(68);
      c_pressed = 0;
    }
  }
  
 
}

This:

  if (b_pressed = 1)

should probably be this:

  if (b_pressed == 1)

I want to send a character (say “b”) over the serial port and I want the servo to move to position, wait there, and then move back to the same position - a timed action.

Back to the same position? Same as what?

  servo_01.attach(10);
  servo_02.attach(11);
  servo_03.attach(9);
  servo_04.attach(6);
  servo_05.attach(5);
  servo_06.attach(3);

Servos can be attached to any digital pin - not just the PWM ones.

  if (b_pressed = 1) {

An assignment statement is pretty useless. Using == would work better.

Now, none of this matters if none of the servos ever moves. If none of them ever move, you have a wiring problem. How are the servos powered?

Oh Geez,

What a stupid mistake on my part - I apologize for that.

Air condition isn't working well today.

Thanks.

Air condition isn’t working well today.

Not working too well here, either. Only 59 damned degrees outside.

Well, not quite solved.

Here’s the full code with the changes - but it doesn’t move the servos as needed.

The commands that don’t have an if condition work - those with the if condition at the bottom don’t work.

If I send a “b”, nothing happens.

Any ideas?

#include <Servo.h> 

Servo servo_01;
Servo servo_02;
Servo servo_03;
Servo servo_04;
Servo servo_05;
Servo servo_06;

int usb_switch_01 = 8;
int usb_switch_02 = 7;
int usb_switch_03 = 13;

int b_pressed = 0;
int c_pressed = 0;

int g_pressed = 0;
int h_pressed = 0;

int l_pressed = 0;
int m_pressed = 0;

long b_millis = 0;
long c_millis = 0;

long g_millis = 0;
long h_millis = 0;

long l_millis = 0;
long m_millis = 0;

void setup() {
  
  servo_01.attach(10);
  servo_02.attach(11);
  servo_03.attach(9);
  servo_04.attach(6);
  servo_05.attach(5);
  servo_06.attach(3);
  
  servo_01.write(68);  
  servo_02.write(70);
  servo_03.write(65);
  servo_04.write(75);
  servo_05.write(57);
  servo_06.write(83);
  
  pinMode(usb_switch_01, OUTPUT);
  pinMode(usb_switch_02, OUTPUT);
  pinMode(usb_switch_03, OUTPUT);
  
  digitalWrite(usb_switch_01, LOW);
  digitalWrite(usb_switch_02, LOW);
  digitalWrite(usb_switch_03, LOW);
  
  Serial.begin(9600); 
} 

void loop() {
    
  if (Serial.available() > 0) {
    
    int keyboard_button = Serial.read();
    
    switch (keyboard_button) {
      
      case 'a': 
        servo_01.write(68);
        break;

      case 'b':
        servo_01.write(57);
        b_pressed == 1;
        b_millis == millis();
        break;
        
      case 'c':
        servo_01.write(56);
        c_pressed == 1;
        c_millis == millis();
        break;
        
      case 'd':
        servo_01.write(118);
        break;
      
      case 'e':
        servo_01.write(119);
        break;
        
        
        
       
      case 'f':
        servo_03.write(65);
        break;
        
      case 'g':
        servo_03.write(55);
        g_pressed == 1;
        g_millis == millis();
        break;
        
      case 'h':
        servo_03.write(54);
        h_pressed == 1;
        h_millis == millis();
        break;
        
      case 'i':
        servo_03.write(106);
        break;
        
      case 'j':
        servo_03.write(107);
        break;
      
      
      
      
      case 'k':
        servo_05.write(57);
        break;
        
      case 'l':
        servo_05.write(47);
        l_pressed == 1;
        l_millis == millis();
        break;
        
      case 'm':
        servo_05.write(46);
        m_pressed == 1;
        m_millis == millis();
        break;
        
      case 'n':
        servo_05.write(106);
        break;
        
      case 'o':
        servo_05.write(107);
        break;
        
      
      
      
      case 'p':
        servo_02.write(70);
        break;
        
      case 'q':
        servo_02.write(46);
        break;
        
      
      
      
      case 'r':
        servo_04.write(75);
        break;
        
      case 's':
        servo_04.write(51);
        break;
        
        
        
        
      case 't':
        servo_06.write(83);
        break;
        
      case 'u':
        servo_06.write(107);
        break;
        
        
        
        
      case '1':
        digitalWrite(usb_switch_01, LOW);
        break;
        
      case '4':
        digitalWrite(usb_switch_01, HIGH);
        break;
        
        
        
        
      case '2':
        digitalWrite(usb_switch_02, LOW);
        break;
        
      case '5':
        digitalWrite(usb_switch_02, HIGH);
        break;
        
        
        
        
      case '3':
        digitalWrite(usb_switch_03, LOW);
        break;
        
      case '6':
        digitalWrite(usb_switch_03, HIGH);
        break;
    }  
  }
  
  if (b_pressed = 1) {
    if (millis() - b_millis > 5300) {
      servo_01.write(68);
      b_pressed == 0;
    }
  }
  
  if (c_pressed = 1) {
    if (millis() - c_millis > 5300) {
      servo_01.write(68);
      c_pressed == 0;
    }
  }
  
  if (g_pressed = 1) {
    if (millis() - g_millis > 5300) {
      servo_03.write(65);
      g_pressed == 0;
    }
  }
  
  if (h_pressed = 1) {
    if (millis() - h_millis > 5300) {
      servo_03.write(65);
      h_pressed == 0;
    }
  }
  
  if (l_pressed = 1) {
    if (millis() - l_millis > 5300) {
      servo_05.write(57);
      l_pressed == 0;
    }
  }
  
  if (m_pressed = 1) {
    if (millis() - m_millis > 5300) {
      servo_05.write(57);
      m_pressed == 0;
    }
  }
}

Those assignments should be comparisons.

Oh wait, I screwed up... I put the == in the wrong places - geez, I gotta get this air condition working.

Gonna check out the fixes now.

Sorry.

Everything works now.

Thanks for the wake-up.

Here’s the working code:

#include <Servo.h> 

Servo servo_01;
Servo servo_02;
Servo servo_03;
Servo servo_04;
Servo servo_05;
Servo servo_06;

int usb_switch_01 = 8;
int usb_switch_02 = 7;
int usb_switch_03 = 13;

int b_pressed = 0;
int c_pressed = 0;

int g_pressed = 0;
int h_pressed = 0;

int l_pressed = 0;
int m_pressed = 0;

long b_millis = 0;
long c_millis = 0;

long g_millis = 0;
long h_millis = 0;

long l_millis = 0;
long m_millis = 0;

void setup() {
  
  servo_01.attach(10);
  servo_02.attach(11);
  servo_03.attach(9);
  servo_04.attach(6);
  servo_05.attach(5);
  servo_06.attach(3);
  
  servo_01.write(68);  
  servo_02.write(70);
  servo_03.write(65);
  servo_04.write(75);
  servo_05.write(57);
  servo_06.write(83);
  
  pinMode(usb_switch_01, OUTPUT);
  pinMode(usb_switch_02, OUTPUT);
  pinMode(usb_switch_03, OUTPUT);
  
  digitalWrite(usb_switch_01, LOW);
  digitalWrite(usb_switch_02, LOW);
  digitalWrite(usb_switch_03, LOW);
  
  Serial.begin(9600); 
} 

void loop() {
    
  if (Serial.available() > 0) {
    
    int keyboard_button = Serial.read();
    
    switch (keyboard_button) {
      
      case 'a': 
        servo_01.write(68);
        break;

      case 'b':
        servo_01.write(57);
        b_pressed = 1;
        b_millis = millis();
        break;
        
      case 'c':
        servo_01.write(56);
        c_pressed = 1;
        c_millis = millis();
        break;
        
      case 'd':
        servo_01.write(118);
        break;
      
      case 'e':
        servo_01.write(119);
        break;
        
        
        
       
      case 'f':
        servo_03.write(65);
        break;
        
      case 'g':
        servo_03.write(55);
        g_pressed = 1;
        g_millis = millis();
        break;
        
      case 'h':
        servo_03.write(54);
        h_pressed = 1;
        h_millis = millis();
        break;
        
      case 'i':
        servo_03.write(106);
        break;
        
      case 'j':
        servo_03.write(107);
        break;
      
      
      
      
      case 'k':
        servo_05.write(57);
        break;
        
      case 'l':
        servo_05.write(47);
        l_pressed = 1;
        l_millis = millis();
        break;
        
      case 'm':
        servo_05.write(46);
        m_pressed = 1;
        m_millis = millis();
        break;
        
      case 'n':
        servo_05.write(106);
        break;
        
      case 'o':
        servo_05.write(107);
        break;
        
      
      
      
      case 'p':
        servo_02.write(70);
        break;
        
      case 'q':
        servo_02.write(46);
        break;
        
      
      
      
      case 'r':
        servo_04.write(75);
        break;
        
      case 's':
        servo_04.write(51);
        break;
        
        
        
        
      case 't':
        servo_06.write(83);
        break;
        
      case 'u':
        servo_06.write(107);
        break;
        
        
        
        
      case '1':
        digitalWrite(usb_switch_01, LOW);
        break;
        
      case '4':
        digitalWrite(usb_switch_01, HIGH);
        break;
        
        
        
        
      case '2':
        digitalWrite(usb_switch_02, LOW);
        break;
        
      case '5':
        digitalWrite(usb_switch_02, HIGH);
        break;
        
        
        
        
      case '3':
        digitalWrite(usb_switch_03, LOW);
        break;
        
      case '6':
        digitalWrite(usb_switch_03, HIGH);
        break;
    }  
  }
  
  if (b_pressed == 1) {
    if (millis() - b_millis > 5300) {
      servo_01.write(68);
      b_pressed = 0;
    }
  }
  
  if (c_pressed == 1) {
    if (millis() - c_millis > 5300) {
      servo_01.write(68);
      c_pressed = 0;
    }
  }
  
  if (g_pressed == 1) {
    if (millis() - g_millis > 5300) {
      servo_03.write(65);
      g_pressed = 0;
    }
  }
  
  if (h_pressed == 1) {
    if (millis() - h_millis > 5300) {
      servo_03.write(65);
      h_pressed = 0;
    }
  }
  
  if (l_pressed == 1) {
    if (millis() - l_millis > 5300) {
      servo_05.write(57);
      l_pressed = 0;
    }
  }
  
  if (m_pressed == 1) {
    if (millis() - m_millis > 5300) {
      servo_05.write(57);
      m_pressed = 0;
    }
  }
}