serial.read from an ultrasonic sensor and light sensor

Hi

Im struggling with a project im trying, Im creating a traffic light system with ultra sonic and light sensors.
Iv coded it up with serial.avaliable and serial.read() and i cant figure out why its not working, ill copy code below

Thanks

#include <HCSR04.h>
//variable values
int red = 10;
int yellow = 9;
int green = 8;
int buzzer = 7;
int sensorPin = A0;
unsigned int sensorValue = 0;
float voltage;
const int ECHOPIN= 12;
const int TRIGPIN= 13;

char inVal; 


//String inString = "";

void setup() {                

 pinMode(red, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(ECHOPIN, INPUT);
 pinMode(TRIGPIN, OUTPUT);
 pinMode (buzzer, OUTPUT);
 Serial.begin(9600);
 

}



void loop() {

  if (Serial.available()>0)
  {
    inVal=Serial.read();
    switch(inVal)
    
    {
       case '0': //REGULAR MODE
       for (;;)
      {
        digitalWrite(red, HIGH);
        delay(2000);
        digitalWrite(red, LOW);
        digitalWrite(yellow, HIGH);
        delay(1000);
        digitalWrite(yellow, LOW);
        digitalWrite(green, HIGH);
        delay(2000);
        digitalWrite(green, LOW);      
        digitalWrite(yellow, HIGH);
        delay(1000);
        digitalWrite(yellow, LOW); 
      }
      
      case '1': //PEDESTRIAN MODE
      {
        for(;;)
       {
      
       digitalWrite(TRIGPIN, LOW);
       delayMicroseconds(2);
        digitalWrite(TRIGPIN,HIGH);
        delayMicroseconds(10);
        digitalWrite(TRIGPIN,LOW); 
        float distance = pulseIn(ECHOPIN, HIGH);
        distance = distance/58;
        

        
        while(distance > 10)
        {

          digitalWrite(red, HIGH);
          delay(2000);
          digitalWrite(red, LOW);
          digitalWrite(yellow, HIGH);
          delay(1000);
          digitalWrite(yellow, LOW);
          digitalWrite(green, HIGH);
          delay(2000);
          digitalWrite(green, LOW);      
          digitalWrite(yellow, HIGH);
          delay(1000);
          digitalWrite(yellow, LOW);
          break; 
        }
        
        while(distance < 10)
        {   
          int i;    
          for (i = 0; i < 10; i++)
          {
          tone(buzzer, 2000, 500);
          digitalWrite(yellow, HIGH);
          delay(100);
          tone(buzzer, 3000, 500);
          digitalWrite(yellow, LOW);
          delay(100);
          }
          break;
        }
       }
          case '2': //NIGHT MODE
          for(;;)
          {
  
          sensorValue = analogRead(sensorPin);    
          voltage = sensorValue * (5000.0 / 1024.0);
          Serial.print("Sensor Output: ");
          Serial.println(sensorValue);
          Serial.print("Voltage (mv): ");
          Serial.println(voltage);
          Serial.println();
          delay(1000);

          digitalWrite(TRIGPIN, LOW);
          delayMicroseconds(2);
          digitalWrite(TRIGPIN,HIGH);
          delayMicroseconds(10);
          digitalWrite(TRIGPIN,LOW); 
          float distance = pulseIn(ECHOPIN, HIGH);
          distance = distance/58;

         if (sensorValue < 200) //given a sensor threshhold
         {
          digitalWrite(red, HIGH); //turns on red light
          digitalWrite(green, LOW); //turns off green light
          

          if (distance < 10)
          {
            digitalWrite(green, HIGH); //turns on green light
            digitalWrite(red, LOW); //turns off red light
          }
         }
          }
      }
    }
  }
}

It would help if you tell us what "not working" means. What does the code actually do? How is that different than what you want.

I suggest that you get rid of the

for(; ; ;)

stuff and let loop() do the looping. And move the switch case out of the Serial reading loop.

sorry i realised i didnt actually explain it hahaha
basically when i Input (ie case 0 or case 1 or case 2) I have to reupload every time

is there a way where i dont need to re-upload every time i want to change case?

and also i added the for ( ; ; ) because if it wasnt it only did each thing once, it wouldnt loop each case when selected

The for loops are the cause of needing to reset to continue. They are endless loops with no way to exit except a reset.

There is nothing wrong with calling a case over and over.

awesome cheers, ill have a look see what I can do to change it up so it loops a case untill another case is selected :slight_smile:

Your Title is misleading. The Serial data is not coming from the ultrasonic sensor or from the light sensor.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R