Dear All,
I am very new to Arduino Programming. I have connected 10K potentiometer to A0 and A1 of Uno. When I run the program, default Analog Read Serial, I get correct values of the potentiometer on the Serial Monitor.
I wrote another program to send data to another Arduino via Software Serial and when I run the program, I get junk data. When I change the A0 potentiometer, I get erratic values. Changing A0 pot also changes the A1 input. I tried putting delays, but didn't help. The default program example works but the one I wrote gives errors. I am sure I am making some syntax mistakes or my approach is wrong. I wrote this program by looking others program on the net. Could you please help me out with it?
Working Properly:
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue0 = analogRead(A0);
int sensorValue1 = analogRead(A1);
// print out the value you read:
Serial.println(sensorValue0);
Serial.println(sensorValue1);
delay(1000); // delay in between reads for stability
}
Written by me, which gives errors:
/To send these values to another Arduino usung Software Serial/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); //RX, TX, These pins will be used to send the data to another Arduino
/* #### varialbles for potentiometers, handles ##########*/
int sensor0 = 0; // analog pin used to connect the potentiometer
int sensor1 = 1; // analog pin used to connect the potentiometer
int ValSensor0;
int ValSensor1;
String strValSensor0;
String strValSensor1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(9600);
Serial.setTimeout(10);
mySerial.setTimeout(10);
}
void loop() {
// put your main code here, to run repeatedly:
//########## code for potentiometers, handles #################
ValSensor0 = analogRead(ValSensor0); // reads the value of the potentiometer (value between 0 and 1023)
delay (50); // To Stabilize
ValSensor1 = analogRead(ValSensor1);
delay (50); // To Stabilize
ValSensor0 = map(ValSensor0, 0, 1023, 0, 255); // Map Valuse
ValSensor1 = map(ValSensor0, 0, 1023, 0, 255); // Map Valuse
strValSensor0 = String(ValSensor0); // Convert to string
strValSensor1 = String(ValSensor1); // Convert to string
//read figures in serial monitor
Serial.println("handle Sensor 0: "+ strValSensor0);
Serial.println("handle Sensor 1: "+ strValSensor1);
mySerial.print(":" + strValSensor0 + "," + strValSensor1 + ";"); // Send the values to another Arduino via Software Serial 4,5
int ValSensor0=0 ;
int ValSensor1=0 ;
mySerial.flush();//Waits for the transmission of outgoing serial data to complete.
delay(50);//allow some time for the serial buffer
}





