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