I would like to know what I am doing wrong, in this program.
I want to us A Potentiometer to send Analog input to,
Arduino with xbee. The data would be sent serial to
xbee to Arduino to light up led, from 0 to 255.
Using this program.
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Here is the two program's.
int potPin = 0;
void setup()
{
//Create Serial Object (9600 Baud)
Serial.begin(9600);
}
void loop()
{
int val = map(analogRead(potPin), 0, 1023,0,225);
Serial.println(val);
delay(50);
}
I think, I am missing something in the RX code, where it's not reading the serialin.
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}
void loop() {
while( Serial.available() == 0);
int sensorValue = Serial.read() -'0';
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
Serial.flush();
delay(2);
}
Do you see what you expect to see displayed in the serial monitor? Have you gotten the setup to work just connecting the arduinos together with wires instead of using the xbees?
On the Serial Monitor in on the rx side of the Arduino, the data
that I am getting in, is this. I am missing something on the rx side and
tx side, that will send more then one byte.
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 7 output = 1
sensor = -35 output = -8
sensor = 1 output = 0
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = -38 output = -9
sensor = 1 output = 0
sensor = -38 output = -9
sensor = 1 output = 0
sensor = -35 output = -8
sensor = 1 output = 0
sensor = -35 output = -8
sensor = 1 output = 0
sensor = 7 output = 1
sensor = 1 output = 0
sensor = 7 output = 1
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
sensor = 1 output = 0
sensor = 9 output = 2
I did find some code on line, but can't get it working. Here it is.
// SENDER
int analogValue5, val5;
void setup() {
// Serial port enable
Serial.begin(19200);
}
void loop() {
// read analog pin 5
analogValue5 = analogRead(5);
// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);
// send the value to the serial port
Serial.println(val5, BYTE);
}
// RECIEVER
byte incomingByte;
void setup() {
// Serial port enable
Serial.begin(19200);
// declare pin 11 as output, this is the LED
pinMode (11, OUTPUT);
}
void loop() {
// if there is bytes available coming from the serial port
if (Serial.available()) {
// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();
// write the value to the pin 11
analogWrite(11, int(incomingByte));
}
}
Here is the code I was looking for, and it works. Thank's for all your help, people..........
// SENDER
int analogValue5, val5;
void setup() {
// Serial port enable
Serial.begin(19200);
}
void loop() {
// read analog pin 5
analogValue5 = analogRead(0);
// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);
// send the value to the serial port
Serial.write(val5);
}
// RECIEVER
byte incomingByte;
void setup() {
// Serial port enable
Serial.begin(19200);
// declare pin 11 as output, this is the LED
pinMode (11, OUTPUT);
}
void loop() {
// if there is bytes available coming from the serial port
if (Serial.available()) {
// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();
// write the value to the pin 11
analogWrite(11, int(incomingByte));
}
}