i have written a piece of code that consists of a dc motor adafruit motor shield a thumbstick (potentiometer) and a servo motor.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <Servo.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
Servo myservo; // create servo object to control a servo
int potPinServo = 0;
int potPinDrive = 1;
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(10);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(255);
myMotor->run(FORWARD);
// turn on motor
myMotor->run(RELEASE);
myMotor->run(BACKWARD);
}
void loop() {
// read value of joystich axis
int potVal = analogRead(potPinDrive);
val = analogRead(potPinServo); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(50); // waits for the servo to get there
if(potVal > 512)
{
myMotor->run(FORWARD);
myMotor->setSpeed (potVal);
}
else if(potVal < 500)
{
myMotor->run(BACKWARD);
myMotor->setSpeed(255);
}
else
{
myMotor->run(RELEASE);
myMotor->setSpeed(0);
}
}
this sketch is working and running at the moment but i want to use this code and make it wireless using rx one way radio link modules.
i assume i will have to use two arduino uno boards. split the code i have at the moment from inputs and outputs then put the inputs or controls on one arduino and transmit them to the other arduino were the motors will be set up.
this link shows the transmitter and receiver modules i am using
can this be done i also have example code for these link modules that transmitts a signal
transmitter
//Transmitter Code (Uno)
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
vw_setup(2000);
vw_set_tx_pin(7);
}
void loop()
{
if(Serial.available())
{
char c = Serial.read();
if(c == '1')
{
vw_send((uint8_t *)c, 1);
}
else if(c == '0')
{
vw_send((uint8_t *)c, 1);
}
}
}
receiver
//Receiver code
#include <VirtualWire.h>
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
uint8_t buflen = VW_MAX_MESSAGE_LEN;
uint8_t buf[buflen];
if (vw_get_message(buf,&buflen))
{
for(int i = 0;i <buflen;i++)
{
if(buf[i] == '1')
{
digitalWrite(13,LOW);
}
}
}
}