Nothing much there:
#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//NRF24L01 VCC ………………………………………… 3.3V of STM32F103C
//NRF24L01 CSN ………………………………………… PA4 of STM32F103C
//NRF24L01 MOSI ………………………………………… PA7 of STM32F103C
//NRF24L01 GND ………………………………………… GND of STM32F103C
//NRF24L01 CE ………………………………………… PB0 of STM32F103C
//NRF24L01 SCK ………………………………………… PA5 of STM32F103C
//NRF24L01 MISO ………………………………………… PA6 of STM32F103C
#define reverselights 4
#define headlights 5
#define stoplights 6
#define servomotor 9
#define goFWD 10
#define goBCK 11
#define honker 12
#define horisontal 13
#define leftturnsig 3
#define rightturnsig 2
bool emerlightsenable = false;
bool leftlightsenable = false;
bool rightlightsenable = false;
int triggertime = 500;
long currmillis = 0;
bool emerlights = false;
bool leftlights = false;
bool rightlights = false;
byte fwdthrottle = 0;
byte revthrottle = 0;
byte stterangle = 0;
byte frwrdthrottle = 0;
byte revrsthrottle = 0;
bool headlightON = false;
int steerangle = 110;
struct Signal {
byte leftx = 0;
byte lefty = 0;
byte rightx = 0;
byte righty = 0;
byte btn1 = 0;
byte btn2 = 0;
byte btn3 = 0;
byte btn4 = 0;
byte btn5 = 0;
byte btn6 = 0;
byte btn7 = 0;
byte btn8 = 0;
byte vari = 0;
};
Servo steering;
Servo hor;
Signal data;
const uint64_t pipeIn = 0xE9E8F0F0E1LL;
RF24 radio(7, 8); // CE, CSN
byte stopbrightness = 0;
void ResetData()
{
data.leftx = 127;
data.lefty = 0;
data.rightx = 0;
data.righty = 127;
data.btn1 = 0;
data.btn2 = 0;
data.btn3 = 0;
data.btn4 = 0;
data.btn5 = 0;
data.btn6 = 0;
data.btn7 = 0;
data.btn8 = 0;
data.vari = 0;
}
void debug()
{
char buffer[250];
sprintf(buffer, "Steering: %d, Gas: %d, Reverse: %d, Buttons: %d, %d, %d, %d, %d, %d", stterangle, fwdthrottle, revthrottle, data.btn1, data.btn2, data.btn3, data.btn4, data.btn5, data.btn6);
Serial.println(buffer);
}
void setup()
{
steering.attach(servomotor);
hor.attach(horisontal);
Serial.begin(9600);
ResetData();
radio.begin();
radio.openReadingPipe(1,pipeIn);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
steering.write(130);
analogWrite(stoplights, stopbrightness);
ResetData();
radio.read(&data, sizeof(Signal));
ResetData();
}
unsigned long lastRecvTime = 0;
void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(Signal));
lastRecvTime = millis(); // receive the data | data alınıyor
}
}
void loop()
{
//int starttime = millis();
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
ResetData(); // Signal lost.. Reset data | Sinyal kayıpsa data resetleniyor
}
#pragma region //Processing joystick's right Y axis(gas)
if(data.righty > 130)
{
frwrdthrottle = map(data.righty, 127, 255, 0, 255);
}
else if(data.righty < 120)
{
revrsthrottle = map(data.righty, 127, 0, 0, 255);
}
else
{
frwrdthrottle = 0; revrsthrottle = 0;
}
#pragma endregion
#pragma region //Emergency lights
if(data.btn4 == 1)
{
delay(200);
if(emerlightsenable == false)
{
emerlightsenable = true;
analogWrite(leftturnsig, 100);
analogWrite(rightturnsig, 100);
}
else
{
emerlightsenable = false;
analogWrite(leftturnsig, 0);
analogWrite(rightturnsig, 0);
}
currmillis = 0;
}
if(emerlightsenable == true)
{
if(millis() - currmillis > 500)
{
emerlights = !emerlights;
if(emerlights)
{
analogWrite(leftturnsig, 100);
analogWrite(rightturnsig, 100);
}
else
{
analogWrite(leftturnsig, 0);
analogWrite(rightturnsig, 0);
}
currmillis = millis();
}
}
#pragma endregion
#pragma region //Left turn lights
if(data.btn2 == 1)
{
delay(200);
if(leftlightsenable == false)
{
leftlightsenable = true;
analogWrite(leftturnsig, 100);
}
else
{
leftlightsenable = false;
analogWrite(leftturnsig, 0);
}
currmillis = 0;
}
if(leftlightsenable == true)
{
if(millis() - currmillis > 500)
{
leftlights = !leftlights;
if(leftlights)
{
analogWrite(leftturnsig, 100);
}
else
{
analogWrite(leftturnsig, 0);
}
currmillis = millis();
}
}
#pragma endregion
#pragma region //Right turn lights
if(data.btn3 == 1)
{
delay(200);
if(rightlightsenable == false)
{
rightlightsenable = true;
analogWrite(rightturnsig, 100);
}
else
{
rightlightsenable = false;
analogWrite(rightturnsig, 0);
}
currmillis = 0;
}
if(rightlightsenable == true)
{
if(millis() - currmillis > 500)
{
rightlights = !rightlights;
if(rightlights)
{
analogWrite(rightturnsig, 100);
}
else
{
analogWrite(rightturnsig, 0);
}
currmillis = millis();
}
}
#pragma endregion
#pragma region //Honker check
if(data.btn6 == 1)
{
tone(honker, 300);
}
else
{
noTone(honker);
}
#pragma endregion
#pragma region //Headlights switch
if(data.btn1 == 1)
{
headlightON = !headlightON;
delay(200);
}
if(headlightON)
{
analogWrite(headlights, 220);
stopbrightness = 5;
}
else{
analogWrite(headlights, 0);
stopbrightness = 0;
}
#pragma endregion
#pragma region //Steering
stterangle = map(data.leftx, 0,255,0,120);
steering.write(stterangle);
#pragma endregion
#pragma region ///Processing smooth gas
if(frwrdthrottle > fwdthrottle)
{
fwdthrottle++;
delay(2);
}
else if(frwrdthrottle < fwdthrottle)
{
fwdthrottle--;
delay(2);
}
else if(frwrdthrottle == fwdthrottle)
{
fwdthrottle = frwrdthrottle;
}
if(revrsthrottle > revthrottle)
{
revthrottle++;
delay(2);
}
else if(revrsthrottle < revthrottle)
{
revthrottle--;
delay(2);
}
else if(revrsthrottle == revthrottle)
{
revthrottle = revrsthrottle;
}
analogWrite(goBCK, revthrottle);
analogWrite(goFWD, fwdthrottle);
#pragma endregion
#pragma region //Stoplights setup
if((frwrdthrottle + revrsthrottle != 0))
{
analogWrite(stoplights, stopbrightness);
}
else
{
analogWrite(stoplights, stopbrightness + 100);
}
#pragma endregion
#pragma region //Reverse lights control
if(revrsthrottle > frwrdthrottle)
{
analogWrite(reverselights, 200);
}
else
{
analogWrite(reverselights, 0);
}
#pragma endregion
//debug();
//int thistime = millis() - starttime;
//Serial.print("loop takes "); Serial.print(thistime); Serial.println(" milliseconds");
}