Hi folks.
I'm having some issues with using boolean logic operators in my latest sketch, for an RC car starting gantry.
The idea is that the ultrasonic sensors mounted above two lanes will automatically trigger a starting light sequence when they both return a value less than a predefined value (in this case 100).
I have gotten most of it about where I want it but the sketch seems to be stuck running the 'triggered' sequence even when the logic conditions for that are not met.
I have been googling my butt off and have yet to find the problem - so thought if I posted my code here someone may be kind enough to look over it and see if what I have gotten wrong is simple or if it's totally borked.
// Drew's drift start gantry code
#include <Adafruit_NeoPixel.h> //Include NeoPixel Library
const int sensorOneTrigPin = 8; //Define pin for sensorOne trig pin
const int sensorOneEchoPin = 9; //Define pin for sensorTwo echo pin
const int sensorTwoTrigPin = 11; //Define pin for sensorTwo trig pin
const int sensorTwoEchoPin = 12; //Define pin for sensorTwo echo pin
long duration1; //Variable for duration of sensorOne
int distance1; //Variable for distance of SensorOne
long duration2; //Variable for duration of sensorTwo
int distance2; //Variable for distance of sensorTwo
float MaximumBrightness = 255; //Variable for maximum KLED brightness
float SpeedFactor = 0.008; //Speed of breathing LEDs
float StepDelay = 5; //ms for a step delay on the lights
bool sensorOneReady; //Boolean variable for sensor one ready status
bool sensorTwoReady; //Boolean variable for sensor two ready status
#define LED_PIN 2 //Which pin on the Arduino is connected to the NeoPixels?
#define LED_COUNT 21 //How many NeoPixels are attached to the Arduino?
//Breathing LEDs
int MinBrightness = 20; //value 0-255
int MaxBrightness = 100; //value 0-255
int numLoops1 = 1; //Variable for looping
int numLoops2 = 5; //Variable for looping
//int numLoops3 = 5;
//int numLoops4 = 3; //Add new integer and value for more color loops if needed.
int fadeInWait = 1; //Lighting up speed, steps.
int fadeOutWait = 12; //Dimming speed, steps.
//Declare NeoPixel strip object
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);//Names the LEDs as "strip"
//SYNTAX REFERENCE ++++++++++++++++++++++
//Argument 1 (LED_COUNT)= Number of pixels in NeoPixel strip
//Argument 2 (LED_PIN)= Arduino pin number (most are valid)
//Argument 3 (NEO_GRB + NEO_KHZ800) = Pixel type flags - leave alone
uint32_t purple=strip.Color(255,0,255); //Define 32 bit packed colors
uint32_t red=strip.Color(255,0,0);
uint32_t green=strip.Color(0,255,0);
uint32_t yellow=strip.Color (255,230,0);
//DEFINING MY FUNCTIONS ++++++++++++++++++++++
//ULTRASONIC SETUP ++++++++++++++++++++++
void activateSensors(){
digitalWrite (sensorOneTrigPin, LOW); //Clears TrigPinOne
delayMicroseconds(2);
digitalWrite (sensorOneTrigPin, HIGH); //Sets trigPinOne to HIGH for 10 Microseconds
delayMicroseconds(2);
digitalWrite (sensorOneTrigPin, LOW); //Sets trigPinOne back to LOW
delayMicroseconds(2);
duration1 = pulseIn(sensorOneEchoPin, HIGH); //Reads echoPin1 and returns microseconds
Serial.print("Sensor 1 : "); //Prints distance1 variable to serial monitor
Serial.println(distance1);
digitalWrite (sensorTwoTrigPin, LOW); //Clears TrigPinTwo
delayMicroseconds(2);
digitalWrite (sensorTwoTrigPin, HIGH); //Sets trigPinTwo to HIGH for 10 Microseconds
delayMicroseconds(2);
digitalWrite (sensorTwoTrigPin, LOW); //Sets trigPinTwo back to LOW
delayMicroseconds(2);
duration2 = pulseIn(sensorTwoEchoPin, HIGH); //Reads echoPin2 and returns microseconds
Serial.print("Sensor 2 : "); //Prints distance2 variable to serial monitor
Serial.println(distance2);
distance1=duration1*0.034/2; //Defines distance1 using algorithm
distance2=duration2*0.034/2; //Defines distance2 using algorithm
}
//START SEQUENCE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void startSequence() {
strip.setBrightness(25); //Set LED brightness in %
strip.fill(red); //Set all LEDs to red
strip.show(); //Send red colour to all LEDs
delay (1000); //Wait a second
strip.fill(yellow, 0, 7); //Set first LEDS to yellow
strip.show(); //Send yellow to first LEDs
delay (1000); //Wait a second
strip.fill(yellow, 7, 7); //Set second LEDs to yellow
strip.show(); //Send yellow to second LEDs
delay (1000); //Wait a second
strip.fill(yellow, 14, 7); //Set third LEDs to yellow
strip.show(); //Send yellow to third LEDs
delay(1000); //Wait a second
strip.fill(green); //Set all LEDs to green
delay(1000); //Wait a second
strip.show(); //Send green to all LEDs
delay(5000); //Wait 5 seconds
strip.fill(yellow); //Set all LEDs to purple
strip.show(); //Send colours to LEDs
delay(1000); //Wait 5 seconds
strip.clear(); //Turn off all LEDs
}
//BREATHING SEQUENCE +++++++++++++++++++++++++++++++++++++++++++++++++
void rgbBreathe(uint32_t c, uint8_t x, uint8_t y) {
for (int j = 0; j < x; j++) {
for (uint8_t b = MinBrightness; b < MaxBrightness; b++) {
strip.setBrightness(b * 255 / 255);
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(fadeInWait);
}
strip.setBrightness(MaxBrightness * 255 / 255);
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(y);
}
for (uint8_t b = MaxBrightness; b > MinBrightness; b--) {
strip.setBrightness(b * 255 / 255);
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(fadeOutWait);
}
}
}
//IDLE SEQUENCE +++++++++++++++++++++++++++++++++++++++++++++++++
void idleSequence() {
//+++++ SYNTAX REFERENCE +++++++
// rbgBreathe(stripColor(RGB1, RGB2, RGB3), loops, delay in ms);
rgbBreathe(strip.Color(255, 0, 255), numLoops1, 0); //Purple
//rgbBreathe(strip.Color(0, 255, 0), numLoops1, 0); //Green
//rgbBreathe(strip.Color(0, 0, 255), numLoops1, 0); //Blue
//rgbBreathe(strip.Color(255, 255, 255), numLoops1, 0); //White
//duplicate for more colors.
}
void setup() {
Serial.begin(9600); //Open the serial port at 9600 bps:
pinMode (sensorOneEchoPin, INPUT); //Set sensorOneEchoPin as an INPUT
pinMode (sensorOneTrigPin, OUTPUT); //Set sensorOneTrigPin as an OUTPUT
pinMode (sensorTwoEchoPin, INPUT); //Set sensorTwoEchoPin as an INPUT
pinMode (sensorTwoTrigPin, OUTPUT); //Set sensorTwoEchoPin as an OUTPUT
strip.begin(); //Start the strip
strip.setBrightness(85); //Lower brightness and save eyeballs!
strip.show(); //Initialize all pixels to 'off'
sensorOneReady = false;
sensorTwoReady = false;
}
//LOOP ++++++++++++++++++++++++
void loop() {
activateSensors(); //Run activateSensor
//Logic+++++++++++++++++++++++
if (distance1 < 100) //If variable "distance1" is less than 100
{
sensorOneReady = true; //Set variable "sensorOneReady" to "True"
}
else //If variable "distance1" is NOT less than 100
{
sensorOneReady = false; //Set variable sensorOneReady to "False"
}
if (distance2 < 100) //If variable "distance2" is less than 100
{
sensorTwoReady = true; //Set variable "sensorTwoReady" to "True"
}
else //If variable "distance2" is NOT less than 100
{
sensorTwoReady = false; //Set variable "sensorTwoReady" to "False"
}
if ((sensorOneReady = true) and (sensorTwoReady = true)) //If values of variables "sensorOneReady" and "sensorTwoReady" are both "True"
{
startSequence(); //Call function "startSequence"
}
else //If values of variables "sensorOneReady" and "sensorTwoReady" are NOT both "True"
{
idleSequence(); //Call function "idleSequence"
}
}
//END OF CODE
Thanks in advance,
Drew.