Hello,
I am trying to write two separate codes for a project that takes 2 potentiometers as inputs, goes into an arduino, and is wirelessly transmitted via Xbee to another xbee/arduino, where it will output two PWM signals (one based off of each potentiometer).
I can get it to work well using one potentiometer and LED, but when I try to add the second pair, I get stuck with the coding.
Here is the code I have for the transmitter side:
//Constants:
const int potPin1 = A0; //Pot input at Arduino A0 pin
const int potPin2 = A5; //Pot input at Arduino A5 pin
//Variables:
int value1 ; //Value from pot1
int value2;//Value from pot2
void setup() {
//Start the serial communication
Serial.begin(19200); //Starts serial communication @ 19200bps (Same as Xbee)
}
void loop() {
//Read the analog value from pot and store it to "value" variable
value1 = analogRead(A0);//Sets value1 as analog input from port A0
// value2 = analogRead(A5);//Sets value2 as analog input from port A5
//Map the analog value to pwm value
value1 = map (value1, 0, 1023, 0, 255);// maps original values of 0-1023 to 0-255
// value2 = map (value2, 0, 1023, 0, 255);// maps original values of 0-1023 to 0-255
//Send the message:
Serial.print('<'); //Starting symbol pot1
Serial.print(value1);//Sends value1 from 0 to 255
Serial.println('>');//Ending symbol pot1
Serial.print('{');//Starting symbol pot2
Serial.print('value2');//Sends value2 from 0 to 255
Serial.print('}');//ending symbol pot2
}
I am not sure if I should get rid of the ending symbol on value1 and the starting symbol of value2, or keep it the way it is. This code outputs as <255>{255}<255>{255}... and so on(example).
The main confusion I have is the receiver side of the code, here is my attempt so far:
//Constants
const int ledPin = 3; //Led to Arduino pin 3 (PWM)
const int ledPin2 = 5; //Led to Arduino pin 5 (PWM)
//Variables
bool started= false;//True: Message is strated
bool ended = false;//True: Message is finished
char incomingByte ; //Variable to store the incoming byte
char msg[3]; //Message1 - array1 from 0 to 2 for PWM 0-255 values
char msg2[3];//Message2 - array2 from 0 to 2 for PWM 0-255 values
byte index; //Index of array 1
byte index2;//Index of array2
void setup() {
//Start the serial communication
Serial.begin(19200); //Starts serial communication @ 19200bps (Same as Xbee)
pinMode(ledPin, OUTPUT);//Sets pin 3 as a PWM output
pinMode(ledPin2, OUTPUT);//Sets pin 5 as a PWM output
}
void loop() {
while (Serial.available()>0){
//Read the incoming byte
incomingByte = Serial.read();
if(incomingByte == '<' || '{') //NOT SURE IF THIS WILL WORK
{
started = true;
index = 0;
msg[index] = '\0'; // Throw away any incomplete packet
}
else if(incomingByte == '>' || '}')
{
ended = true;
break; // Done reading - exit from while loop!
}
//Read the message!
else
{
if(index < 4) // Make sure there is room
{
msg[index] = incomingByte; // Add char to array
index++;
msg[index] = '\0'; // Add NULL to end
}
}
}
if(started && ended)
{
int value = atoi(msg);
analogWrite(ledPin, value);
analogWrite(ledPin2,value);
//Serial.println(value); //Only for debugging
index = 0;
msg[index] = '\0';
started = false;
ended = false;
}
}
I do not know if I should make multiple arrays, or keep it all as one and somehow extract them out separately. I am really stuck here and would appreciate any help.
Thank you very much!