simple on/off code for virtualwire

Hello,

I thought I could figure this simple code for myself, but I'm not successful at all.

I'm very new to this, been having fun with many tutorials, now on to my project and I can't seem to figure out a thing!

All I need is to turn a motor on and off with a simple pushbutton switch wirelessly. I know it's sounds so simple, but the lack of progress is driving me crazy!

I have the sparkfun 433 Mhz tx/rx. All the tutorials I'm finding on this unit has to do with sending text. I don't know how to change their examples to remote control.

I do have 2 arduinos, one is setup with the tx and a pushbutton and the other arduino with the rx and a TIP120 transistor to a motor.

Does anyone have a simple code to get me started on this, I do have virtualwire setup in my library, but at the moment I can't figure that one either.

Phil

All the tutorials I'm finding on this unit has to do with sending text.

So send one character of text when the button is pressed say 'P' and another when it is released say 'R'.

Then at the receive end simply look at the received character and say:-
if(received == 'P') { } // motor on command
if(received == 'R') { } // motor off command

Beautiful thank you, I'll give it a try!

ok so I dropped Virtual Wire, couldn't understand it yet to really play around with it. Using your advice, I used letters as a digital code to send.
Actually I used 2 tutorials that were on serial communication to have them talk together.

I'm really close, but not yet there.
When button is pressed, it sends 'b'
when button is released, it sends 'n'
b's value is 98
n is 110

on the other arduino when it receives 98 the led turns on
when it receives anything other than 98 the led turns off

but when I press the button from arduino 1, it sends
98
13
10
so the led flashes,
when I release the button it sends
110
13
10
that doesn't matter as much because all of them are not 98.

how can I send only 98 or 110, why are 13 and 10 following after?

The codes 13 and 10 are the ascii codes for carriage return (move to beginning of line) and line feed (advance to next line) respectively.

Obviously you are sending these from your sketch - so the source will need to be modified to avoid them. E.g. Serial.print() sends data without 10/13 and Serial.println() adds 10/13.

As an alternative you could change your receiving side to just ignore 10/13 (or anything else for that matter) and just act on 'b' and 'n'.

Awesome! It worked, I pulled the ln from Serial.println to only Serial.print and she goes Great. Thank you.

I have One last obstacle, but I'll try to figure it out,
once I figure it out I'll post my code so people could use it.

In the mean while thanks Ben_F and Grumpy_Mike.

Sorry BenF for spelling your name wrong in the previous post.

I now have the 2 arduinos communicating beautifully but only through a direct wire going from Arduino(1)'s tx (digital 1) straight to Arduino(2)'s rx (digital 0).

It works great
Arduino(1) has a pushbutton on a pull-up resistor connected to digital 3.

Arduino(2) has a transistor on digital 5 turning a motor on and off when Arduino (1) presses the button.

Now I'd like to send that communication over rf.
I've got those inexpensive rf tx/rx 315 mhz units from Sparkfun.

When I try the same communication over rf
Arduino(1) digital1 output connected to the transmitter
Arduino(2) digital0 input connected to the receiver

The receiver goes crazy, lots of noise, on the serial monitor I'm showing mostly digits between 240 - 255 (not always within that range), but there's no rhyme or reason to it, just random numbers shooting through.

When I press the button from Arduino(1) it does not override the noise. Every once in a while the signal will happen to be 100 or 'd' so the motor flashes on for a split second.

How do I get my signal '100' or '110'(motor off) to penetrate the noise?

Dupwii

I trust you have Gnd and +5V connected to the receiver and transmitter as well.

Make sure serial speed is not too high. Try 2400 baud or lower and remeber to use the same serial speed on both Arduino's.

If the receiver/transmitter is more than a few feet apart you should add antennas. A piece of thin wire - 23cm - for the 315Mhz version.

You could also try it without the motor, just put an LED in place. When that works you know that the problem is with interference caused by the motor. You need to suppress this, a 0.1uF capacitor across the motor will help reduce the noise as will increased supply decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
For both TX and RX boards.
However if the interference is radiated then only suppression of the motor will suffice and that might be tricky.

Thank you guys for your response. The article really looks like that's my problem. I disconnected the motor and placed an LED, but the noise is still as active as before.

I have the 23 cm antennas, my baud rate is 1200.

If the noise is still present even after totally disconnecting the motor from the circuit, then there must be something in the connection. I could even disconnect the data from arduino(1) tx and switch and the noise in arduino(2) is very much just as active.

I've got a multimeter, but the formula that the article gives requires me to read the frequency and the capacity in farads, which I don't think I can do.

The article definitely describes the very problem I'm facing, and if it's true, that 9 out of 10 times the problem is decoupling then this is probably what I should really look into.

I tried different size capacitors around what they showed, but I don't know if I'm placing them in the right places. I put them between the power and the ground on both chips (which must be what they mean by supply), and kind of anywhere I could try. But still no success. I'm going to give this a try for while.

Again thanks,
Phil Dupuis
(dupwii)

So I haven't yet figured out where to place the capacitors to do the decoupling thing. I'm building the arduinos independant on breadboards so that I can place capacitors before the chips, haven't yet achieved it, need to buy 10uf capacitors.

However on my way of doing that, I came up with another mystery.
I unplugged Arduino(1) from the USB and placed a 6V(4 AA) power supply into it. I left Arduino(2) plugged up by USB to the computer so that I can read the serial monitor.
I placed a hard wire between the OUTPUT of Arduino(1) and the INPUT of Arduino(2). So that they were connected directly again(returning to wire rather then rf for a test). BUT the communication didn't go through?!?!? By pressing the switch on Arduino(1), I couldn't read anything on the Serial Monitor hooked up to Arduino(2), nor would the LED light up on Arduino(2).

It works perfectly when I connect both Arduinos powered by USB, but as soon as I changed the power supply of one of them to 6V, I lose the communication between the 2. I didn't think the power source made a difference.

Does it affect Serial.print??

Any ideas?

Dupwii
(Phil Dupuis)

I didn't think the power source made a difference.

You have to connect the grounds together:-
http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html

so that I can place capacitors before the chips

Not sure what you mean, the capacitors go across the supply, that is from + to ground. They should be placed on the chip as close as possible. preferably the 0.1uF should be of the ceramic type. The inductor or resistor should be in series with the +ve line between the chip and the supply.

I'm building the arduinos independant on breadboards

Breadboards are not the best form of construction to minimise noise pick up.

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)

I'm trying the code that came on the virtual wire pdf file.
I'm understanding VirtualWire more now.

I changed the message to say "hhhhhhhh" rather than "hello" for the tx, the delay was also changed to 50.
I then had the rx turn the ledPin on HIGH when it receives "hhhhhhhh" and LOW as soon as the message stops.

I realized that if I only had one "h", the light would blink faintly, but if I sent 4 to 8 h's, the light would blink brighter.

I attached the ledPin to a transistor that turns a motor on.
Same thing, one h and the motor would be very weak and it would ever so slightly sputter, but 4 to 8 h's in the message turns the motor at normal speed, but the motor still sputters, it seems because I have more h's the motor runs smoother for longer between sputters, which makes sense.

However, how can I get the motor to run smoothly all the way through?

Dupwii