Hello, I am currently taking a meccano car and using an arduino uno R3 to turn it into an RC car. I am using an H-Bridge circuit to switch the car forwards, backwards and to get it to stop. you can see the code here
/*------------------------------------------------------------
L9110S-Stepper-DC-motor-Driver-Module
made on 28 oct 2020
by Amir Mohammad Shojaee @ Electropeak
edited by Evan Moore
---------------------------------------------------------------*/
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
#define A1 5 // Motor A pins
#define A2 6
byte Speed = 255; // Speed of motor 255 is 100% used to vary speed of PWM
typedef struct
{
uint16_t dataitem1;
uint16_t dataitem2;
} MyDataStruct;
RH_ASK driver;
int incomingByte = 0; // for incoming serial data
void setup() {
pinMode(A1, OUTPUT); //defines pin 5 to go to H-Bridge pin A1
pinMode(A2, OUTPUT); //defines pin 6 to go to H-Bridge pin A2
//Sets pins to low to override past variables so the transistors don't fry themselves
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void forward() { //function of forward
analogWrite(A1, Speed); //sets Pin A1 to use PWM with a duty cycle of Speed*100/255% in order to modulate the current through the motor
analogWrite(A2, 0); //sets Pin A2 to 0 so current does not pass through the transistors
}
void backward() { //function of backward. the same as forward but switch which is activated and zero in order to flip direction as current
analogWrite(A1, 0);
analogWrite(A2, Speed);
}
void Stop() { //function of stop. no current passes to the motor as all transistors are off
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
}
int input = 0;
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
input = incomingByte - 48; //convert ASCII code of numbers to 1,2,3
switch (input) {
case 1: // if input=1 ....... motors turn forward
forward();
break;
case 2: // if input=2 ....... motors turn backward
backward();
break;
case 3: // if input=1 ....... motors turn stop
Stop();
break;
}
//wait a bit before resetting the input I don't know exactly why this is here but someone smarter than me made it so I am not touching it.
delay(200);
input=0;
}
}
and I have gotten a cheap 433MHz. transmitter receiver to (hopefully) send what is currently being inputted into the serial monitor, and the speed of the car. however I cannot seem to get radiohead to work with me for the life of me.
here is the code for the receiver, I got it online, and tried to modify it for my purposes
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
#define LED 13
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
//Define packet for the direction (X axis and Y axis)
int data[2];
void setup() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
rf_driver.init();
pinMode(LED, OUTPUT);
}
void loop() {
// Set buffer to size of expected message
int receivedData[2] = {0};
uint8_t buflen = sizeof(receivedData);
if (rf_driver.recv((uint8_t*)receivedData, &buflen)) {
//if data is not an array, use &receivedData
for (byte i = 0; i < 3; i++) {
Serial.print(receivedData[i]);
Serial.print('\t');
Serial.println(receivedData[i],HEX);
}
}
}
currently I want to take one part of the data array and let that be either 1, 2 or 3. as that is what tells the code I made for the motor to go forwards, reverse or to stop. I want to be able to also use PWM to be able to modify the speed of the car, so the other part of the array will have a number from zero to 255.
right now I just don't know what I should replace the for loop in the receiver code to output those numbers.
finally I have my transmitter code which I also copied somewhere on the internet.
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
//Define packet for the
int data[2];
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
// Initialize ASK Object
rf_driver.init();
}
int input = 0;
void loop() {
incomingByte = Serial.read();
input = incomingByte - 48;
data[0] = input;
data[1] = 255;
rf_driver.send((const uint8_t*)data, sizeof(data));
rf_driver.waitPacketSent();
delay(200);
}
the code looks like it should work. I don't have much to say about it because I copied it from the same place as the receiver code
Update 1:03 PM 12/2/21 added the arduino I am using and the radio parts. also changed the amount of bytes the receiver asks for.