Created
January 4, 2015 18:25
-
-
Save conoro/223035df942b72d2d2f6 to your computer and use it in GitHub Desktop.
RC Car Control Transmitter with simple 433MHz modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// transmitter.pde | |
// | |
// Simple example of how to use VirtualWire to transmit messages | |
// Implements a simplex (one-way) transmitter with an TX-C1 module | |
// | |
// See VirtualWire.h for detailed API docs | |
// Author: Mike McCauley ([email protected]) | |
// Copyright (C) 2008 Mike McCauley | |
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $ | |
#include <VirtualWire.h> | |
// Store the Arduino pin associated with each input | |
// Select button is triggered when joystick is pressed | |
const byte PIN_BUTTON_SELECT = 2; | |
const byte PIN_BUTTON_RIGHT = 3; | |
const byte PIN_BUTTON_UP = 4; | |
const byte PIN_BUTTON_DOWN = 5; | |
const byte PIN_BUTTON_LEFT = 6; | |
const byte PIN_ANALOG_X = 0; | |
const byte PIN_ANALOG_Y = 1; | |
int x_position; | |
int y_position; | |
char msg[19]="X0000Y0000A0B0C0D0"; | |
int led = 13; | |
void setup() { | |
pinMode(led, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("setup"); | |
pinMode(PIN_BUTTON_RIGHT, INPUT); | |
digitalWrite(PIN_BUTTON_RIGHT, HIGH); | |
pinMode(PIN_BUTTON_LEFT, INPUT); | |
digitalWrite(PIN_BUTTON_LEFT, HIGH); | |
pinMode(PIN_BUTTON_UP, INPUT); | |
digitalWrite(PIN_BUTTON_UP, HIGH); | |
pinMode(PIN_BUTTON_DOWN, INPUT); | |
digitalWrite(PIN_BUTTON_DOWN, HIGH); | |
pinMode(PIN_BUTTON_SELECT, INPUT); | |
digitalWrite(PIN_BUTTON_SELECT, HIGH); | |
// VirtualWire Initialise the IO and ISR | |
vw_set_ptt_inverted(true); // Required for DR3100 | |
vw_setup(2000); // Bits per sec | |
} | |
void loop() { | |
int thous; | |
int huns; | |
int tens; | |
int ones; | |
char digitsascii[5] = "0000"; | |
x_position = analogRead(PIN_ANALOG_X); | |
y_position = analogRead(PIN_ANALOG_Y); | |
// Format is XnnnnYnnnnAnBnCnDn | |
// X is X position of joystick as 4 digits | |
// Y is Y position of joystick as 4 digits | |
// A is A button on joystick. 0= Not pressed. 1 = pressed | |
// B is B button on joystick. 0= Not pressed. 1 = pressed | |
// C is C button on joystick. 0= Not pressed. 1 = pressed | |
// D is D button on joystick. 0= Not pressed. 1 = pressed | |
//First convert X pos to 4 ascii characters (48 is decimal ASCII code for '0') | |
thous = x_position/1000; | |
msg[1]=thous+48; | |
huns = (x_position - (thous*1000))/100; | |
msg[2]=huns+48; | |
tens = (x_position - (thous*1000) - (huns*100))/10; | |
msg[3]=tens+48; | |
ones = x_position - (thous*1000) - (huns*100) - (tens*10); | |
msg[4]=ones+48; | |
//Then convert Y pos to 4 ascii characters | |
thous = y_position/1000; | |
msg[6]=thous+48; | |
huns = (y_position - (thous*1000))/100; | |
msg[7]=huns+48; | |
tens = (y_position - (thous*1000) - (huns*100))/10; | |
msg[8]=tens+48; | |
ones = y_position - (thous*1000) - (huns*100) - (tens*10); | |
msg[9]=ones+48; | |
// Will implement buttons laters | |
digitalWrite(led, HIGH); | |
Serial.println(msg); | |
// send string to receiver (actually just broadcast. No concept of 1-1 comms with these TX/RX modules | |
vw_send((uint8_t *)msg, strlen(msg)); | |
vw_wait_tx(); // Wait until the whole message is gone | |
delay(100); | |
digitalWrite(led, LOW); | |
delay(100); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment