Serial problems

hi there having problems with truing to get this code to work i don’t no what i am doing wrong, what do you think?

uno with no chip connected to pc sending serial to my other arduino meg
gnd to gnd
pin2 to pin10

just truing to turn on and off a led over serial

// Software Serial
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 9); // RX, TX

const int ledPin = 13;
char val;

void setup() {

mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {

if (mySerial.available()) {

val = mySerial.read();

if (val == ‘1’) {
digitalWrite(ledPin, HIGH);
}
if (val == ‘2’) {
digitalWrite(ledPin, LOW);
}
}
}

any help will be appreciated :slight_smile:

What about the code for the sender?

there is non, there is no chip in there, so it is coming for the pc

You need to connect pin 0 (RX on the uno) to pin 10.

not TX (pin1) on the arduino from pc to slave Pin10 RX ??

He wants to sent data to this mega using the usb-serial of a uno. So you need to connect pin 10 on this mega (RX) to pin 1 (TX),not 2!!!, of the uno.

Why do you use soft serial??? A mega has 4 hardware serials...

And do you really sent the chat '0' and not just 0 (the byte)?

He wants to sent data to this mega using the usb-serial of a uno. So you need to connect pin 10 on this mega (RX) to pin 1 (TX),not 2!!!, of the uno.

ok cool dun

Why do you use soft serial??? A mega has 4 hardware serials

the end code will be on a uno

And do you really sent the chat '0' and not just 0 (the byte)?

would that be better? would i still be sending 1 and 2? and how would i change that? take out ' ' ?

thanks Joe

Joes: would that be better? would i still be sending 1 and 2? and how would i change that? take out ' ' ?

thanks Joe

Not that either one is better or worse, just need to make sure you are consistent on both ends.

right ok so what does the arduino serial monitor put out then?

Then why not use the serial of the uno???

And if you sent '1' (char) of 1 depends on who sent it... I think the serial monitor of Arduino does the char one. What's better depends on the application. But if you sent "146" with serial monitor it actually sends 3 bytes/chars, a 1, a 4 and a 6. So it would make sens to send just the single byte 146...

ok so what do you recommend i do then?

Like I said, it depends what sent the serial.

But for now that's not the problem. Is there a reason not to use the hardware serial? If not, switch to that. And fix your connections ;)

connections are as u sed and the idea of this is not to use the hardware serial as code is going on uno and the hardware lines mite end up doing something else

"Might end up doing something else"... Okay, why? You are the designer! First use hardware serial. io can go to another pin. Soft serial is tricky business.

LCD

First use hardware serial. io can go to another pin. Soft serial is tricky business.

ok lets do that then

I've used the below serial test code to communicate between two arduinos.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

You might find the examples in serial input basics useful.

...R

zoomkat: I've used the below serial test code to communicate between two arduinos.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {  Serial.begin(9600);  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded }

void loop() {

 //expect a string like wer,qwe rty,123 456,hyre kjhg,  //or like hello world,who are you?,bye!,

 if (Serial.available())  {    char c = Serial.read();  //gets one byte from serial buffer    if (c == ',') {      if (readString.length() >0) {        Serial.print(readString); //prints string to serial port out        Serial.println(','); //prints delimiting ","        //do stuff with the captured readString        readString=""; //clears variable for new input      }    }      else {          readString += c; //makes the string readString    }  } }

thanks for that example is quite good, i had a play with it and come up with this to turn pin13 on and off what works fine :)

String readString;

const int ledPin = 13;

void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); }

void loop() {

if (Serial.available()) { char c = Serial.read(); //gets one byte from serial buffer

if (c == '1') { digitalWrite(ledPin, HIGH); } if (c == '2') { digitalWrite(ledPin, LOW); } } }

so how do we move that to Software Serial then?

thanks all for your help :)

Joes: so how do we move that to Software Serial then?

Just change the references to Serial to mySoftSerial or whatever you decide to call it.

...R

Right i got that working :slight_smile:

// Software Serial
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 9); // RX, TX

const int ledPin = 13;

void setup() {
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {

if (mySerial.available()) {
char c = mySerial.read(); //gets one byte from serial buffer

if (c == ‘1’) {
digitalWrite(ledPin, HIGH);
}
if (c == ‘2’) {
digitalWrite(ledPin, LOW);
}
}
}

but I’m going to through a spanner in the works lol but could we get it to work on at ATTiny84?
i have had a look on here Communication with Tiny's to get some ideas and come up with this

#include <SoftwareSerial.h>
const int rx=6;
const int tx=-1; // not used
SoftwareSerial mySerial(rx,tx);

int i=0;
char buf[12];

const int ledPin = 0;

void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
mySerial.begin(9600);

pinMode(ledPin, OUTPUT);
}

void loop() {

if (mySerial.available()>0) {
buf*= mySerial.read();*

  • if (i == ‘1’) {*
  • digitalWrite(ledPin, HIGH);*
  • }*
  • if (i == ‘2’) {*
  • digitalWrite(ledPin, LOW);*
  • }*
  • }*
    }[/quote]
    but naval happens so what am i doing wrong now? and can i us any pins or is there pacific pins i should be using ?
    thanks all