Post by Deynox on Aug 27, 2015 12:59:34 GMT
I modified code:
It's really simple and not too good because I can't control motor speed - function motor_speed doesn't work, only set.Speed work fine.
And of course I need to control 2 servo but I don't know how to write it.
// 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 (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
int x_position;
int y_position;
const int led_pin = 11;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;
char msg[19]="X0000Y0000A0B0C0D0";
int led = 13;
void setup()
{
delay(1000);
Serial.begin(9600); // Debugging only
Serial.println("setup");
// VirtualWire Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
int thous;
int huns;
int tens;
int ones;
char digitsascii[5] = "0000";
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
//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);
}
/ receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
const int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;
const int X_THRESHOLD_LOW = 505;
const int X_THRESHOLD_HIGH = 530;
const int Y_THRESHOLD_LOW = 500;
const int Y_THRESHOLD_HIGH = 510;
int x_position;
int y_position;
int x_direction;
int y_direction;
int current_x_direction;
int current_y_direction;
uint8_t motor_speed;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
motor_speed = 255;
motor.setSpeed(200);
motor.run(RELEASE);
// Due to lack of access to Digital Pins with Motor Shield in place, using Analogue A0 (Arduino Pin 14) as Digital
vw_set_rx_pin(2);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
current_x_direction = 0;
current_y_direction = 0;
x_direction = 0;
y_direction = 0;
Serial.println("setup done");
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
uint8_t i;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
int j;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
// 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
//Serial.println("");
x_position = (1000 * (buf[1] - 48)) + (100*(buf[2] - 48)) + (10*(buf[3] - 48)) + (buf[4] - 48);
y_position = (1000 * (buf[6] - 48)) + (100*(buf[7] - 48)) + (10*(buf[8] - 48)) + (buf[9] - 48);
Serial.println(x_position);
Serial.println(y_position);
// Forwards
if (y_position > 600){
y_direction = 1;
motor.run(FORWARD);
for (i=0; i<120; i++) {
motor.setSpeed(i);
}
}
// Backwards
else if (y_position < 400){
y_direction = -1;
motor.run(BACKWARD);
for (i=0; i<100; i++) {
motor.setSpeed(i);
}
}
// Stop
else {
y_direction = 0;
motor.run(RELEASE);
}
}
}
It's really simple and not too good because I can't control motor speed - function motor_speed doesn't work, only set.Speed work fine.
And of course I need to control 2 servo but I don't know how to write it.