Pages: 1 [2]   Go Down
Author Topic: reading the serial port  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9560
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a technical forum, so questions need to be in a technical format. That being said, below is some very simple code that might get you started.

Code:
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;


void setup() {
Serial.begin(9600);
        pinMode(13, OUTPUT);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(2); 
    char c = Serial.read();
        readString += c;
        }
       
      if (readString.length() >0) {
      Serial.println(readString);
     
    if (readString == "304") {
digitalWrite(13, HIGH);
        Serial.println("Led On");
    }
   if (readString == "303") {
digitalWrite(13, LOW);
        Serial.println("Led Off");
    }
        readString="";
   }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

Quote
looking for Serial.available() == 0   is not a good idea.
@michinyon: Read the code again.
Code:
  while (Serial.available() == 0);


OP, this question comes up here in different forms probably a couple of times a month.

Do some of your own legwork.
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
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[rant]

Mortified!

This is indicative of the selfish, instant gratification, vague, rude, posts that seem to be forever increasing across the net.
Well done you guys that spoke out!

We all love to help, but, I at least, like to feel that the person receiving help will somehow meet me half way, do some research, and improve their lot and maybe pass on some of that new knowledge. Not, as I feel will happen here, take what they can, use it until bored and then demand more ways to fulfil their ill conceived plans.

Many people on this forum are keen to give up their time and freely spout code until the cows come home, personally I like to point in the right direction, try to get them to understand what it is that they are doing and why. That way they get more fulfilment.

I suppose that the Arduino is it's own worst enemy, it appears to de-skill to such a degree that looks like putting Lego together - which is cool - but belays the need for at least a meagre understanding.

But what really annoys me is the rudeness of these people. This guy should be ashamed of himself - he is basically mugging people and spitting in their face while he does it!

Everyone should take a stand against this kind of behaviour.

[/rant]
Logged

For whom does the clock pulse? It pulses for you!

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

Quote
, like to feel that the person receiving help will somehow meet me half way
Amen.
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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4803
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We're not supposed to do people's homework either.

I don't mind helping someone learn as long as they do learn. Otherwise it's unpaid work since yes I consider a person who has really learned makes my world a bit better.

Tyguy: serial data you get from the serial monitor is ASCII Encoded Text. The character '0' has a value of 48 but you can use '0' in code.

You can make this into a new sketch and change serInput as you need until it makes sense. Leave loop empty.

Code:
void setup( void )
{
  Serial.begin( 9600 );

  char serInput = '5';
  byte value;

  if (( serInput >= '0' ) && ( serInput <= '9' ))
  {
    value = serInput - '0';
    Serial.println( "Value is " );
    Serial.println( value, DEC );
  }
  else
  {
    Serial.println( "Input is not a digit." );
  }
}

And for your own sake, learn about C string arrays and avoid depending on C++ Strings when coding on limited RAM machines like the UNO. String variables are wasteful and unnecessarily hide what they do and how they do it. The 'advantages' they give in some respects are matched by the disadvantages that come with learning to depend on every black box.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

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

thank you for your help. some of you at least.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4803
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, you don't need anyone's attitude because you already got one of your own even if you think not. Yes, people asking for code help are expected to co-operate and put in real effort. Just repeating 'gimmie' tends to turn the help you wanted away. And besides, I'm not at my best up at 4AM because of a toothache. Note that the other members have lives as well. They are not asking You for help. Think about that before being affronted and think of your own part as well.

For your own good you need to learn this stuff and frankly just dropping code on you is only going to lead to more questions about the parts you hadn't guessed mattered. If what you get isn't exactly what you need, how are you going to modify it if you don't know how most of it works?

In my 15 years of using forums there's always 1 commin Rule: SEARCH before you ASK!

From Nick Gammon, a major wizard here:
Quote
How to process incoming serial data without blocking
http://www.gammon.com.au/forum/?id=11425
If you can learn that, you will be at least 5 steps ahead of where you are now.

Here's the Arduino site Playground -- a place to find code you can use:
Look under Input then serial. My oh my, what a lot of articles right there just for looking.
http://playground.arduino.cc/

Whatever else you learn, you should learn how to find things on your own. It is a basic skill.
Even if you don't get it all yourself at least you can find out enough to ask good questions.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: