Pages: 1 2 [3]   Go Down
Author Topic: Reading from Serial  (Read 3882 times)
0 Members and 1 Guest are viewing this topic.
nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8525
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
should i remove swread entirely? as well as the type constants? Just to clean things up?
Yep.

Quote
It returns 0's as well.
Then that's what you are getting from the router. Add a print just after you get the character to prove it but you have a problem there I'd say.

You must be getting something or .available() would never return a positive number, so I'd look at the buad rate. If you don't have any instruments try all different values until you get the right value.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

Here's the latest revision to my code. I'm trying to wrap my mind around the baud rates. The router sends and receives at 115200. But it too is printing out 0's, so it doesnt sound like my sketch is the problem anymore. it sounds like the carserver is only dumping single digits. I could reflash and try it with whiterussian openwrt, but id rather keep with dd-wrt if i can. I've reflashed and started from scratch so many times that i could probably do it in my sleep, but before i do that, can you take one last look at my code and let me know if im missing anything?

Code:
// include the SoftwareSerial library so you can use its functions:
#include <NewSoftSerial.h>
#include <ctype.h>

#define DEBUG 0

#define txPin 9
#define rxPin 8
#define LEDpin 13

NewSoftSerial mySerial(8,9);
int inByte;         // incoming serial byte

int serialChar; // stores character of serial Input as integer
 
unsigned long decay; // counter from last command
unsigned long decaylimit=100000; // 100,000 is about 5 seconds

 
void setup() {
    // define pin modes for tx, rx, led pins:

  pinMode(LEDpin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(115200);
  Serial.println("Hello"); // For debugging purposes
 
 
  //left/right motors
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  //end debugging
}
 
void loop() {
  //check for serial communications
 {

  if (mySerial.available()) {
      serialChar = mySerial.read();
      mySerial.println((char)inByte);
                digitalWrite (LEDpin, HIGH);
delay (100);
digitalWrite (LEDpin, LOW);
delay (100);
   
       #if DEBUG     // echo for debugging only:
      Serial.println(serialChar, HEX);
   
    // decay counts the age of the last command
    // at the limit (100,000 = ~5 seconds, all signals are shut off
    if (decay > decaylimit) {
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      // stop counting at limit, shut off all pins
    }
    else {
      decay++;
    }
   
   {
 
       #if DEBUG     // echo for debugging only:
      Serial.println(serialChar, HEX);

 #endif
        // serialChar contains the ASCII code of the *first* character sent over serial when defined as int
        switch (serialChar) {
 
         // logical signals w s a d style     
         case 'w': // 119
           digitalWrite(4, LOW); 
           digitalWrite(7, LOW);
           digitalWrite(5, HIGH);
           digitalWrite(6, HIGH);           
            decay=0;           
            break;
         case 's': // 115
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(4, HIGH);
            digitalWrite(7, HIGH);           
            decay=0;           
            break;
         case 'a': // 97 
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(4, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;
         case 'd': // 100
            digitalWrite(4, LOW);
            digitalWrite(6, HIGH);
            digitalWrite(5, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;           
         case 'q': // 113
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            decay = decaylimit;           

        }
      }
    }
  }
}




Logged

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

Ok, so i took a screenie to show you what im talking about. When you press w or up arrow you get 01, s or down, 02, a or left, 04, d or right 08.

The terminal sees it as being put out to serial and streams it, but the serial monitor only shows a single 0 coming into the arduino. Is my code set up to only read the first character? because if so, its only going to read that 0 and not the character after... have a look

Code:
// include the SoftwareSerial library so you can use its functions:
//01 = forward
//02 = backward
//04 = left
//08 = right

#include <NewSoftSerial.h>
#include <ctype.h>

#define DEBUG 1

#define txPin 9
#define rxPin 8
#define LEDpin 13

NewSoftSerial mySerial(8,9);
int inByte;         // incoming serial byte

int serialChar; // stores character of serial Input as integer
 
unsigned long decay; // counter from last command
unsigned long decaylimit=100000; // 100,000 is about 5 seconds

 
void setup() {
    // define pin modes for tx, rx, led pins:

  pinMode(LEDpin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(115200);
  mySerial.begin(115200);
  Serial.println("Hello"); // For debugging purposes
  
 
  //left/right motors
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  //end debugging
}
 
void loop() {
  //check for serial communications
 {

  if (mySerial.available()) {
      serialChar = mySerial.read();
      mySerial.println((char)inByte);
                digitalWrite (LEDpin, HIGH);
delay (100);
digitalWrite (LEDpin, LOW);
delay (100);
  
    
{
      Serial.println(serialChar, HEX);
    
    // decay counts the age of the last command
    // at the limit (100,000 = ~5 seconds, all signals are shut off
    if (decay > decaylimit) {
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      // stop counting at limit, shut off all pins
    }
    else {
      decay++;
    }
  
   {
 
       #if DEBUG     // echo for debugging only:
      Serial.println(serialChar, HEX);

 #endif
 
 if (mySerial.available()) {
      serialChar = mySerial.read();
      
      
        // serialChar contains the ASCII code of the *first* character sent over serial when defined as int
        switch (serialChar) {
 
         // logical signals w s a d style    
         case 'w': // 119
           digitalWrite(4, LOW);  
           digitalWrite(7, LOW);
           digitalWrite(5, HIGH);
           digitalWrite(6, HIGH);            
            decay=0;            
            break;
         case 's': // 115
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(4, HIGH);
            digitalWrite(7, HIGH);            
            decay=0;            
            break;
         case 'a': // 97  
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(4, LOW);
            digitalWrite(7, LOW);            
            decay=0;            
            break;
         case 'd': // 100
            digitalWrite(4, LOW);
            digitalWrite(6, HIGH);
            digitalWrite(5, LOW);
            digitalWrite(7, LOW);            
            decay=0;            
            break;            
         case 'q': // 113
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            decay = decaylimit;            

        }
      }
    }
  }
 }
}
}






Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When you press w or up arrow you get 01, s or down, 02, a or left, 04, d or right 08.
You get this where? Is that what the application is sending out? How is it sending the values? As bytes or as strings?

Code:
  if (mySerial.available()) {
      serialChar = mySerial.read();
If the data is sent as a binary value, it is read here, and discarded, as nothing more is done with serialChar than to print it.

However, as the serial monitor shows a 0, it appears that the application is sending the value as a string, so the first character is always 0, which is what you see printed.

I have no idea what decal and decayLimit are doing.

Since it appears that the data is sent as a string, and there is a delay after turning the LED on, and another after turning it off, the second character '1', '2', '4', or '8' will have arrived when this code is executed:
Code:
if (mySerial.available()) {
      serialChar = mySerial.read();

Then you have this:
Code:
        switch (serialChar) {
 
         // logical signals w s a d style     
         case 'w': // 119
         case 's': // 115
         case 'a': // 97 
         case 'd': // 100
         case 'q': // 113

Now, the obvious question is why your cases don't match what you say you are sending.
Logged

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

But why is the arduino only seeing the first character? its not going to matter what is being sent to it if all it sees is a 0 with a carriage return. i need two characters before i can change the sketch around.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But why is the arduino only seeing the first character?
How do you know it is only seeing the first character? The fact that you don't echo the result of the second read makes it hard for you to prove that it didn't occur.
Logged

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

Ok, im back and ready to work on this project some more. Sorry for the long interval, i had to move and just now got my internet working again.

Paul, what do i need to be inserting and where, in order to get the second read to make sure there is a second character being sent?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      serialChar = mySerial.read();
Read a character.

Code:
      Serial.println(serialChar, HEX);
Echo it.

Code:
      serialChar = mySerial.read();
Read another character.

Code:
        switch (serialChar) {
Assume that it is good.

Is it really that hard to figure out how to echo what you read?
Logged

Pages: 1 2 [3]   Go Up
Jump to: