hello,
i am working on my final sketch of a small project which is:
i have a small RC car on which i installed a hall effect switch on the wheel so i can calculate the speed of the car, and i have 4 RED LEDs which will work like the knight rider effect connected to a shift register and the shift register to arduino, and another 4 Blue LEDs that simply go on or off on command and connected directly to arduino and of a module nRF24L01+ connected to arduino.
On the controller side, i have a module nRF24L01+ connected to arduino, 2 pushbuttons, 2 LEDs, and a crystal LCD. The LCD will give me the speed of the car and will print On or OFF when i click the push buttons. These push buttons will lights up the 4 red LEDs on the car or the blue LEDs.
i tested the hardware side, all works great, now when i uploaded the sketch i managed to make most of the things work, 2 small problems :
1 - i am not getting an ackPayload which has the speed value from the car on the controller side, Now i get a value but the value which should be 1 or 2 digits example 8 KM/H, instead i get 8254 KM/H, so i get 3 more digits after 1 seconds or around that which are not shown on the Serial Monitor, it first starts 0 KM/H or 1 digit and after 1 sec or around that i get 3 more digits !
here is a video which i uploaded that shows the problem - YouTube
2 - when i turn the red LEDs off, they have a small delay until the for loop() in my codes finished (that's how i explained it so far :/)
3 - the speed starts at 0 but when i stop throttle, it doesn't go back to 0, instead it gives me 2 or 3
i have spent hours tested and experimented with the codes but i couldn't figure out what is going wrong
so i need some help please
here are the codes and thanks for everyone in advance :
Transmitter, Controller side
/*December 9, 2015
RC car speedometer and display screen, Controller sketch, mainly considered a Transmitter
-------------------------------------- By Firas Helou -------------------------------------
RF Module used : nRF24L01+
*/
//the RF24 for the RF Module Library
#include <SPI.h>
#include <RF24.h>
#include <LiquidCrystal.h> //LCD Library
int msgTX[3]; //Message to be transmitted, can contain up to 3 array elements, 3 bytes
int ackMessage[1]; //Acknowledgment message, means the message that will be received from the receiver or the car, 1 element for the moment
//Defining radio object for the RF24 function
RF24 radio(9,10);
//Defining the radio variables and values
const uint64_t pipe = 0xE8E8F0F0E1LL; //pipe address
const rf24_datarate_e dataRate = RF24_250KBPS; //Data rate defined in the documentations, RF24_250KBPS, RF24_1MBPS or RF24_2MBPS
//INDICATOR LEDs of the controller
const byte Red_LED = 2; //Top LEDs ON/OFF indicator
const byte Blue_LED = 4; //Bottom LEDs ON/OFF indicator
//PUSH BUTTONS
const byte RedLEDs_Button = 6; //Push Button that turns the Top red LEDs on
const byte BlueLEDs_Button = 7; //Push Button that turns the Bottom blue LEDs on
boolean lastRedLED = LOW;
boolean currentRedLED = HIGH;
boolean RedLEDOn = false;
boolean lastBlueLED = LOW;
boolean currentBlueLED = HIGH;
boolean BlueLEDOn = false;
boolean valRedLEDButton = 0;
boolean valBlueLEDButton = 0;
//SPEED value
unsigned long Speed = 0;
//LCD PINS
const byte LCD_RS = 3;
const byte LCD_Enable = 5;
const byte LCD_D4 = 8;
const byte LCD_D5 = A0;
const byte LCD_D6 = A1;
const byte LCD_D7 = A2;
//Defining LCD pins
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7); //defining the LCD pins
//LCD letter O
byte letter_O[8] = {
0b01110,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b01110,
0b00000
};
//LCD letter N
byte letter_N[8] = {
0b10001,
0b10001,
0b11001,
0b10101,
0b10011,
0b10001,
0b10001,
0b00000
};
//LCD letter F
byte letter_F[8] = {
0b11111,
0b10000,
0b10000,
0b11110,
0b10000,
0b10000,
0b10000,
0b00000
};
//LCD empty
byte empty[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
//VOID SETUP()
void setup()
{
Serial.begin(9600);
lcd.createChar(0,letter_O); // define our characters into the sketch as variables
lcd.createChar(1,letter_N);
lcd.createChar(2,letter_F);
lcd.createChar(3,empty);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print("KM/H:");
pinMode( RedLEDs_Button, INPUT_PULLUP ); //RedLeds Button
pinMode( BlueLEDs_Button, INPUT_PULLUP ); //BlueLeds Button
pinMode( Red_LED, OUTPUT ); //Red Led indicator Top LEDs
pinMode( Blue_LED, OUTPUT ); //Blue Led indicator Bottom LEDs
//radio start
radio.begin();
radio.setDataRate(dataRate); //Giving the data rate speed
radio.enableAckPayload(); //enables receiving data from receiver side
}
//DEBOUNCING FUNCTION
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button);
if(last != current)
{
delay(5);
current = digitalRead(button);
}
return current;
}
//MAIN LOOP FUNCTION
void loop()
{
LCD_Display_Function();
getPushButtonStates();
DataTransmission();
AcknowledgmentDATA();
Serial.println(ackMessage[0]);
}
//FUNCTION that takes care of the Push Buttons
void getPushButtonStates()
{
currentRedLED = debounce(lastRedLED, RedLEDs_Button);
currentBlueLED = debounce(lastBlueLED, BlueLEDs_Button);
//For the BLUE LEDs button
if(lastRedLED == HIGH && currentRedLED == LOW){
RedLEDOn = !RedLEDOn;
//valRedLEDButton = 1;
}
lastRedLED = currentRedLED;
digitalWrite(Red_LED, RedLEDOn);
//For the RED LEDs button
if(lastBlueLED == HIGH && currentBlueLED == LOW)
{
BlueLEDOn = !BlueLEDOn;
valBlueLEDButton = !valBlueLEDButton;
}
lastBlueLED = currentBlueLED;
digitalWrite(Blue_LED, BlueLEDOn);
}
//Function that takes care of the LCD display
void LCD_Display_Function()
{
lcd.setCursor(0, 1);
lcd.print("T-L:");
//For the Red LED
if(RedLEDOn == HIGH)
{
//TopLed = "ON ";
lcd.setCursor(4, 1);
lcd.write(3);
lcd.setCursor(5, 1);
lcd.write((byte)0);
lcd.setCursor(6,1);
lcd.write(1);
}else if(RedLEDOn == LOW){
//TopLed = "OFF";
lcd.setCursor(4, 1);
//lcd.print(TopLed);
lcd.write((byte)0);
lcd.setCursor(5,1);
lcd.write(2);
lcd.setCursor(6,1);
lcd.write(2);
}
//For the Blue LED
lcd.setCursor(9, 1);
lcd.print("B-L:");
if(BlueLEDOn == HIGH)
{
//BottomLed = "ON";
lcd.setCursor(13, 1);
lcd.write(3);
lcd.setCursor(14, 1);
lcd.write((byte)0);
lcd.setCursor(15,1);
lcd.write(1);
}else if(BlueLEDOn == LOW){
lcd.setCursor(13, 1);
lcd.write((byte)0);
lcd.setCursor(14,1);
lcd.write(2);
lcd.setCursor(15,1);
lcd.write(2);
}
//Prints the Speed Value on the LCD
lcd.setCursor(10, 0);
lcd.print(Speed);
}
//DATA Transmission Function
void DataTransmission(){
if(RedLEDOn == HIGH) {
radio.openWritingPipe(pipe); //Opens Pipe 0 for Writing
valRedLEDButton = 1;
msgTX[0] = valRedLEDButton;
}else{
radio.openWritingPipe(pipe); //Opens Pipe 0 for Writing
valRedLEDButton = 0;
msgTX[0] = valRedLEDButton;
}
if(BlueLEDOn == HIGH){
radio.openWritingPipe(pipe);
valBlueLEDButton = 1;
msgTX[1] = valBlueLEDButton;
}else{
radio.openWritingPipe(pipe);
valBlueLEDButton = 0;
msgTX[1] = valBlueLEDButton;
}
radio.write(msgTX, sizeof(msgTX)); //Sends the Data
}
//DATA Receiving from the Receiver part, Acknowledgment Data
void AcknowledgmentDATA(){
if ( radio.isAckPayloadAvailable() ){
radio.read(ackMessage, sizeof(ackMessage));
ackMessage[0] = Speed ;
//delay(5);
}else{
Serial.println("No connection is made");
}
}
and this is the Receiver, Car side in the first comment