Hi everyone! I am working on an RC system. The basic exersise is, I want to controll a DC motor with an nrf24l01 based joystick controller, and I would like to controll a gear motor in the same time also with this controller with a toggle switch. The most neccesary thing, the gear motor have to rotate while the upper or the lower limitswitch isn't pushed, and I want to change the rotating direction with the toggleswitch.
Now I made this code, and the problem is, I have to push the limitswitch more, after and after, so the rotation of the motor don't stop when a microswitch is pushed just ones. I think I have to use a millis, but I don't know where.
Summary: I have to make program for an nrf24l01 based transmitter and reciver, where I can controll a DC motor with a joystick and a gear motor with a toggleswitch in the same time, and these things are on an RC Controller. And I would like to stop the rotation of the gear motor when an upper or a lower limitswitch is pushed. I don't kniw how can I fix this problem, maybe have to use millis() at the correct position. Thank you for your help.
The code for Transmitter:
#include <SPI.h>
#include<nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <printf.h>
const int button_up=2;
const int button_down=3;
const int toggle_switch=9;
RF24 radio(8,10);
const byte address[6]= "00001";
struct Data_Package{
byte leftJoyX;
byte leftJoyY;
byte toggleSwitch;
};
Data_Package data;
void setup() {
Serial.begin(9600);
radio.begin();
printf_begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
radio.printDetails();
radio.enableDynamicPayloads();
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(false);
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
pinMode(toggle_switch, INPUT_PULLUP);
data.leftJoyX=127;
data.leftJoyY=127;
data.toggleSwitch=1;
}
void loop() {
data.leftJoyX = map(analogRead(A0), 0, 1023, 0, 255);
data.leftJoyY = map(analogRead(A1), 0, 1023, 0, 255);
data.toggleSwitch = digitalRead(toggle_switch);
Serial.println(data.toggleSwitch);
radio.write(&data,sizeof(Data_Package));'
The code for Reciever:
#include <SPI.h>
#include<nRF24L01.h>
#include <RF24.h>
#include <printf.h>
#include <Servo.h>
#define driveMotorsLeftEnA 7
#define driveMotorsLeftIn1 6
#define driveMotorsLeftIn2 5
#define driveMotorsRightEnB 2
#define driveMotorsRightIn3 4
#define driveMotorsRightIn4 3
#define gearMotorIn3 A1
#define gearMotorIn4 A0
#define gearMotorEnB 9
#define microSwitchUp A2
#define microSwitchDown A4
RF24 radio(8,10);
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime =0;
unsigned long currentTimeMicro=0;
unsigned long prevTimeMicro = 0;
Servo servo1;
struct Data_Package{
byte leftJoyX;
byte leftJoyY;
byte toggleSwitch;
};
Data_Package data;
int leftJoyXValue;
int leftJoyYValue;
int toggleSwitchValue;
int motorSpeedDriveMotorsLeft=0;
int motorSpeedDriveMotorsRight=0;
int motorSpeedGearMotor=0;
int microSwitchDownVal=0;
int microSwitchUpVal=0;
int readyToBait=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
printf_begin();
radio.openReadingPipe(0,address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
radio.printDetails();
radio.enableDynamicPayloads();
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(false);
pinMode(driveMotorsLeftEnA, OUTPUT);
pinMode(driveMotorsLeftIn1, OUTPUT);
pinMode(driveMotorsLeftIn2, OUTPUT);
pinMode(driveMotorsRightEnB, OUTPUT);
pinMode(driveMotorsRightIn3, OUTPUT);
pinMode(driveMotorsRightIn4, OUTPUT);
pinMode(gearMotorIn3, OUTPUT);
pinMode(gearMotorIn4, OUTPUT);
pinMode(gearMotorEnB, OUTPUT);
pinMode(microSwitchUp, INPUT_PULLUP);
pinMode(microSwitchDown, INPUT_PULLUP);
digitalWrite(gearMotorIn3, LOW);
digitalWrite(gearMotorIn4, LOW);
digitalWrite(driveMotorsLeftIn1, LOW);
digitalWrite(driveMotorsLeftIn2, LOW);
digitalWrite(driveMotorsRightIn3, LOW);
digitalWrite(driveMotorsRightIn4, LOW);
analogWrite(driveMotorsLeftEnA, 0);
analogWrite(driveMotorsRightEnB, 0);
analogWrite(gearMotorEnB, 0);
}
void loop() {
// put your main code here, to run repeatedly:
/*if (radio.available()){
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}*/
currentTime = millis();
if (currentTime - lastReceiveTime > 1000){
resetData();
}
if (radio.available()){
radio.read(&data, sizeof(Data_Package));
lastReceiveTime = millis();
}
leftJoyXValue = data.leftJoyX;
leftJoyYValue = data.leftJoyY;
toggleSwitchValue = data.toggleSwitch;
/*Serial.println(leftJoyXValue);*/
/*Serial.println(toggleSwitchValue);
Serial.print(digitalRead(microSwitchUp));
Serial.print(digitalRead(microSwitchDown));*/
/*Drive*/
if (leftJoyXValue < 110){
digitalWrite(driveMotorsLeftIn1, HIGH);
digitalWrite(driveMotorsLeftIn2, LOW);
digitalWrite(driveMotorsRightIn3, HIGH);
digitalWrite(driveMotorsRightIn4, LOW);
motorSpeedDriveMotorsLeft = map(leftJoyXValue, 110, 0, 0, 255);
motorSpeedDriveMotorsRight = map(leftJoyXValue, 110, 0, 0, 255);
}
else if (leftJoyXValue>140){
digitalWrite(driveMotorsLeftIn1, LOW);
digitalWrite(driveMotorsLeftIn2, HIGH);
digitalWrite(driveMotorsRightIn3, LOW);
digitalWrite(driveMotorsRightIn4, HIGH);
motorSpeedDriveMotorsLeft = map(leftJoyXValue, 140, 255, 0, 255);
motorSpeedDriveMotorsRight = map(leftJoyXValue, 140, 255, 0, 255);
}
else {
motorSpeedDriveMotorsLeft=0;
motorSpeedDriveMotorsRight=0;
}
/*Steering*/
if (leftJoyYValue < 110) {
int yMapped = map(leftJoyYValue, 110,0,0,255);
motorSpeedDriveMotorsLeft=motorSpeedDriveMotorsLeft - yMapped;
motorSpeedDriveMotorsRight=motorSpeedDriveMotorsRight + yMapped;
if(motorSpeedDriveMotorsLeft<0){
motorSpeedDriveMotorsLeft=0;
}
if(motorSpeedDriveMotorsRight>255){
motorSpeedDriveMotorsRight=255;
}
}
if (leftJoyYValue > 140) {
int yMapped = map(leftJoyYValue, 140,255,0,255);
motorSpeedDriveMotorsLeft=motorSpeedDriveMotorsLeft + yMapped;
motorSpeedDriveMotorsRight=motorSpeedDriveMotorsRight - yMapped;
if(motorSpeedDriveMotorsLeft>255){
motorSpeedDriveMotorsLeft=255;
}
if(motorSpeedDriveMotorsRight<0){
motorSpeedDriveMotorsRight=0;
}
}
if (motorSpeedDriveMotorsLeft < 70) {
motorSpeedDriveMotorsLeft = 0;
}
if (motorSpeedDriveMotorsRight < 70) {
motorSpeedDriveMotorsRight = 0;
}
analogWrite(driveMotorsLeftEnA, motorSpeedDriveMotorsLeft);
analogWrite(driveMotorsRightEnB, motorSpeedDriveMotorsRight);
if (toggleSwitchValue==0 && digitalRead(microSwitchUp) == HIGH) { // Billenő kapcsoló felső állapotban
readyToBait=1;
motorSpeedGearMotor = 50;
digitalWrite(gearMotorIn3, LOW);
digitalWrite(gearMotorIn4, HIGH);
analogWrite(gearMotorEnB, motorSpeedGearMotor);
microSwitchUpVal = digitalRead(microSwitchUp);
Serial.println(microSwitchUpVal);
if (toggleSwitchValue==0 && microSwitchUpVal == LOW && readyToBait == 1){
motorSpeedGearMotor = 0;
digitalWrite(gearMotorIn3, LOW);
digitalWrite(gearMotorIn4, LOW);
analogWrite(gearMotorEnB, motorSpeedGearMotor);
readyToBait=0;
Serial.println("belépett az if-be");
Serial.print(readyToBait);
}
}
if (toggleSwitchValue==1 && digitalRead(microSwitchDown) == HIGH) { // A billenő kapcsoló alsó állapotban
readyToBait=1;
motorSpeedGearMotor = 50;
analogWrite(gearMotorEnB, motorSpeedGearMotor);
digitalWrite(gearMotorIn3, HIGH);
digitalWrite(gearMotorIn4, LOW);
microSwitchDownVal = digitalRead(microSwitchDown);
Serial.println(microSwitchDownVal);
if (toggleSwitchValue==1 && microSwitchDownVal == LOW && readyToBait == 1){
motorSpeedGearMotor = 0;
digitalWrite(gearMotorIn3, LOW);
digitalWrite(gearMotorIn4, LOW);
analogWrite(gearMotorEnB, motorSpeedGearMotor);
readyToBait=0;
Serial.println("belépett az if-be 1");
Serial.print(readyToBait);
}
}
}
void resetData(){
data.leftJoyX=127;
data.leftJoyY=127;
data.toggleSwitch=0;
}