Please help me rewrite delay with millis

#include <Servo.h> 

  
Servo left_servo;              // Define left servo
Servo right_servo;             // Define right servo
 
#include <IRremote.h>

//TV remote
#define input 2640
#define power 2704
#define syncMenu 3416
#define stbMenu 18723
#define one 16
#define two 2064
#define three 1040
#define four 3088
#define five 528
#define six 2576
#define seven 1552
#define eight 3600
#define nine 272
#define zero 2320
#define dot 23785
#define display 1488
#define googlePlay 12579
#define netflix 8024
#define yellow 29417
#define blue 4841
#define red 21225
#define green 13033
#define actionMenu 26915
#define guide 27941
#define apps 10787
#define back 25321
#define home 112
#define up 752
#define down 2800
#define left 720
#define right 3280
#define enter 2672
#define volPlus 1168
#define volMinus 3216
#define jump 3536
#define mute 656
#define chPlus 144
#define chMinus 2192
#define audio 3728
#define fastRewind 27881
#define play 11497
#define fastForward 7401
#define preSearch 7913
#define pause 19689
#define nextSearch 24297
#define stop 3305
#define subtitle 2793
#define help 22819
#define wide 24101
#define picOff 2000

//Infrared remote == JVC LP21036-038
#define vcr 
#define dvd 
#define power 
#define topMenu
#define menu
#define mute
#define tv_vcr
#define repeat_plus
#define repeat_minus
#define onScreen_plus
#define onScreen_minus
#define date_plus
#define date_minus
#define ch_plus
#define ch_minus
#define one
#define two
#define three
#define four
#define five
#define six
#define seven
#define eight
#define nine
#define zero
#define cancel_reset
#define plus_ten
#define prog
#define timer
#define return
#define rec_line
#define rew
#define play
#define ff
#define rec
#define stop
#define pause
#define up
#define down
#define left
#define right
#define enter
#define pre_search
#define next_search
#define set_up
#define display 
#define phonic
#define vol_minus
#define vol_plus
#define dvd_picture

int buzzer_red = 11;    //  Red wire = Positive
int buzzer_black = 12;  //  Black wire = negaetive
int RECV_PIN = 5; //  Infrared signal pin

// Four independent timed
unsigned long currentMillis = 0;

const unsigned long eventMillis_1 = 1000;
const unsigned long eventMillis_2 = 400;
const unsigned long eventMillis_3 = 1000;
const unsigned long eventMillis_4 = 400;

unsigned long previousMillis_1 = 0;
unsigned long previousMillis_2 = 0;
unsigned long previousMillis_3 = 0;
unsigned long previousMillis_4 = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;
volatile int active_left = LOW;
volatile int active_right = LOW;
boolean started = false;
 
void setup() {
  // Set pin modes for switches
  pinMode(buzzer_red, OUTPUT);       //  Buzzer positive output
  pinMode(buzzer_black, OUTPUT);     //  Buzzer negative output
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);     // Serves as ground connection
   
  pinMode(6, OUTPUT);       // IR power, ground pins
  pinMode(7, OUTPUT);
  digitalWrite(6, LOW);     // IR ground
  digitalWrite(7, HIGH);    // IR power
 
  right_servo.attach(10);    // Set right servo to digital pin 9  
  left_servo.attach(9);    // Set left servo to digital pin 10
  irrecv.enableIRIn();     // Start the receiver
   
  Serial.begin(9600);
     
  // Set up interrupts
  attachInterrupt(0, bump_right, FALLING);
  attachInterrupt(1, bump_left, FALLING);
   
  started = true;
}
 
void loop() {

  /* Updates frequently */
  unsigned long currentMillis = millis();

  /* This is event 1 stuff */

   
  if (active_left == HIGH) {           // If left bumper hit
    //tone(buzzer_red, 1250, 1000);                                                  //  Buzzer at 1,250 Hz for 1 second
    go_backwards();
    delay(1000); 
    spin_right();
    delay(400);
    go_forward();
    active_left = LOW;
    Serial.println("active_left");
  }
   
  if (active_right == HIGH) {          // If right bumper hit
    //tone(buzzer_red, 1250, 1000);                                                  //  Buzzer at 1,250 Hz for 1 second
    go_backwards();
    delay(1000); 
    spin_left();
    delay(400);
    go_forward();
    active_right = LOW;
    Serial.println("active_right");  
  }
   
  if (irrecv.decode(&results)) {
    Serial.print(results.value, DEC);
    Serial.print("    ");
    Serial.println(results.value);
    switch (results.value) {
      case one:
        Serial.println("1");     // Turn left forward
        left_turn_fwd();
        break;
      case two:
        Serial.println("2");     // Forward
        go_forward();
        break;
      case three:
        Serial.println("3");     // Turn right forward
        right_turn_fwd();
        break;
      case four:
        Serial.println("4");    // Spin left
        spin_left();
        break;
      case five:
        Serial.println("5");    // Stop
        stop_all();
        break;
      case six:
        Serial.println("6");    // Spin right
        spin_right();
        break;
     case seven:
        Serial.println("7");    // Turn left reverse
        left_turn_backwards();
        break;
      case eight:
        Serial.println("8");    // Reverse
        go_backwards();
        break;
      case nine:
        Serial.println("9");    // Turn right reverse
        turn_right_backwards();
        break;
    }        
    irrecv.resume(); // Receive the next value
    delay(2);
  }
}
 
// Routines for forward, reverse, turns, and stop
void go_forward() {
  left_servo.write(1700);
  right_servo.write(1300);
}
void go_backwards() {
  left_servo.write(1300);
  right_servo.write(1700);
}
void spin_right() {
  left_servo.write(1700);
  right_servo.write(1700);
}
void spin_left() {
  left_servo.write(1300);
  right_servo.write(1300);
}
void right_turn_fwd() {
  left_servo.write(1700);
  right_servo.write(1500);
}
void left_turn_fwd() {
  left_servo.write(1500);
  right_servo.write(1300);
}
void left_turn_backwards() {
  left_servo.write(1500);
  right_servo.write(1700);
}
void turn_right_backwards() {
  left_servo.write(1300);
  right_servo.write(1500);
}
void stop_all() {
  left_servo.write(1500);
  right_servo.write(1500);
}
 
// Interrupt service routines
void bump_left() {
  if (started)
    active_left = HIGH;
}
void bump_right() {
  if (started) 
    active_right = HIGH;
}

This code works fine. All I want is someone to rewrite my delays as millis. I fully understand the millis function but not in a row (one after another).

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

1 Like

look at you code, IMHO, you don't really need to go through doing a radical change from 'delay' to millis().

if I were you, I would keep the 2ms delay as a loop delay and then use variables are counters to trigger the various stepper operations (maybe even make a FSM for that :wink: )

happy coding!

It needs to be changed for me to add a flashing light.

you did not understand my suggestion.... :upside_down_face:

the counters would get rid of your large delays in your code.

unless ur flashing light is >500Hz, the 2ms is insignificant! :wink:

hope that help...

OP's free to choose whatever loop delay to use.

I merely pick one out from when I skimmed thru his code! :stuck_out_tongue:

Interesting chosen words "rewrite my delays as millis".

Yes sure using millis needs to some extent rewriting the code.

You can see your functionality as different modes of operation are needed

if left bumper is hit

change from
mode 1: go_forward
to
mode 2: go_backwards for a 1000 milliseconds
after that change to
mode 3: spin_right for 400 millisecods
after that change back to
mode 1: go_forward

if right bumper is hit

change from
mode 1: go_forward
to
mode 2: go_backwards for a 1000 milliseconds
after that change to
mode 4: spin_left for 400 millisecods
after that back to
mode 1: go_forward

This means you have two sequences of certain modes of operation
that shall be run trhough always in the same order

These sequences with different modes of operation are invoked by the event
left bumper hit / right bumper hit

In Programming something like this is called a state-machine.
Each mode of operation is a "state" the "machine" is in.

There are events like

  • "sensor-value becomes "HIGH"
    or
  • sensor-value goes above a threshold-value
    or
  • sensor-value goes below a threshold-value
    or
  • a certain amount of time has passed by

This is what your code is doing depending on events happening change the mode of operation
= change the state

The event a certain amount of time has passed by (since a reference-point in time)
is something that you know from your everyday life
example
baking a frosted pizza. The cover says "baking time 10 minutes"

You can start at any time of the day to put the frosted pizza into a hot oven
You take a look onto your watch and make a snapshot of time inside your head
= the reference point in time
for example putting the frosted pizza into the oven at the time 13:05
baking time is ten minutes means at time 13:15 pizza is ready to eat

another day you do it at time 14:37
baking time is ten minutes means at time 14:47 pizza is ready to eat
etc. etc.
you check the time that has passed by since a reference-point in time
The rest of the details is explained in this tutorial

How to code different modes of operation named state-machine are explained here

So start reading in these tutorials and ask questions if you have any question

best regards Stefan

Note that your have a conflicting defines in your code:
First you define one, two, three ... etc as numbers:

and than redefime the same macros as nothing:

the result will not that you expected, as I think.

In general, you never should define the same macros more than once in the code.

Two tv remote. the second one was suppose to be commented out.

but it not commented in real code... it can cause a error

consider the following in place of the code in loop() that check for active_left/right being HIGH

    static unsigned long msecPeriod = 0;
    static unsigned long msecLst;
    static int           state = 0;

    if (active_right == HIGH || active_left == HIGH) {
        if (currentMillis - msecLst >= msecPeriod)  {
            msecLst = currentMillis;

            switch (state)  {
            case 0:
                go_backwards();
                msecPeriod = 1000;
                state = 1;
                break;

            case 1:
                if (active_right == HIGH)
                    spin_right();
                else
                    spin_left();
                msecPeriod = 400;
                state = 2;
                break;

            case 2:
                go_forward();
                active_right = active_left = 0;
                msecPeriod   = 0;
                state        = 0;
                break;
            }
        }
    }

error currentMillis was not declared

it was in your original code

sorry i deleted the line "currentMillis = 0"

so does this change do what you want?

code change does nothing

I hate the chatty ones.

the code change renders useless

#include <Servo.h> 
 
/**
 * This code runs the remote controlled robot
 * 
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-14
 */
  
Servo left_servo;              // Define left servo
Servo right_servo;             // Define right servo
 
#include <IRremote.h>

//TV remote
#define input 2640
#define power 2704
#define syncMenu 3416
#define stbMenu 18723
#define one 16
#define two 2064
#define three 1040
#define four 3088
#define five 528
#define six 2576
#define seven 1552
#define eight 3600
#define nine 272
#define zero 2320
#define dot 23785
#define display 1488
#define googlePlay 12579
#define netflix 8024
#define yellow 29417
#define blue 4841
#define red 21225
#define green 13033
#define actionMenu 26915
#define guide 27941
#define apps 10787
#define back 25321
#define home 112
#define up 752
#define down 2800
#define left 720
#define right 3280
#define enter 2672
#define volPlus 1168
#define volMinus 3216
#define jump 3536
#define mute 656
#define chPlus 144
#define chMinus 2192
#define audio 3728
#define fastRewind 27881
#define play 11497
#define fastForward 7401
#define preSearch 7913
#define pause 19689
#define nextSearch 24297
#define stop 3305
#define subtitle 2793
#define help 22819
#define wide 24101
#define picOff 2000

//Infrared remote == JVC LP21036-038
/* #define vcr 
#define dvd 
#define power 
#define topMenu
#define menu
#define mute
#define tv_vcr
#define repeat_plus
#define repeat_minus
#define onScreen_plus
#define onScreen_minus
#define date_plus
#define date_minus
#define ch_plus
#define ch_minus
#define one
#define two
#define three
#define four
#define five
#define six
#define seven
#define eight
#define nine
#define zero
#define cancel_reset
#define plus_ten
#define prog
#define timer
#define return
#define rec_line
#define rew
#define play
#define ff
#define rec
#define stop
#define pause
#define up
#define down
#define left
#define right
#define enter
#define pre_search
#define next_search
#define set_up
#define display 
#define phonic
#define vol_minus
#define vol_plus
#define dvd_picture */

int buzzer_red = 11;    //  Red wire = Positive
int buzzer_black = 12;  //  Black wire = negaetive
int RECV_PIN = 5; //  Infrared signal pin

// Four independent timed
static unsigned long currentMillis = 0;

static unsigned long msecPeriod = 0;
static unsigned long msecLst;
static int           state = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;
volatile int active_left = LOW;
volatile int active_right = LOW;
boolean started = false;
 
void setup() {
  // Set pin modes for switches
  pinMode(buzzer_red, OUTPUT);       //  Buzzer positive output
  pinMode(buzzer_black, OUTPUT);     //  Buzzer negative output
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);     // Serves as ground connection
   
  pinMode(6, OUTPUT);       // IR power, ground pins
  pinMode(7, OUTPUT);
  digitalWrite(6, LOW);     // IR ground
  digitalWrite(7, HIGH);    // IR power
 
  right_servo.attach(10);    // Set right servo to digital pin 9  
  left_servo.attach(9);    // Set left servo to digital pin 10
  irrecv.enableIRIn();     // Start the receiver
   
  Serial.begin(9600);
     
  // Set up interrupts
  attachInterrupt(0, bump_right, FALLING);
  attachInterrupt(1, bump_left, FALLING);
   
  started = true;
}
 
void loop() {

  /* Updates frequently */

  /*static unsigned long msecPeriod = 0;
  static unsigned long msecLst;
  static int           state = 0;*/

  if (active_right == HIGH || active_left == HIGH) {
    if (currentMillis - msecLst >= msecPeriod)  {
      msecLst = currentMillis;

      switch (state)  {
        case 0:
          go_backwards();
          msecPeriod = 1000;
          state = 1;
          break;

        case 1:
          if (active_right == HIGH)
            spin_right();
          else
            spin_left();
            msecPeriod = 400;
            state = 2;
            break;

        case 2:
          go_forward();
          active_right = active_left = 0;
          msecPeriod   = 0;
          state        = 0;
          break;
      }
    }
  }

  
  /*if (active_left == HIGH) {           // If left bumper hit
    //tone(buzzer_red, 1250, 1000);                                                  //  Buzzer at 1,250 Hz for 1 second
    go_backwards();
    delay(1000); 
    spin_right();
    delay(400);
    go_forward();
    active_left = LOW;
    Serial.println("active_left");
  }
   
  if (active_right == HIGH) {          // If right bumper hit
    //tone(buzzer_red, 1250, 1000);                                                  //  Buzzer at 1,250 Hz for 1 second
    go_backwards();
    delay(1000); 
    spin_left();
    delay(400);
    go_forward();
    active_right = LOW;
    Serial.println("active_right");  
  }*/
   
  if (irrecv.decode(&results)) {
    Serial.print(results.value, DEC);
    Serial.print("    ");
    Serial.println(results.value);
    switch (results.value) {
      case one:
        Serial.println("1");     // Turn left forward
        left_turn_fwd();
        break;
      case two:
        Serial.println("2");     // Forward
        go_forward();
        break;
      case three:
        Serial.println("3");     // Turn right forward
        right_turn_fwd();
        break;
      case four:
        Serial.println("4");    // Spin left
        spin_left();
        break;
      case five:
        Serial.println("5");    // Stop
        stop_all();
        break;
      case six:
        Serial.println("6");    // Spin right
        spin_right();
        break;
     case seven:
        Serial.println("7");    // Turn left reverse
        left_turn_backwards();
        break;
      case eight:
        Serial.println("8");    // Reverse
        go_backwards();
        break;
      case nine:
        Serial.println("9");    // Turn right reverse
        turn_right_backwards();
        break;
    }        
    irrecv.resume(); // Receive the next value
    delay(2);
  }
}
 
// Routines for forward, reverse, turns, and stop
void go_forward() {
  left_servo.write(1700);
  right_servo.write(1300);
}
void go_backwards() {
  left_servo.write(1300);
  right_servo.write(1700);
}
void spin_right() {
  left_servo.write(1700);
  right_servo.write(1700);
}
void spin_left() {
  left_servo.write(1300);
  right_servo.write(1300);
}
void right_turn_fwd() {
  left_servo.write(1700);
  right_servo.write(1500);
}
void left_turn_fwd() {
  left_servo.write(1500);
  right_servo.write(1300);
}
void left_turn_backwards() {
  left_servo.write(1500);
  right_servo.write(1700);
}
void turn_right_backwards() {
  left_servo.write(1300);
  right_servo.write(1500);
}
void stop_all() {
  left_servo.write(1500);
  right_servo.write(1500);
}
 
// Interrupt service routines
void bump_left() {
  if (started)
    active_left = HIGH;
}
void bump_right() {
  if (started) 
    active_right = HIGH;
}

the code for the collision no longer works

still doesn't work