Pages: [1] 2   Go Down
Author Topic: how to read analog value continuous with Serial.read()  (Read 1583 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

from this sketch i can read analog value each case for once value i think Serial.read read the oldest byte but i want to read from present byte like without its how can i setting this ?  

Code:
const int ledPin = 13;
void setup() {
 
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop() {
   if (Serial.available() > 0) {
  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);
    }
}  
« Last Edit: April 06, 2013, 04:56:56 am by mickeleckorn » Logged

UK
Offline Offline
Tesla Member
***
Karma: 125
Posts: 7225
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From a quick look at your program it seems to be arranged to read properly so I don't understand your question.

What do you mean by the "oldest" and "present" byte?

With your program there will only be a tiny delay between the reading and interpreting and I don't see how that can be avoided.

Perhaps you could explain what you would like to happen.
Logged

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


from this program , e.g. when i type 'a' and send in serial monitor this monitoring one value (not continuous) and i try to use for , while but its monitoring one value repeat not change when i change the value (from Potentiometer)

i want its to read value continuous not one value .

 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

from this sketch i can read analog value each case for once value i think Serial.read read the oldest byte but i want to read from present byte like without its how can i setting this ? 

How to use this forum
Logged


UK
Offline Offline
Tesla Member
***
Karma: 125
Posts: 7225
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect English is not your first language and I am doing my best to understand your question - but I can't.

As it is written the program will send back the value from a single read whenever you press the appropriate letter. And this will be the "present" value of that sensor. If you want it to send back several values you will have to change the program so that there is a loop of reads associated with each key press. If you want it to send back a continuous stream of values you will have to have a program on your PC to use the data. If you explain how you intend to use the data it may help to understand  what you are trying to do.

However I'm not even sure that that's what you want because you seem to be saying that even with a single read it is not showing different values when you move the potentiometer. If that is the problem there may be several reasons including possible wiring errors. You will need to give us a diagram of your circuit.

...R



from this program , e.g. when i type 'a' and send in serial monitor this monitoring one value (not continuous) and i try to use for , while but its monitoring one value repeat not change when i change the value (from Potentiometer)

i want its to read value continuous not one value .

 
Logged

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

Thanks Robin.
Sorry for bad english

when i don't read from serial.read this program work well (use analogRead)

i think this problem is in serial.read can receive only one data , when i rotate the potentiometer and send 'a' again value is changed but i want to send 'a' once and program monitoring continuous likes to use analogRead.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As it is currently written your program will only read the analogue inputs whilst there is something in the serial buffer.
Is that how you want it to work ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

i want serial monitor to monitoring continuous like a loop , not single value.
Logged

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

Code:
case 'a':
     while(Serial.available() != 0);{
       Serial.println(sensorValue1);
  }
       break;
Logged

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

 Thanks , Grumpy_Mike

but this not work ,program still monitoring single value.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i want serial monitor to monitoring continuous like a loop , not single value.
Can you please describe what the program should do ?  That might help understand exactly what you mean.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


http://www.gammon.com.au/serial
Logged


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

sorry again for my bad english skill

i want my program to receive analog value from analog pin A0 A1 A2 selected by type and send letters in serial monitor.

now i use switch case for my program , but this serial monitor display single value, i want this to monitoring value continuous like in a loop.



Logged

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

Why do you keep your requirements secret?
Reply #8 showed you how why are you ignoring it?

Quote
sorry again for my bad english skill
They are probably not good enough for you to understand any answer.
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3031
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem with your program,  is that it is only going to read the analog inputs,   when there is a character from
you arriving at the Arduino's serial input.

So unless you send characters to the arduino very often,  you won't get a reading very often.

Logged

Pages: [1] 2   Go Up
Jump to: