Millis timer outside of loop() function

For the most part, everything works. I am trying to make my motors "slowly" ramp up to speed when void Forward_() is called instead of just jumping in at full blast. If the current void Forward_() was in my void loop() it would work, but because it isn't in a continuous loop it only increments once and stops. I have placed serial prints on almost everything so you can load this up and run it with no other hardware and see what is going on in the serial monitor. Even made cute menus so you can see the commands. :slight_smile:
So, the only thing I really want to change is the motor speed ramping up because 0 to 255 just spins unless on carpet. I can use delays but I don't want to. Slowly ramping up also allows more fine control if you only want to go several inches then stop.
I'm open to hearing suggestions on other things too but everything else works. I'm sure some of it can be streamlined but for now, I'm happy except for this issue.

#include "VT100.h" //terminal emulator
#include "SoftwareSerial.h"
#define rxPin 10 //sw serial rx
#define txPin 11 //sw serial tx
#define MOTORL1 8 //to motor controller left1
#define MOTORL2 7 //to motor controller left2
#define MOTORR1 4 //to motor controller right1
#define MOTORR2 3 //to motor controller right2
#define PWM_L 9   //to motor controller EN pin left
#define PWM_R 5   //to motor controller EN pin right
#define speakerPin 12
#define ledPin 13
unsigned long previousMillis=0;
unsigned long currentMillis;
int speed=0;
unsigned long interval=500;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte SerialCommand;  //Input from Serial connection
byte speedT; //Reduced turn motor speed
int VoffR = 0;  //variable offset right
int VoffL = 0;  //variable offset left


void setup() {
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    Serial.begin(9600); //hardware serial
    mySerial.begin(9600); //software serial
  delay(50);
  randomSeed(analogRead(5));

  pinMode(MOTORL1, OUTPUT);
  pinMode(MOTORL2, OUTPUT);
  pinMode(MOTORR1, OUTPUT);
  pinMode(MOTORR2, OUTPUT);
  pinMode(PWM_L, OUTPUT);
  pinMode(PWM_R, OUTPUT);
  speak(); //play sound on boot up
  splash(); //show splash screen in terminal program

  //sounds
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  randomSeed(analogRead(0));
  speak();
   }


void loop() {
  if (Serial.available() > 0)  // Checks for data on SW serial port
  {
    SerialCommand = Serial.read();  // Reads serial data and assign value to variable 'SerialCommand'
  }
  /*--(end serial loop )-- */
  if (mySerial.available() > 0)  // Checks for data on SW serial port
  {
    SerialCommand = mySerial.read();  // Reads serial data and assign value to variable 'SerialCommand'
  }
  /*--(end sw serial loop )-- */

  switch (SerialCommand) {
    case '?': menu(); break;
    case '8': Forward_(); Serial.println(F("Direction : Forward")); break; //may need to add speed=0;
    case '4': PivotLeft_(); Serial.println(F("Direction : Left Pivot")); break;
    case '6': PivotRight_(); Serial.println(F("Direction : Right Pivot")); break;
    case '2': Back_(); Serial.println(F("Direction : Reverse")); break;
    case '5': Stop_(); Serial.println(F("Direction : Stop")); break;
    case '1': speak(); Serial.println(F("Speaking")); break;
    case '0': splash(); break;
    case 'u': VoffLup(); break;
    case 'd': VoffLdn(); break;
    case 'U': VoffRup(); break;
    case 'D': VoffRdn(); break;
    case 's': speed = 50; break;
    case 'm': speed = 75; break;
    case 'f': speed = 95; break;
    case 'q': System_Status_(); break;
    default:;
  }

  SerialCommand = 0;  //reset SerialCommand and wait for next instruction.  Allows last command to auto repeat.
}

void Forward_() {
  currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
   if (speed<250) {
      speed=(speed+10);

  digitalWrite(MOTORL1, HIGH);
  digitalWrite(MOTORL2, LOW);
  digitalWrite(MOTORR1, HIGH);
  digitalWrite(MOTORR2, LOW);
  analogWrite(PWM_L, (speed + VoffL));
  analogWrite(PWM_R, (speed + VoffR));
  Serial.println(speed); 
         

}

  SerialCommand = 0;
}
}

void PivotRight_() {
  speedT = speed / 2;
  digitalWrite(MOTORL1, LOW);
  digitalWrite(MOTORL2, HIGH);
  digitalWrite(MOTORR1, HIGH);
  digitalWrite(MOTORR2, LOW);
  analogWrite(PWM_L, (speedT + VoffL));
  analogWrite(PWM_R, (speedT + VoffR));
  SerialCommand = 0;
}


void PivotLeft_() {
  speedT = speed / 2;
  digitalWrite(MOTORL1, HIGH);
  digitalWrite(MOTORL2, LOW);
  digitalWrite(MOTORR1, LOW);
  digitalWrite(MOTORR2, HIGH);
  analogWrite(PWM_L, (speedT + VoffL));
  analogWrite(PWM_R, (speedT + VoffR));
  SerialCommand = 0;
}


void Back_() {
  digitalWrite(MOTORL1, LOW);
  digitalWrite(MOTORL2, HIGH);
  digitalWrite(MOTORR1, LOW);
  digitalWrite(MOTORR2, HIGH);
  analogWrite(PWM_L, (speed + VoffL));
  analogWrite(PWM_R, (speed + VoffR));
  SerialCommand = 0;
}

void Stop_() {
  analogWrite(PWM_L, 0);
  analogWrite(PWM_R, 0);
  speed=0;
  SerialCommand = 0;
}

void splash() {
  Serial.println(F(""));
  Serial.println(F(""));
  Serial.println(F(""));
  Serial.println(F(":::..::::::......:::::::::::::::::..::::..:::.......:::"));
  Serial.println(F("'########::::'##::::::::::::::::::'##::: ##::'#######::"));
  Serial.println(F("... ##..:::'####:::::::::::::::::: ###:: ##:'##.... ##:"));
  Serial.println(F("::: ##:::::.. ##:::::::::::::::::: ####: ##:..::::: ##:"));
  Serial.println(F("::: ##::::::: ##::::::'#######:::: ## ## ##::'#######::"));
  Serial.println(F("::: ##::::::: ##::::::........:::: ##. ####::...... ##:"));
  Serial.println(F("::: ##::::::: ##:::::::::::::::::: ##:. ###:'##:::: ##:"));
  Serial.println(F("::: ##:::::'######:::::::::::::::: ##::. ##:. #######::"));
  Serial.println(F(":::..::::::......:::::::::::::::::..::::..:::.......:::"));
  Serial.println(F("-------------------------------------------------------"));
  Serial.println(F("      Working Prototype and code Copyright    "));
  Serial.println(F("                   Joe Willson                "));
  Serial.println(F("               For Commands Press '?'         "));
}

void menu() {
  Serial.println(F("#####################################"));
  Serial.println(F("#        Commands:                  #"));
  Serial.println(F("#                                   #"));
  //Serial.println(F("# Remote Control Mode = r            #"));
  //Serial.println(F("# Autonomous Mode     = a            #"));
  Serial.println(F("# Forward    = 8         _ _        #"));
  Serial.println(F("# Reverse    = 2        (_**)       #"));
  Serial.println(F("# Left Turn  = 4       __) #_       #"));
  Serial.println(F("# Right Turn = 6      ( )...()      #"));
  Serial.println(F("# Stop       = 5      || | |I|      #"));
  Serial.println(F("# Show Splash = 0     || | |()=<    #"));
  Serial.println(F("# Speak      = 1      [](___)       #"));
  Serial.println(F("#                   _-''''''''-_    #"));
  Serial.println(F("#                    -,,,,,,,,-     #"));
  if (VoffR <= 9) {
    Serial.println((String) "# Left offset = u/d   Now = " + VoffL + "       #");
    Serial.println((String) "# Rght offset = U/D   Now = " + VoffR + "       #");
  } else {
    Serial.println((String) "# Left offset = u/d   Now = " + VoffL + "      #");
    Serial.println((String) "# Rght offset = U/D#  Now = " + VoffR + "       #");
  }
  Serial.println(F("# Speed: [s]low [m]edium [f]ast     #"));
  Serial.println(F("# System Status = q                 #"));
  Serial.println(F("#####################################"));
  SerialCommand = 0;
}

//Variable offsets set from terminal input
void VoffLup() {
  if (VoffL < 15)  //Max + offset=15
  {
    VoffL = VoffL + 1;
    Serial.println(F("Left offset speed increased by 1"));
  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffLdn() {
//  if (VoffL > 0)  //Minimum - offset=0
//  {
    VoffL =  VoffL - 1;
//    Serial.println(F("Left offset speed decreased by 1"));
//  }

  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffRup()  //Max + offset=15
{
  if (VoffR < 15) {
    VoffR = VoffR + 1;
    Serial.println(F("Rght offset speed increased by 1"));
  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffRdn() {
// if (VoffR > 0)  //Minimum - offset=0
//  {
    VoffR = VoffR - 1;
//    Serial.println(F("Rght offset speed decreased by 1"));
//  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
}
// End variable offsets

void System_Status_() {
  Serial.println(F("#####################################"));
  Serial.println(F("#           Project:Alpha           #"));
  Serial.println(F("#           System Status           #"));
  Serial.println(F("#                                   #"));
  if (VoffR <= 9) {
    Serial.println((String) "# Left motor offset is currently  " + VoffL + " #");
    Serial.println((String) "# Right motor offset is currently " + VoffR + " #");
  } else {
    Serial.println((String) "# Left motor offset is currently  " + VoffL + "#");
    Serial.println((String) "# Right motor offset is currently " + VoffR + "#");
  }
  Serial.println(F("#                                   #"));
//if (speed <= 99) {
//  Serial.println((String) "# Current Speed is set to   = " + speed + "%   #");
//} else {
//   Serial.println((String) "# Current Speed is set to   = " + speed + "%  #");
//  }
Serial.println((String) "# Left Speed is set to   = " + (speed + VoffL) + "%     #");
Serial.println((String) "# Right Speed is set to  = " + (speed + VoffR) + "%     #");
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#####################################"));
}

//sounds

void speak() {
  int K = 2000;  //2000
  switch (random(1, 12)) {

    case 1: phrase1(); break;
    case 2: phrase2(); break;
    case 3: phrase3(); break;
    case 4:
      phrase1();
      phrase2();
      break;
    case 5:
      phrase2();
      phrase1();
      break;
    case 6:
      break;
    case 7:
      phrase1();
      phrase2();
      phrase1();
      break;
    case 8:
      phrase2();
      phrase1();
      phrase2();
      break;
    case 9:
      phrase1();
      phrase3();
      break;
    case 10:
  //    phrase3();
      phrase2();
      break;
    case 11:
      break;

  }
  for (int i = 0; i <= random(2, 11); i++)  //2, 10 how many tones
  {

    digitalWrite(ledPin, HIGH);
    tone(speakerPin, K + random(-1700, 2000));  //-1700, 2000
    delay(random(70, 170));
    digitalWrite(ledPin, LOW);
    noTone(speakerPin);
    delay(random(0, 30));
  }
  noTone(speakerPin);
  //delay(random(2000, 4000));
}


void phrase1() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = 0; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = 0; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
}
void phrase2() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = 0; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = 0; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 2));
  }
}
void phrase3() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = -300; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = -100; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 3));
  }
}

The cause why it does not work is SerialCommand = 0; at the end of loop() and / or in the forward function.

In Forward_(), make the reset of SerialCommand happen if the speed is greater than or equal to 250; so basically the else of if (speed < 250).

And in loop(), you don't need to reset the SerialCommand to solve the problem so the line can be removed. There might be other sections of the code that rely on it but I'll leave it up to you to figure that out.

1 Like

Let me back up. That may be it! I remember having to put that in there for a reason but I wrote that part several years ago and am reusing parts of it. Thank you!

I know one iteration of this I had to add that serial command=0 because otherwise I had to keep pressing forward on the remote. It looks like I fixed it in my rewrite. Thank you again.

The only thing that is going to run continuously is your loop. So, what you need to do, is keep track of which direction you are going and then increase the speed every so often within the main loop. So, in your forward function you could have a flag like "movingForward" (you have to declare this as global). that you would set to true when you are moving forward.

Take a look at the changes here, this should lead you on the right path but it is untested:

#include "VT100.h" //terminal emulator
#include "SoftwareSerial.h"
#define rxPin 10 //sw serial rx
#define txPin 11 //sw serial tx
#define MOTORL1 8 //to motor controller left1
#define MOTORL2 7 //to motor controller left2
#define MOTORR1 4 //to motor controller right1
#define MOTORR2 3 //to motor controller right2
#define PWM_L 9   //to motor controller EN pin left
#define PWM_R 5   //to motor controller EN pin right
#define speakerPin 12
#define ledPin 13
int speed=0;
unsigned long interval=500;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte SerialCommand;  //Input from Serial connection
byte speedT; //Reduced turn motor speed
int VoffR = 0;  //variable offset right
int VoffL = 0;  //variable offset left
bool movingForward = false;
unsigned long previousTime = 0, rampDelay = 100;


void setup() {
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    Serial.begin(9600); //hardware serial
    mySerial.begin(9600); //software serial
  delay(50);
  randomSeed(analogRead(5));

  pinMode(MOTORL1, OUTPUT);
  pinMode(MOTORL2, OUTPUT);
  pinMode(MOTORR1, OUTPUT);
  pinMode(MOTORR2, OUTPUT);
  pinMode(PWM_L, OUTPUT);
  pinMode(PWM_R, OUTPUT);
  speak(); //play sound on boot up
  splash(); //show splash screen in terminal program

  //sounds
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  randomSeed(analogRead(0));
  speak();
   }


void loop() {
  if (Serial.available() > 0)  // Checks for data on SW serial port
  {
    SerialCommand = Serial.read();  // Reads serial data and assign value to variable 'SerialCommand'
  }
  /*--(end serial loop )-- */
  if (mySerial.available() > 0)  // Checks for data on SW serial port
  {
    SerialCommand = mySerial.read();  // Reads serial data and assign value to variable 'SerialCommand'
  }
  /*--(end sw serial loop )-- */

  switch (SerialCommand) {
    case '?': menu(); break;
    case '8': Forward_(); Serial.println(F("Direction : Forward")); break; //may need to add speed=0;
    case '4': PivotLeft_(); Serial.println(F("Direction : Left Pivot")); break;
    case '6': PivotRight_(); Serial.println(F("Direction : Right Pivot")); break;
    case '2': Back_(); Serial.println(F("Direction : Reverse")); break;
    case '5': Stop_(); Serial.println(F("Direction : Stop")); break;
    case '1': speak(); Serial.println(F("Speaking")); break;
    case '0': splash(); break;
    case 'u': VoffLup(); break;
    case 'd': VoffLdn(); break;
    case 'U': VoffRup(); break;
    case 'D': VoffRdn(); break;
    case 's': speed = 50; break;
    case 'm': speed = 75; break;
    case 'f': speed = 95; break;
    case 'q': System_Status_(); break;
    default:;
  }

  SerialCommand = 0;  //reset SerialCommand and wait for next instruction.  Allows last command to auto repeat.
  if(movingForward && millis()-previousTime>rampDleay){
     speed = speed+5;
     if(speed> 255){
        speed = 255;
        movingForward = false;
     }
     analogWrite(PWM_L, (speed + VoffL));
     analogWrite(PWM_R, (speed + VoffR));
     previousTime = millis();
  }
}

void Forward_() {
  speed = 10;
  digitalWrite(MOTORL1, HIGH);
  digitalWrite(MOTORL2, LOW);
  digitalWrite(MOTORR1, HIGH);
  digitalWrite(MOTORR2, LOW);
  analogWrite(PWM_L, (speed + VoffL));
  analogWrite(PWM_R, (speed + VoffR));
  Serial.println(speed); 
  movingForward = true;
  previousTime = millis();
  SerialCommand = 0;
}

void PivotRight_() {
  speedT = speed / 2;
  digitalWrite(MOTORL1, LOW);
  digitalWrite(MOTORL2, HIGH);
  digitalWrite(MOTORR1, HIGH);
  digitalWrite(MOTORR2, LOW);
  analogWrite(PWM_L, (speedT + VoffL));
  analogWrite(PWM_R, (speedT + VoffR));
  SerialCommand = 0;
 movingForward = false;
}


void PivotLeft_() {
  speedT = speed / 2;
  digitalWrite(MOTORL1, HIGH);
  digitalWrite(MOTORL2, LOW);
  digitalWrite(MOTORR1, LOW);
  digitalWrite(MOTORR2, HIGH);
  analogWrite(PWM_L, (speedT + VoffL));
  analogWrite(PWM_R, (speedT + VoffR));
  SerialCommand = 0;
  movingForward = false;
}


void Back_() {
  digitalWrite(MOTORL1, LOW);
  digitalWrite(MOTORL2, HIGH);
  digitalWrite(MOTORR1, LOW);
  digitalWrite(MOTORR2, HIGH);
  analogWrite(PWM_L, (speed + VoffL));
  analogWrite(PWM_R, (speed + VoffR));
  SerialCommand = 0;
  movingForward = false;
}

void Stop_() {
  analogWrite(PWM_L, 0);
  analogWrite(PWM_R, 0);
  speed=0;
  SerialCommand = 0;
  movingForward = false;
}

void splash() {
  Serial.println(F(""));
  Serial.println(F(""));
  Serial.println(F(""));
  Serial.println(F(":::..::::::......:::::::::::::::::..::::..:::.......:::"));
  Serial.println(F("'########::::'##::::::::::::::::::'##::: ##::'#######::"));
  Serial.println(F("... ##..:::'####:::::::::::::::::: ###:: ##:'##.... ##:"));
  Serial.println(F("::: ##:::::.. ##:::::::::::::::::: ####: ##:..::::: ##:"));
  Serial.println(F("::: ##::::::: ##::::::'#######:::: ## ## ##::'#######::"));
  Serial.println(F("::: ##::::::: ##::::::........:::: ##. ####::...... ##:"));
  Serial.println(F("::: ##::::::: ##:::::::::::::::::: ##:. ###:'##:::: ##:"));
  Serial.println(F("::: ##:::::'######:::::::::::::::: ##::. ##:. #######::"));
  Serial.println(F(":::..::::::......:::::::::::::::::..::::..:::.......:::"));
  Serial.println(F("-------------------------------------------------------"));
  Serial.println(F("      Working Prototype and code Copyright    "));
  Serial.println(F("                   Joe Willson                "));
  Serial.println(F("               For Commands Press '?'         "));
}

void menu() {
  Serial.println(F("#####################################"));
  Serial.println(F("#        Commands:                  #"));
  Serial.println(F("#                                   #"));
  //Serial.println(F("# Remote Control Mode = r            #"));
  //Serial.println(F("# Autonomous Mode     = a            #"));
  Serial.println(F("# Forward    = 8         _ _        #"));
  Serial.println(F("# Reverse    = 2        (_**)       #"));
  Serial.println(F("# Left Turn  = 4       __) #_       #"));
  Serial.println(F("# Right Turn = 6      ( )...()      #"));
  Serial.println(F("# Stop       = 5      || | |I|      #"));
  Serial.println(F("# Show Splash = 0     || | |()=<    #"));
  Serial.println(F("# Speak      = 1      [](___)       #"));
  Serial.println(F("#                   _-''''''''-_    #"));
  Serial.println(F("#                    -,,,,,,,,-     #"));
  if (VoffR <= 9) {
    Serial.println((String) "# Left offset = u/d   Now = " + VoffL + "       #");
    Serial.println((String) "# Rght offset = U/D   Now = " + VoffR + "       #");
  } else {
    Serial.println((String) "# Left offset = u/d   Now = " + VoffL + "      #");
    Serial.println((String) "# Rght offset = U/D#  Now = " + VoffR + "       #");
  }
  Serial.println(F("# Speed: [s]low [m]edium [f]ast     #"));
  Serial.println(F("# System Status = q                 #"));
  Serial.println(F("#####################################"));
  SerialCommand = 0;
}

//Variable offsets set from terminal input
void VoffLup() {
  if (VoffL < 15)  //Max + offset=15
  {
    VoffL = VoffL + 1;
    Serial.println(F("Left offset speed increased by 1"));
  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffLdn() {
//  if (VoffL > 0)  //Minimum - offset=0
//  {
    VoffL =  VoffL - 1;
//    Serial.println(F("Left offset speed decreased by 1"));
//  }

  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffRup()  //Max + offset=15
{
  if (VoffR < 15) {
    VoffR = VoffR + 1;
    Serial.println(F("Rght offset speed increased by 1"));
  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
  SerialCommand = 0;
}

void VoffRdn() {
// if (VoffR > 0)  //Minimum - offset=0
//  {
    VoffR = VoffR - 1;
//    Serial.println(F("Rght offset speed decreased by 1"));
//  }
  Serial.println((String) "Left Offset = " + VoffL);
  Serial.println((String) "Rght Offset = " + VoffR);
}
// End variable offsets

void System_Status_() {
  Serial.println(F("#####################################"));
  Serial.println(F("#           Project:Alpha           #"));
  Serial.println(F("#           System Status           #"));
  Serial.println(F("#                                   #"));
  if (VoffR <= 9) {
    Serial.println((String) "# Left motor offset is currently  " + VoffL + " #");
    Serial.println((String) "# Right motor offset is currently " + VoffR + " #");
  } else {
    Serial.println((String) "# Left motor offset is currently  " + VoffL + "#");
    Serial.println((String) "# Right motor offset is currently " + VoffR + "#");
  }
  Serial.println(F("#                                   #"));
//if (speed <= 99) {
//  Serial.println((String) "# Current Speed is set to   = " + speed + "%   #");
//} else {
//   Serial.println((String) "# Current Speed is set to   = " + speed + "%  #");
//  }
Serial.println((String) "# Left Speed is set to   = " + (speed + VoffL) + "%     #");
Serial.println((String) "# Right Speed is set to  = " + (speed + VoffR) + "%     #");
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#                                   #"));
  Serial.println(F("#####################################"));
}

//sounds

void speak() {
  int K = 2000;  //2000
  switch (random(1, 12)) {

    case 1: phrase1(); break;
    case 2: phrase2(); break;
    case 3: phrase3(); break;
    case 4:
      phrase1();
      phrase2();
      break;
    case 5:
      phrase2();
      phrase1();
      break;
    case 6:
      break;
    case 7:
      phrase1();
      phrase2();
      phrase1();
      break;
    case 8:
      phrase2();
      phrase1();
      phrase2();
      break;
    case 9:
      phrase1();
      phrase3();
      break;
    case 10:
  //    phrase3();
      phrase2();
      break;
    case 11:
      break;

  }
  for (int i = 0; i <= random(2, 11); i++)  //2, 10 how many tones
  {

    digitalWrite(ledPin, HIGH);
    tone(speakerPin, K + random(-1700, 2000));  //-1700, 2000
    delay(random(70, 170));
    digitalWrite(ledPin, LOW);
    noTone(speakerPin);
    delay(random(0, 30));
  }
  noTone(speakerPin);
  //delay(random(2000, 4000));
}


void phrase1() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = 0; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = 0; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
}
void phrase2() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = 0; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = 0; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 2));
  }
}
void phrase3() {

  int k = random(1000, 2000);
  digitalWrite(ledPin, HIGH);
  for (int i = -300; i <= random(100, 2000); i++) {

    tone(speakerPin, k + (i * 2));
    delay(random(.9, 2));
  }
  digitalWrite(ledPin, LOW);
  for (int i = -100; i <= random(100, 1000); i++) {

    tone(speakerPin, k + (-i * 2));
    delay(random(.9, 3));
  }
}

This will start the speed at 10 and ramp the speed up by 5 every 100 ms. so it would take 5 seconds to get from 10 to full speed. If you wanted you could control whether or not the speed ramps via a different serial command to set a flag that is stored as a global also. That way you could get more precise motion when you wanted.

1 Like

That is a more elegant way of doing it. With the help I had plus a large club I got it working. There is a lot of redundancy though. I think I am going to take what you gave and tuck it away for later, when I have a better understanding of what it is doing. Since this will probably be the base code for several projects, it will always be in a state of change. You don't want to know how long I spent yesterday trying to wrap my head around the millis delay, especially when my real issue was the serialcommand=0 line buggering it up.

What I learned:
When I receive a value on the serial port that matches a case, it will run that case. As long as nothing else comes in on the serial port that same case will be run over and over. When I used the serialcommand=0 I made it so the case would only run once. That was fine when I was sending a static instruction bus when I needed a loop to run the millis timer, it killed it. Thinking back, I added that command to make the serial monitor output look good and tidy. I kind of put all of the arduino stuff away 2 years or so ago and didn't touch it. LUCKILY, I made LOTS of comments in my code and I was able to pick it back up pretty quickly. I know it is pretty basic stuff, but it's my first program and it works :slight_smile:

Thanks to both of you for the help. And now, thanks to Ryan I have to figure out how this works because it is prettier..lol

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