Wow, that helped a LOT Grumpy_Mike!
I'm so close. I've got it communicating partially by RF!! Finally! but it's still not 100%.
I don't think I've got the most effective code written, so correct me if I'm wrong, but I think the code might be a reason why there's not perfect communication.
I've copied these codes from a few tutorials and made them to work together, so there's a lot of useless code that does NOT need to be in there. I've just left it for now.
Since last post, they now communicate perfectly through a hard wire using just the power supply rather then USB, but having a bit of difficulty with RF. When close(a few inches away), it works almost all the time, but as soon as I pull them apart further than say... 9 inches, the LED on Arduino(2) just flashes on for a split second when I press the button on Arduino(1). Again seems like noise I'm dealing with.
Tx is receiving 9V and has a 23 cm antenna
Rx is receiving 5V and also has a 23 cm antenna
Here's the tx code I'm using:
/*
* Arduino(1)
* Alternating switch with Tx
*/
int switchPin = 2; // switch is connected to pin 2
int dataPin = 1;
int ledPin = 12;
int thisByte = 'd';
int thatByte = 'n';
int val; // variable for reading the pin status
int buttonState; // variable to hold the last button state
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(dataPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(1200); // Set up serial communication at 1200bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
digitalWrite(dataPin, 'd');
digitalWrite(ledPin, HIGH);
Serial.print('d');
} else { // the button is -not- pressed...
digitalWrite(dataPin, 'n');
digitalWrite(ledPin, LOW);
Serial.print('n');
}
}
buttonState = val; // save the new state in our variable
}
Here's the Rx code I'm using:
I'm really only using one LED with a 220 ohm resistor, which is case 'd', digital output 5.
/*
Arduino(2)
Rx with LED
Switch statement with serial input
Demonstrates the use of a switch statement. The switch
statement allows you to choose from among a set of discrete values
of a variable. It's like a series of if statements.
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
the LEDs off.
The circuit:
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
created 1 Jul 2009
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SwitchCase2
*/
int dataPin = 0;
void setup() {
// initialize serial communication:
Serial.begin(1200);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
pinMode(dataPin, INPUT);
}
}
void loop() {
// read the sensor:
if (Serial.available() > 0) {
int dataPin = Serial.read();
Serial.println(dataPin);
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:
switch (dataPin) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}
Any advice on the code?
or does this code seem ok?
Phil Dupuis
(Dupwii)