Pages: 1 [2]   Go Down
Author Topic: how to read analog value continuous with Serial.read()  (Read 1092 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Online Online
Faraday Member
**
Karma: 51
Posts: 3436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
    }

Logged

New Jersey
Online Online
Faraday Member
**
Karma: 51
Posts: 3436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
    }


 
   
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 51
Posts: 3436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you have another go at explaining what you want it to do?

That looks like it will keep reporting one sensor value (which may vary) until you tell it to report another by sending another character.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i want my program to receive analog value from analog pin A0 A1 A2 selected by sending character in serial monitor.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make incomingbyte static?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all i'm finish my code, serial monitor can read analog value continuous now.
Code:
 const int ledPin = 13;
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(9, OUTPUT);
}
int incomingByte;
void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  int sensorValue3 = analogRead(A2);
  if (Serial.available() ) {
  // 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;
  }
    delay(300);
    }

  
  
    
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31555
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but this not work ,program still monitoring single value.
Have you set the serial monitor to not send any new line characters?
Select "no line ending" from the drop down at the bottom corner of the window.
Logged

Pages: 1 [2]   Go Up
Jump to: