//arduino NANO RC receiver to PWM
//Arduino Nano ATmega328P on dev/ttyUSB0
//Old bootloader
#include <EnableInterrupt.h>
#define SERIAL_PORT_SPEED 19200
#define RC_NUM_CHANNELS 2
#define RC_CH1 0
#define RC_CH2 1
#define enL 7
#define enR 8
#define RC_CH1_IN A2
#define RC_CH2_IN A3
#define LED 13
int x = 0;
int pwm = 0;
uint16_t rc_values[RC_NUM_CHANNELS];
uint32_t rc_start[RC_NUM_CHANNELS];
volatile uint16_t rc_shared[RC_NUM_CHANNELS];
void rc_read_values() {
noInterrupts();
memcpy(rc_values, (const void *)rc_shared, sizeof(rc_shared));
interrupts();
}
void calc_input(uint8_t channel, uint8_t input_pin) {
if (digitalRead(input_pin) == HIGH) {
rc_start[channel] = micros();
} else {
uint16_t rc_compare = (uint16_t)(micros() - rc_start[channel]);
rc_shared[channel] = rc_compare;
}
}
void calc_ch1() { calc_input(RC_CH1, RC_CH1_IN); }
void calc_ch2() { calc_input(RC_CH2, RC_CH2_IN); }
void serialPrint() {
Serial.print("CH1: "); Serial.println(rc_values[RC_CH1]);
Serial.print("CH2: "); Serial.println(rc_values[RC_CH2]);
Serial.print("pwm "); Serial.println(pwm);
x = 0;
}
void setup() {
Serial.begin(SERIAL_PORT_SPEED);
pinMode(RC_CH1_IN, INPUT);
pinMode(RC_CH2_IN, INPUT);
pinMode(enL, OUTPUT); // enable left
pinMode(enR, OUTPUT); // enable right
//pinMode(2, INPUT); //sensor1
pinMode(6, OUTPUT); //pwm left
pinMode(9, OUTPUT); //pwm left
pinMode(10, OUTPUT); //pwm right
pinMode(11, OUTPUT); //pwm right
pinMode(LED, OUTPUT); // LED
//pinMode(2, INPUT_PULLUP)
enableInterrupt(RC_CH1_IN, calc_ch1, CHANGE);
enableInterrupt(RC_CH2_IN, calc_ch2, CHANGE);
digitalWrite(enL, 0); // enable L
digitalWrite(enR, 0); // enable R
}
void loop() {
rc_read_values();
//-----------stopped
if ((rc_values[RC_CH2] < 1650) && (rc_values[RC_CH2] > 1450) && (rc_values[RC_CH1] > 1450) && (rc_values[RC_CH1] < 1550)) {
digitalWrite(enL, 0); //enable right
digitalWrite(enR, 0); //enable left
//analogWrite(6, 0);
//analogWrite(9, 0);
//analogWrite(10, 0);
//analogWrite(11, 0);
}else{
}
// -----------Reverse
if ((rc_values[RC_CH2] < 1450) && (rc_values[RC_CH1] > 1450) && (rc_values[RC_CH1] < 1550)) {
digitalWrite(enL, 1); //enable right
digitalWrite(enR, 1); //enable left
analogWrite(6, 0);
analogWrite(9, (map(rc_values[RC_CH2], 1000, 1450, 230, 20)));
analogWrite(10, 0);
analogWrite(11, (map(rc_values[RC_CH2], 1000, 1450, 230, 20)));
} else {
}
// ----------forward
if ((rc_values[RC_CH2] > 1560) && (rc_values[RC_CH1] > 1450) && (rc_values[RC_CH1] < 1560)) {
digitalWrite(enL, 1); // enable
digitalWrite(enR, 1); //enable
analogWrite(6, (map(rc_values[RC_CH2], 1550, 2000, 25, 210)));
analogWrite(9, 0);
analogWrite(10, (map(rc_values[RC_CH2], 1550, 2000, 25, 230)));
analogWrite(11, 0);
} else {
}
// ----------forward right
if ((rc_values[RC_CH2] > 1560) && (rc_values[RC_CH1] > 1560)) {
digitalWrite(enL, 1); //enable right
digitalWrite(enR, 1); //enable left
analogWrite(6, (map(rc_values[RC_CH2], 1550, 2000, 20, 238)));
analogWrite(9, 0);
pwm = (map(rc_values[RC_CH2], 1550, 2000, 20, 250)-(map(rc_values[RC_CH1],1550, 2000, 20, 245)));
if (pwm < 0) {
pwm = 0; // make sure pwm is not negative
}
analogWrite(10, (pwm));
analogWrite(11, 0);
} else {
}
//-----------fwd left
if ((rc_values[RC_CH2] > 1560) && (rc_values[RC_CH1] < 1450)) {
digitalWrite(enL, 1); //enable right
digitalWrite(enR, 1); //enable left
pwm = (map(rc_values[RC_CH2], 1550, 2000, 20, 238)-(map(rc_values[RC_CH1],1000, 1450, 230, 20)));
if (pwm < 0) {
pwm = 0; // make sure pwm is not negative
}
analogWrite(6, (pwm));
analogWrite(9, 0);
analogWrite(10, (map(rc_values[RC_CH2], 1550, 2000, 20, 250)));
analogWrite(11, 0);
} else {
}
//-----------Right spin
if ((rc_values[RC_CH1] > 1550) && (rc_values[RC_CH2] > 1450) && (rc_values[RC_CH2] < 1550)){
digitalWrite(enL, 1); //enable right
digitalWrite(enR, 1); //enable left
analogWrite(6, (map(rc_values[RC_CH1], 1550, 2000, 0, 100)));
analogWrite(9, 0);
analogWrite (10, 0);
analogWrite(11, (map(rc_values[RC_CH1], 1550, 2000, 0, 100)));
} else {
}
// --------left spin
if ((rc_values[RC_CH1] < 1450) && (rc_values[RC_CH2] > 1450) && (rc_values[RC_CH2] < 1550)){
digitalWrite(enL, 1); //enable right
digitalWrite(enR, 1); //enable left
analogWrite(6, 0);
analogWrite(9, (map(rc_values[RC_CH1], 1000, 1450, 100, 0)));
analogWrite(10, (map(rc_values[RC_CH1], 1000, 1450, 100, 0)));
analogWrite(11, 0);
}
// -----------Out of range
if ((rc_values[RC_CH2] < 900)) {
digitalWrite(enL, 0); //enable right
digitalWrite(enR, 0); //enable left
digitalWrite(LED, 1);
} else {
digitalWrite(LED, 0);
}
x++;
while(x > 60){ //print to serial every so often
serialPrint();
}
delay(20);
}
Comments