Hi there, I need more help with my code as I'm trying to implement it with bluetooth capabilities.
I've created an app that sends a value via serial and I want that value to tell the Arduino to move the servo forward in this case by 160 degrees, I successfully achieved that with this code:
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <VarSpeedServo.h> // servo library
VarSpeedServo Left; // Left servo name
VarSpeedServo Right; // Right servo name
int bluetoothTx = 2; // bluetooth tx to 10 pin
int bluetoothRx = 3; // bluetooth rx to 11 pin
const int LeftServo = 7; //Left Servo Pin
const int RightServo = 8; //Right Servo Pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Left.attach(LeftServo); // attach servo signal wire to pin 9
Right.attach(RightServo);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()> 0 ) // receive number from bluetooth
{
if (bluetooth.read()==160) { // read number sent by the app
Right.write(160, 100, true); //writes the steps
delay(100);
Left.write(160, 100, true);
}
}
}
And I'm now trying to fit it in my code, but I'm getting stuck since I have an if and an else if but nothing else seems to work if i try to add extra conditionals, all I wanted to do is tell the Arduino that if there is a value of 160 or the digital read is high set the servo to go to 160.
//SETTINGS
//DELAY
int T1 = 500; // Forward delay in ms.
int T2 = 500; // Reverse delay in ms
//BUTTON SETTINGS
int InchStepsL = 5; // Inch rotation degrees 0-160
int InchStepsR = 5; // Inch rotation degrees 0-160
int HoldButton = 3000; // Hold button time for instant full rotation in ms
//SPEED
int Speed = 50; // Rotation speed 1-100 in ms
////SAFE ZONE
//
//int SafeZone = 100; // Safe zone position degrees 0-160
// // after power surge
//CODE
//#include <Servo.h>
#include <VarSpeedServo.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
int bluetoothTx = 2; // bluetooth tx to 10 pin
int bluetoothRx = 3; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
VarSpeedServo Left; //Left Servo Name
VarSpeedServo Right; //Right Servo Name
const int LeftServo = 7; //Left Servo Pin
const int RightServo = 8; //Right Servo Pin
const int Forward = 4; //Forward Button Pin
const int Reverse = 6; //Reverse Button Pin
const int LOCK = 8; //Lock Pin
const int LeftPot = A0; //Left Pot Pin
const int RightPot = A1; //Left Pot Pin
int LeftAngle = 0;
int RightAngle = 0;
void setup() {
Serial.begin(9600);
pinMode(Forward, INPUT_PULLUP);
pinMode(Reverse, INPUT_PULLUP);
pinMode(LOCK, OUTPUT);
bluetooth.begin(9600);
// LeftAngle = analogRead(LeftPot);
// LeftAngle = map(LeftAngle, 1028, 0, 160, 0);
// RightAngle = analogRead(RightPot);
// RightAngle = map(RightAngle, 1028, 0, 160, 0);
Left.attach(LeftServo);
Right.attach(RightServo);
Left.write(LeftAngle);
Right.write(RightAngle);
delay(1000);
// Left.detach();
// Right.detach();
digitalWrite(LOCK, HIGH);
}
void loop() {
if (bluetooth.read() == 160) {
Right.write(160, 100, true);
delay(100);
Left.write(160, 100, true);
}
if (digitalRead(Forward) == HIGH) {
Serial.println("FORWARD");
Serial.println("");
unsigned long StartF = millis();
while (digitalRead(Forward) == HIGH) {
Serial.println("FORWARD");
Serial.println("");
if (millis() - StartF < HoldButton) {
digitalWrite(LOCK, LOW);
Serial.println("LESS THAN 3 SECONDS(FORWARD)");
Serial.println("");
RightAngle += InchStepsR;
LeftAngle += InchStepsL;
if (RightAngle >= 160) {
RightAngle = 160;
}
if (LeftAngle >= 160) {
RightAngle = 160;
}
Right.attach(RightServo);
Left.attach(LeftServo);
Right.write(RightAngle, Speed, true);
delay(T2);
Left.write(LeftAngle, Speed, true);
delay(500);
Left.detach();
Right.detach();
digitalWrite(LOCK, HIGH);
}
else if (millis() - StartF >= HoldButton) {
digitalWrite(LOCK, LOW);
Serial.println("MORE THAN 3 SECONDS(FORWARD)");
Serial.println("");
RightAngle = 160;
LeftAngle = 160;
Right.attach(RightServo);
Left.attach(LeftServo);
Right.write(RightAngle, Speed, true);
delay(T2);
Left.write(LeftAngle, Speed, true);
delay(1000);
Left.detach();
Right.detach();
digitalWrite(LOCK, HIGH);
}
}
}
if (digitalRead(Reverse) == HIGH) {
Serial.println("REVERSE");
Serial.println("");
unsigned long StartR = millis();
while (digitalRead(Reverse) == HIGH) {
Serial.println("REVERSE");
Serial.println("");
if (millis() - StartR < HoldButton) {
digitalWrite(LOCK, LOW);
Serial.println("LESS THAN 3 SECONDS(REVERSE)");
Serial.println("");
RightAngle -= InchStepsR;
LeftAngle -= InchStepsL;
if (RightAngle <= 0) {
RightAngle = 0;
}
if (LeftAngle <= 0) {
LeftAngle = 0;
}
Left.attach(LeftServo);
Right.attach(RightServo);
Left.write(LeftAngle, Speed, true);
delay(T1);
Right.write(RightAngle, Speed, true);
delay(500);
Left.detach();
Right.detach();
digitalWrite(LOCK, HIGH);
}
else if (millis() - StartR >= HoldButton) {
digitalWrite(LOCK, LOW);
Serial.println("MORE THAN 3 SECONDS(REVERSE)");
Serial.println("");
RightAngle = 0;
LeftAngle = 0;
Left.attach(LeftServo);
Right.attach(RightServo);
Left.write(LeftAngle, Speed, true);
delay(T1);
Right.write(RightAngle, Speed, true);
delay(1000);
Left.detach();
Right.detach();
digitalWrite(LOCK, HIGH);
}
}
}
}
Obviously how the code is setup now it won't work I just left the two if's at the start of the loops as I'm not sure how to fix it, I've tried to put and, or and to put one if, one else and one else if but it doesn't work, so what do you guys think?
Thanks