Need help ASAP School Major assessment

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)

Why all the separate functions for turning on a series of LEDs instead of one function with parameters ?

else if (stateright != 5 && RightSensor >= 35 && RightSensor <= 31)

You need to sit and think about these. What are the chances that RightSensor, or any other number for that matter, is greater than 35 and at the same time is less than 31.

to be honest im terrible at coding i wouldn't know the first place to start with making that into one function

NightmaresDawn:
to be honest im terrible at coding i wouldn't know the first place to start with making that into one function

So how have you got to the stage of having a major assignment without learning the basics?

...R

i wanted to challenge myself and i did this during a lot of my exams my mind wasn't exactly have it in me to do anything other then that if no one here can help me or see where i messed up apart from everywhere i will redo it and try something different

What kind of ultrasonic sensors are you using? The ones typically used with Arduinos do not have the distance required for motorcycle blind spot duty.

NightmaresDawn:
i wanted to challenge myself

There is a big difference between setting yourself a challenge and setting yourself an impossible task.

You might be capable of becoming the next Usain Bolt. But you would not plan to match his time on your very first race.

If you don't know the basics of programming your complex project is as attainable as an Olympic medal.

...R

Don't think about it in terms of code. Look at the question I asked in #2 and think about it in terms of MATH. The answer should jump out at you at that point.

Thank you delta_G and UKHeliBob I have changed up the coding of it and written it a lot better and it is Working now Robin2 thanks for the criticism however I do like the challenge and have been able to complete it well before my due date thanks everyone

NightmaresDawn:
to be honest im terrible at coding i wouldn't know the first place to start with making that into one function

In the future, you would be doing everyone helping you a huge service just by using punctuation. Notice that everyone else is.

DangerToMyself:
In the future, you would be doing everyone helping you a huge service just by using punctuation. Notice that everyone else is.

It would certainly make things easier to read. And goes a long way towards making a poster look less like an idiot. Sometimes we skip over the ones that it looks like we can't help and when they look like they can't construct a simple sentence you have to wonder how they think they'll ever write code. The compiler has a much higher standard of grammar than you or I or even the most evil high school English teacher.

But these kids these days. They just don't care. They feel like everyone else owes them something for nothing. "Why should I worry about my grammar, you just give me the free answers I want right now!" No respect for anyone but themselves. It's a pity I think. Then they hit the real world and wonder why they can't get jobs. You know I've actually culled a resume the other day that had the word LOL in it after a sentence I guess the writer thought was funny. Who puts that in their cover letter?

NightmaresDawn:
Robin2 thanks for the criticism however I do like the challenge and have been able to complete it well before my due date

Then you were being untruthful when you said "to be honest im terrible at coding ". I can only work with the data that is put before me.

Glad to hear it is working.

...R