I am a novice when it comes to coding Arduino, however I do love the communities and the help online for whatever project you may find. I am in the process of making a raise and lower action with a servo using Bluetooth and an android app (which I have working as well as with VarSpeedServo library for some speed control). I did get this working and in the process learned a bit of android code as well. I also successfully added a LED that does read the sent data and turns on when it is told to.
The problem I face now is to add a millis flash/strobe LED setup code (which I found online and learned from) to do the same, I really don’t like the idea of using delay() at all. I currently added a push button (which simply gets pressed by the arm in lowered position (0)) and got that code working (could probably be simplified but working is good enough for me since I am a novice lol). I have tried various ways to get the millis code snippet to act on the data sent by the bluetooth serial instead of a pushbutton… but I cannot get it to work. It seems to me that the timer for millis() is paused in the loop somehow as I lower the arm it only runs part of the code, lights one led and then if I raise the arm it goes out, next turn I lower the arm the 2nd led in the millis snippet comes on… I have tried various ways to get this to work but I am at a loss here… I am sure it is something pretty basic I am missing and I could just keep using the switch but I like things to be simple if possible Any help would be appreciated.
Here is the current code I am using (sorry for the horrible state of it as I learn as I try):
#include <VarSpeedServo.h> // Variable speed servo library
#include <SoftwareSerial.h> // Include library for software controlled/assigned serial pins
//#define bluetooth Serial // Define bluetooth to use Arduino TX/RX pins through hardware setup
#define RED 0x0
#define RED2 0x1
byte whichLED = RED;
VarSpeedServo myservo; // variable servo speed library defined to myservo
const int BtnPin = 2; // Arm button pin
const int MvPin = 3; // Mainviewer LED pin
const int RedLed1 = 4; // First Red Blink/Strobe LED Pin
const int RedLed2 = 5; // Second Red Blink/Strobe LED Pin
const int bluetoothTx = 10; // Software assigned pin for TX channel
const int bluetoothRx = 11; // Software assigned pin for RX channel
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); // Define software controlled TX/RX pins
// Variables that can change
int buttonState = 0; // Variable for reading the Arm button status
int pos = 0; // Servo position to turn on Mainviewer LED
// State variables for the LEDs
byte Red1_State = LOW;
byte Red2_State = LOW;
// Some delay values to change flashing/strobe behavior
unsigned long switchDelay = 1000;
unsigned long strobeDelay = 10;
// Seed the initial wait for the strobe effect
unsigned long strobeWait = strobeDelay;
// Variable to see when we should swtich LEDs
unsigned long waitUntilSwitch = switchDelay; // seed initial wait
void setup()
{
pinMode(RedLed1, OUTPUT); // Define pinmode for Red LED 1
pinMode(RedLed2, OUTPUT); // Define pinmode for Red LED 2
pinMode(BtnPin, INPUT); // Define pinmode for Arm Button
pinMode(MvPin, OUTPUT); // Define pinmode for Blue/White LED for mainviewer
myservo.attach(9); // Arduino pin for servo control
Serial.begin(9600); // Setup usb serial connection to computer
bluetooth.begin(9600); // Setup Bluetooth serial connection to android
}
void loop()
{
// Read from bluetooth received data to move servo
if(bluetooth.available()> 0 )
{
int servopos = bluetooth.read(); // get the received data
Serial.println(servopos); // print to serial USB monitor
myservo.write(servopos,50,true); // move the servo to the received data location
// LED Control depending if arm is up or down
if (servopos == pos)
{
// read servo pos and turn Blue LED on:
digitalWrite(MvPin, HIGH); // Mainviewer LED
}
else
{
// read servo pos and turn Blue LED off:
digitalWrite(MvPin, LOW); // Mainviewer LED
}
}
{
//Red Blink/Strobe LED's controlled by a HIGH state button
// check if the pushbutton is pressed.
buttonState = digitalRead(BtnPin);
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
{//LED Flash or Strobe Start
digitalWrite(RedLed1, Red1_State); // each iteration of loop() will set the IO pins,
digitalWrite(RedLed2, Red2_State); // even if they don't change, that's okay
// Toggle back and forth between the two LEDs
if ((long)(millis() - waitUntilSwitch)>=0) {
// time is up!
Red1_State = LOW;
Red2_State = LOW;
whichLED = !whichLED; // toggle LED to strobe
waitUntilSwitch += switchDelay;
}
// Create the stobing effect
if ((long)(millis() - strobeWait)>=0) {
if (whichLED == RED)
Red1_State = !Red1_State;
if (whichLED == RED2)
Red2_State = !Red2_State;
strobeWait += strobeDelay;
}
}//LED Flash or Strobe End
else
{
// turn LED off:
digitalWrite(RedLed1, LOW);
digitalWrite(RedLed2, LOW);
}
}
}