I am pretty new to arduino and currently I am working on a school project involving arduino to arduino wireless communication with xbee. I am trying to send data from 2 analog sensors and a push button state from one arduino to another arduino. The sender arduino side would only have the two analog sensors and the push button; the receiving side would have the LEDs. In the end result, I want to remotely lit up an LED on pin 13 via push button, have an LED on pin 3 adjust its brightness according to the analog sensor, and have the LED on pin 8, 9, 10 lit up according to the second analog sensor.
I have the code of all the input and output on the same arduino. Now I need to do this wireless, but I am having trouble figuring out how to send data with serial communication in a way that they are “labeled” in correspondence to the specific analog sensor and push button state on the sender side, or that the receiving end can “differentiate” the data that is coming in and match each data with the right pin output. My lingo is probably quite confusing, pardon me.
Any help would be great, thank you.
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int analogOne = 0; // analog input
int potVal = 0; // reading from the sensor
const int analogInPin = 1; // Analog input pin that the GSR is attached to
const int analogOutPin = 3; // 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)
// variables will change:
int val; // variable for reading the pin status
int buttonState; // variable to hold the button state
int lightMode = 0; // Is the light on or off?
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(8, OUTPUT); // sets the pins as output for Force Sensing Resistor
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(buttonPin); // read the initial state
}
void loop(){
potVal = analogRead(analogOne); // read the potentiometer value at the input pin
Serial.print(potVal);
if (potVal < 341) // Lowest forth of the potentiometer's range (0-340)
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
else if (potVal < 682) // Middle forth of potentiometer's range (341-681)
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
else if (potVal < 800)
{
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
}
else
{
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
val = digitalRead(buttonPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // light is off
lightMode = 1; // light is on!
digitalWrite(ledPin, HIGH);
Serial.print("H");
} else {
lightMode = 0; // light is on!
digitalWrite(ledPin, LOW);
Serial.print("L");
}
}
}
buttonState = val; // save the new state in our variable
// read the analog in value for GSR:
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);
Serial.print(sensorValue);
}