I need help with my Arduino code. I’m using a arduino wii nunchuck and home made arduino receiver both using the nrf2401 .I have got it all working fine like I want but I don’t really know a lot about Arduino code that’s why I’m here. I would like it so when I press the ‘C’ Button it remembers the ‘throttle’ anolog input and instead if outputting the anolog input to the esc it instead outputs the last anolog input . so basictally a cruse control . so when i click the c button it stays at the speed the anolog was last at and when i release the c button it goes back to reading the anolog input again . NOTE only remember last anolog IF the anolog value is 127 or higher (if the value was under 127 the motor would be in reverse and id go backwards) also I have dyslexia so sorry about the poor writing lol . Thanks
If i get help I will post the wiring diagrams and the transmitter code To thank the community.
THE CODE:
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #include <Servo.h> //To create PWM signals we need this lybrary
const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter RF24 radio(9, 10); //CSN and CE pins
// The sizeof this struct should not exceed 32 bytes struct Received_data { byte ch1; //Throttle byte ch2; //Z byte ch3; //C byte ch4; byte ch5; byte ch6; byte ch7; };
Received_data received_data;
Servo channel_1; //Throttle Servo channel_2; //Z Servo channel_3; //C Servo channel_4; Servo channel_5; Servo channel_6; Servo channel_7;
int ch1_value = 0; //Throttle int ch2_value = 0; //Z int ch3_value = 0; //C int ch4_value = 0; int ch5_value = 0; int ch6_value = 0; int ch7_value = 0;
/**************************************************/
void setup() {
//Break lights output pinMode(A1, OUTPUT);
//For Testing //Serial.begin(9600); //make sure the same is selected in serial monitor!!
//Attach the servo signal on pins from D2 to D8 channel_1.attach(2); //Throttle channel_2.attach(3); //Z channel_3.attach(4); //C channel_4.attach(5); channel_5.attach(6); channel_6.attach(7); channel_7.attach(8);
//We reset the received values received_data.ch1 = 127; //Throttle received_data.ch2 = 0; //Z received_data.ch3 = 0; //C received_data.ch4 = 0; received_data.ch5 = 0; received_data.ch6 = 0; received_data.ch7 = 0;
//Once again, begin and radio configuration
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipeIn);
//We start the radio comunication radio.startListening();
}
/**************************************************/
unsigned long last_Time = 0;
//We create the function that will read the data each certain time void receive_the_data() { while ( radio.available() ) { radio.read(&received_data, sizeof(Received_data)); last_Time = millis(); //Here we receive the data } }
/**************************************************/
void loop() { //Receive the radio data receive_the_data();
ch1_value = map(received_data.ch1,0,255,1000,2000); //Throttle ch2_value = map(received_data.ch2,0,255,1000,2000); //Z ch3_value = map(received_data.ch3,0,255,1000,2000); //C ch4_value = map(received_data.ch4,0,255,1000,2000); ch5_value = map(received_data.ch5,0,1,1000,2000); ch6_value = map(received_data.ch6,0,1,1000,2000); ch7_value = map(received_data.ch7,0,255,1000,2000);
//Creathe the PWM signals
channel_1.writeMicroseconds(ch1_value); //Throttle
channel_2.writeMicroseconds(ch2_value); //Z
channel_3.writeMicroseconds(ch3_value); //C
channel_4.writeMicroseconds(ch4_value);
channel_5.writeMicroseconds(ch5_value);
channel_6.writeMicroseconds(ch6_value);
channel_7.writeMicroseconds(ch7_value);
//For Testing //Serial.print("ch1: “); Serial.print(received_data.ch1); Serial.print(” "); //Throttle //Serial.print("ch2: “); Serial.print(received_data.ch2); Serial.print(” "); //Z //Serial.print("ch3: “); Serial.print(received_data.ch3); Serial.print(” "); //C //Serial.print("ch4: “); Serial.print(received_data.ch4); Serial.print(” "); //Serial.print("ch5: “); Serial.print(received_data.ch5); Serial.print(” "); //Serial.print("ch6: “); Serial.print(received_data.ch6); Serial.print(”\n ");
//If the throttle is equal or below 90 turn Break lights on. if (received_data.ch1 <= 90){ digitalWrite(A1, HIGH); //Serial.print("Led: High "); }else{ digitalWrite(A1, LOW); //Serial.print("Led: Low "); }}
//Loop end