Go Down

Topic: how to read analog value continuous with Serial.read() (Read 1 time) previous topic - next topic

mickeleckorn

i'm not ignoring it but this not work ,program still monitoring single value same the old.

wildbill

I'm not sure what you're trying to do, but is it: read a character from serial and then keep sending the analog reading it selected?

If so, you need to shorten the first if statement so that all it does is check for serial.available and read a character if one is there.

All the rest of your code needs to run every time loop runs. As you have it, all your code is controlled by serial.avaialble. If there's no serial input, nothing else happens.

mickeleckorn

Thanks,  but after i remove the first if statement serial monitor displayed single analogvalue same the old.

Code: [Select]
const int ledPin = 13;
void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop() {
   
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  int sensorValue3 = analogRead(A2);
  int light_sensitivity = 500;
  int incomingByte;
 
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
 
  switch (incomingByte) {
   
    case 'a':
       Serial.println(sensorValue1);
       break;
    case 'b':
       Serial.println(sensorValue2);
       break;
    case 'c':
       Serial.println(sensorValue3);
       break;
    case 'H':
       digitalWrite(ledPin, HIGH);
       break;
    case 'L':
       digitalWrite(ledPin, LOW);
       break;
  }
    if (sensorValue3 < light_sensitivity)
      {
        digitalWrite(9, HIGH);
      }
     else
      {
        digitalWrite(9, LOW);
       
      }
      delay(300);
    }



wildbill

Code: [Select]

    // read the oldest byte in the serial buffer:
if(Serial.available())
{
    incomingByte = Serial.read();
}

mickeleckorn

thanks again , but this monitoring single value same the old.

Code: [Select]
const int ledPin = 13;
void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop() {
   
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  int sensorValue3 = analogRead(A2);
  int light_sensitivity = 500;
  int incomingByte;
 
    // read the oldest byte in the serial buffer:
    if(Serial.available()){
    incomingByte = Serial.read();
    }
  switch (incomingByte) {
   
    case 'a':
       Serial.println(sensorValue1);
       break;
    case 'b':
       Serial.println(sensorValue2);
       break;
    case 'c':
       Serial.println(sensorValue3);
       break;
    case 'H':
       digitalWrite(ledPin, HIGH);
       break;
    case 'L':
       digitalWrite(ledPin, LOW);
       break;
  }
    if (sensorValue3 < light_sensitivity)
      {
        digitalWrite(9, HIGH);
      }
     else
      {
        digitalWrite(9, LOW);
       
      }
      delay(300);
    }


 
   

Go Up