I am trying to get an Arduino Micro to send data from a potentiometer to an Arduino Uno. I have the wires as follows:
Rx --> pin5(tx)
Tx --> pin4(rx)
5v --> 5v
gnd --> gnd
Here is my code for the Micro:
int pot=A0;//Receiving pot data from pin A0
int lastval;//Storage for old pot value to see if it has changed
void setup(){
Serial1.begin(9600);
pinMode(pot,INPUT);
}
void loop(){
if(lastval!=(analogRead(pot)/100)){//Check if last pot value(now lastval) has changed
Serial1.println(analogRead(pot)/100);//Send new value if it has changed
}
lastval=(analogRead(pot)/100); //Save current pot value
delay(100);
}
When I change "Serial1" to "Serial" and plug it in via USB it sends values from 1 to 10 when the potentiometer is turned so this seems to be working properly.
This is the code for the receiving Uno:
#include<SoftwareSerial.h>
SoftwareSerial x(4,5);//rx4 tx5
void setup(){
Serial.begin(9600);//Serial to computer
x.begin(9600);//Serial from Micro
delay(500);
}
void loop(){
while(x.available()>0){//Check for incoming data from Micro
Serial.write(x.read());//Relay data to computer
}
}
Both boards should have enough power and will run basic Serial exchanges with a computer and a test with an led showed that the Uno does receive something from the Micro(led lit up only when pot was moved). Neither board has flashing Rx or Tx lights but I assume that is because I am using Software Serial ports. The green light on the Micro turns off after a while but the blue one stays on, does that mean I don't have enough power of the board is going into some kind of sleep mode? Any help is appreciated and thanks in advance.
Please tell us how you have connected it.
Which pin to which pin exactly.
I don't understand how you powered both boards.
Can you attach a photo of the wiring ?
Can you connect the Micro to the computer and have the Serial (via the usb) show messages what it is doing.
So you have two boards connected to the computer, each board with two serial ports.
You should be able to start the Arduino IDE twice to have simultaneous control over both boards.
Your 'lastval' is not going to work like that. If you store the current value, you request a new analogRead(), and that could be something else. For testing perhaps omit the 'lastval', and send the value continueusly with a 500ms delay.
The micro code with lastval is working and producing data when I use "Serial", which connects only to the usb and not to the rx and tx pins which means I have to read the data from a serial monitor. The reason I switched to "Serial1" is because that connects to the physical rx and tx pins which I am using to talk to the Uno. As for powering both boards, the Uno is plugged in via usb and the micro is receiving that power through the 5v line as well as being powered by a separate battery pack. The only wiring is the 4 lines between the boards mentioned before and the pot and battery connected to the micro. The reason pin5 is tx and pin 4 is rx on the Uno is because I am using software serial since "serial" on pins 1 and 0 is already used by the usb. Is it possible the boards have different voltages going to them even if 5v and gnd are connected? The Micro looks like it goes to sleep about 5 seconds after it is disconnected from the usb because the green light fades in and out a couple times and then turns off.
But I mentioned to use also two serial ports on the Micro and connect it to the computer to see if that makes a difference for the power. And you have control over two boards at the same time, while they are talking to each other.
Test code I've used between arduinos where a string sent to the tx arduino from its serial monitor and the rx arduino receives the string and displays the string on its serial monitor.
//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() >1) {
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
}
}
}
@zoomkat
Hi sir, regarding on your code(#reply no. 5) is it possible for your code to input a string in the serial monitor with the GSM switch in UART mode?
Vmateo: @zoomkat
Hi sir, regarding on your code(#reply no. 5) is it possible for your code to input a string in the serial monitor with the GSM switch in UART mode?
I don't know anything about GSM and UART, but if you want to connect the GSM TTL serial tx line to the arduino tx pin so the output will display in the serial monitor, try putting a small signal diode in series between the GSM tx and arduino tx with the diode band on the GSM tx pin side.
Thanks, I don't think this is quite what Caltoa meant but I installed a second Arduino IDE to run both Arduino's at once and narrowed it down to not using Software Serial correctly. I there something I am supposed to do to show that the topic has been solved? Thanks again.
You can edit your first post and add "[SOLVED]" in front of the subject.
The Arduino IDE should be able start multiple instances, you don't have to install two of them.