im not sure where im meant to put this its my first time asking a question on the forum however i seriously need help with this as it is my major project and its due in a couple of weeks ok so
i am creating a blind spot sensor for my motorcycle using 2 led bar graphs paired with an arduino uno 2 ultrasonic distance sensors and 4 74hc595 shift registers.
it is supposed to light up more and more of the led bar graph the closer you are to an object however when i power the arduino the led bar graph blanks between full and 1 off full i cant see the error and that being said my code is terrible to read as i have been rushing through so i may have missed some logic somewhere if someone could give me some help that would be unbelievably amazing
im using if statements to decide what to send to the 74hc595s and i believe its getting stuck somewhere between the first if statements for both right and left
i have linked my code below
#define TRIGGER_PIN_LEFT A0 //sets the trigger pin for the left side sensor to the anolog 0 pin
#define ECHO_PIN_LEFT A1 //sets the echo pin for the left side sensor to the anolog 1 pin
#define TRIGGER_PIN_RIGHT A2//sets the trigger pin for the Right side sensor to the anolog 2 pin
#define ECHO_PIN_RIGHT A3//sets the echo pin for the Right side sensor to the anolog 1 pin
/* disabled due to time restraints
#define TRIGGER_PIN_REAR A4//sets the trigger pin for the Rear sensor to the anolog 4 pin
#define ECHO_PIN_REAR A5//sets the echo pin for the Rear sensor to the anolog 1 pin
*/
#define latchPin_left 3
#define dataPin_left 4
#define clockPin_left 2
#define latchPin_right 6
#define dataPin_right 7
#define clockPin_right 5
//(blanked out due to time restrictons)
//#define latchPin_rear 3
//#define dataPin_rear 4
//#define clockPin_rear 5
int waitFor = 20; //how long for the shift registers to wait before shifting the next bit
int stateright = 0; //int to control which stage the leds on the right hand side are up to
int stateleft = 0; //int to control which stage the leds on the left hand side are up to
long duration, distance, LeftSensor,RightSensor;
void setup() {
//pinmodes for left shift registers
pinMode (dataPin_left, OUTPUT);
pinMode (clockPin_left, OUTPUT);
pinMode (latchPin_left, OUTPUT);
//pinmodes for right shift registers
pinMode (dataPin_right, OUTPUT);
pinMode (clockPin_right, OUTPUT);
pinMode (latchPin_right, OUTPUT);
//pinmode for the left sensor
pinMode (TRIGGER_PIN_LEFT, OUTPUT);
pinMode (ECHO_PIN_LEFT, INPUT);
//pinmode for right sensor
pinMode (TRIGGER_PIN_RIGHT, OUTPUT);
pinMode (ECHO_PIN_RIGHT, INPUT);
BlankR();
BlankL();
// pinmodes for the rear sensor (disabled due to lack of time to complete)
// pinMode (TRIGGER_PIN_REAR, OUTPUT);
// pinMode (ECHO_PIN_REAR, INPUT);
}
void LeftLED (int second, int first) {
digitalWrite (latchPin_left, LOW);
shiftOut (dataPin_left, clockPin_left, LSBFIRST, first); // 9-18
shiftOut (dataPin_left, clockPin_left, LSBFIRST, second); // 1-8
digitalWrite (latchPin_left, HIGH);
delay (waitFor);
}
//code that sends the data
void RightLED (int Forth, int Third) {
digitalWrite (latchPin_right, LOW);
shiftOut (dataPin_right, clockPin_right, LSBFIRST, Third); // 9-16
shiftOut (dataPin_right, clockPin_right, LSBFIRST, Forth); // 1-8
digitalWrite (latchPin_right, HIGH);
delay (waitFor);
}
//function for turning all the leds on the Right hand side off
void BlankR (){
RightLED (0b00000000, 0b00000000);
}
//function for turning all the leds on the left hand side off
void BlankL (){
LeftLED (0b00000000, 0b00000000);
}
// sub function to turn on first led on the left side
void bewareL(){
LeftLED (0b00000000, 0b00100000);
}
// sub function to turn on first led on the right side
void bewareR(){
RightLED (0b00000000, 0b00100000);
}
void Left1 (){
LeftLED (0b00000000, 0b01100000);
}
void Right1 (){
RightLED (0b00000000, 0b01100000);
}
void left2 (){
LeftLED (0b00000001, 0b11100000);
}
void Right2 (){
RightLED (0b00000000, 0b11100000);
}
void left3 (){
LeftLED (0b00000001, 0b11100000);
}
void Right3 (){
RightLED (0b00000001, 0b11100000);
}
void left4 (){
LeftLED (0b00000011, 0b11100000);
}
void Right4 (){
RightLED (0b00000011, 0b11100000);
}
void left5 (){
LeftLED (0b00000111, 0b11100000);
}
void Right5 (){
RightLED (0b00000111, 0b11100000);
}
void left6 (){
LeftLED (0b00001111, 0b11100000);
}
void Right6 (){
RightLED (0b00001111, 0b11100000);
}
void left7 (){
LeftLED (0b00011111, 0b11100000);
}
void Right7 (){
RightLED (0b00011111, 0b11100000);
}
void Left8 (){
LeftLED (0b00111111, 0b11100000);
}
void Right8 (){
RightLED (0b00111111, 0b11100000);
}
void Left9 (){
LeftLED (0b01111111, 0b11100000);
}
void Right9 (){
RightLED (0b01111111, 0b11100000);
}
void fullLeft (){
LeftLED (0b11111111, 0b11100000);
}
void fullRight (){
RightLED (0b11111111, 0b11100000);
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}
void loop() {
SonarSensor(TRIGGER_PIN_LEFT, ECHO_PIN_LEFT);
LeftSensor = distance;
SonarSensor(TRIGGER_PIN_RIGHT, ECHO_PIN_RIGHT);
RightSensor = distance;
//SonarSensor(TRIGGER_PIN_REAR, ECHO_PIN_REAR);
//RearSensor = distance;
long duration, distance;
if(stateright != 0 && RightSensor <= 10 && RightSensor <= 0)
{
fullRight();
stateright = 0;
}
else if (stateright != 1 && RightSensor <= 15 && RightSensor <= 11)
{
Right9();
stateright = 1;
}
else if (stateright != 2 && RightSensor <= 20 && RightSensor <= 16)
{
Right8();
stateright = 2;
}
else if (stateright != 3 && RightSensor >= 25 && RightSensor <= 21)
{
Right7();
stateright = 3;
}
else if (stateright != 4 && RightSensor >= 30 && RightSensor <= 26)
{
Right6();
stateright = 4;
}
else if (stateright != 5 && RightSensor >= 35 && RightSensor <= 31)
{
Right5();
stateright = 5;
}
else if (stateright != 6 && RightSensor >= 40 && RightSensor <= 36)
{
Right4();
stateright = 6;
}
else if (stateright != 7 && RightSensor >= 45 && RightSensor <= 41)
{
Right3();
stateright = 7;
}
else if (stateright != 8 && RightSensor >= 50 && RightSensor <= 46)
{
Right2();
stateright = 8;
}
else if (stateright != 9 && RightSensor >= 55 && RightSensor <= 51)
{
Right1();
stateright = 9;
}
else if (stateright != 10 && RightSensor >= 70 && RightSensor <= 56)
{
bewareR();
stateright = 10;
}
else{
BlankR();
}
//start of if statments controlling left side
if(stateleft != 0 && LeftSensor <= 10 && LeftSensor <= 0)
{
BlankL():
fullLeft();
stateleft = 0;
}
else if (stateleft != 1 && LeftSensor <= 15 && LeftSensor <= 11)
{
BlankL():
Left9();
stateleft = 1;
}
else if (stateleft != 2 && LeftSensor <= 20 && LeftSensor <= 16)
{
Left8();
stateleft = 2;
}
else if (stateleft != 3 && LeftSensor >= 25 && LeftSensor <= 21)
{
left7();
stateleft = 3;
}
else if (stateleft != 4 && LeftSensor >= 30 && LeftSensor <= 26)
{
left6();
stateleft = 4;
}
else if (stateleft != 5 && LeftSensor >= 35 && LeftSensor <= 31)
{
left5();
stateleft = 5;
}
else if (stateleft != 6 && LeftSensor >= 40 && LeftSensor <= 36)
{
left4();
stateleft = 6;
}
else if (stateleft != 7 && LeftSensor >= 45 && LeftSensor <= 41)
{
left3();
stateleft = 7;
}
else if (stateleft != 8 && LeftSensor >= 50 && LeftSensor <= 46)
{
left2();
stateleft = 8;
}
else if (stateleft != 9 && LeftSensor >= 55 && LeftSensor <= 51)
{
Left1();
stateleft = 9;
}
else if (stateleft != 10 && LeftSensor >= 70 && LeftSensor <= 56)
{
bewareL();
stateright = 10;
}
else{
BlankL();
}
}
Christians_code.ino (6.6 KB)