Hello everyone,
what I am working on is having buttons connected to a 4021 shift in register which i got to work fine then based on this input i want to turn LEDs on or off which are connected to a shift register 595. I must mention that I have 2 595 daisy chained. So my problem is that i don't know how to convert bool led1On etc to a byte. I need your help please.
This is my sketch :
#define Nextion Serial
/* define that Serial will be written as Nextion (when we write Nextion.print the compiler read Serial.print)
*By defining the Nextion as Serial we can change at once the serial port for the whole code,
* with changing the Serial to Serial1 for example
*/
// Defining the SPI library and RF24
// We must make sure to download RF24 library and place it in the arduino libraries folder
#include <SPI.h>
#include <RF24.h>
//#include <nRF24L01.h>
//1st byte is for throttle value from pot
//2nd byte is for brake value from pot
//3rd byte is for Steering Left state
//4th byte is for
//5th byte is for
//6th byte is for
//7th byte is for
//8th byte is for
//Steering potentiometers
const byte Throttle = A4;
//const byte Braking = A5;
//Define arduino pins connected to CD4021
int dataPin_4021 = 8; // Pin 0 of DigiSpark connected to Pin 3 of CD4021
int clockPin_4021 = 6; // Pin 1 of DigiSpark connected to Pin 10 of CD4021
int latchPin_4021 = 7; // Pin 2 of DigiSpark connected to Pin 9 of CD4021
// Define arduino pins for the 74HC595 shift register
int dataPin_595 = 4;
int clockPin_595 = 2;
int latchPin_595 = 3;
// Define variable that will hold the data from CD4021
byte RegisterValue = 0; // Used to hold data from CD4021
// Define the pins for push buttons for debouncing
boolean lastPushButton1 = LOW;
boolean currentPushButton1 = LOW;
boolean led1On = false;
boolean lastPushButton2 = LOW;
boolean currentPushButton2 = LOW;
boolean led2On = false;
boolean lastPushButton3 = LOW;
boolean currentPushButton3 = LOW;
boolean led3On = false;
boolean lastPushButton4 = LOW;
boolean currentPushButton4 = LOW;
boolean led4On = false;
boolean lastPushButton5 = LOW;
boolean currentPushButton5 = LOW;
boolean led5On = false;
boolean lastPushButton6 = LOW;
boolean currentPushButton6 = LOW;
boolean led6On = false;
boolean lastPushButton7 = LOW;
boolean currentPushButton7 = LOW;
boolean led7On = false;
boolean lastPushButton8 = LOW;
boolean currentPushButton8 = LOW;
boolean led8On = false;
// Gear Number variable
int gearNum = 0;
// array that contains the LEDs
bool LEDarray[] = {led1On, led2On, led3On, led4On, led5On, led6On};
byte leds = 0;
byte leds2 = 0;
void setup() {
Nextion.begin(9600);
delay(500); // give Nextion some time to finish initialize
Nextion.print("page 0"); // for synchronize Nextion in case of reset to Arduino
Nextion.print("\\xFF\xFF");
Nextion_serial_listen(); // reading from Nextion first time
read_page_selector(); // This is going to read the selector and load the selected page on Nextion for first start up.
//define arduino pins used to connect to the CD4021 Shift Register
pinMode(dataPin_4021, INPUT);
pinMode(latchPin_4021, OUTPUT);
pinMode(clockPin_4021, OUTPUT);
// define arduino pins used to connect to the 595 shift register
pinMode(dataPin_595, OUTPUT);
pinMode(latchPin_595, OUTPUT);
pinMode(clockPin_595, OUTPUT);
radio.begin();
radio.setDataRate(dataRate);
//radio.openWritingPipe(address);
radio.enableAckPayload(); //enables receiving data from receiver side
//radio.setPALevel(RF24_PA_MIN); //Power Amplifier (PA) level to one of four levels RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
//radio.stopListening();
}
void loop() {
int ThrottleValue = analogRead(Throttle); // read the value from the sensor
//ThrottleValue = map(ThrottleValue, 0, 1023, 255, 0); //middle throttle value 121
Nextion.println(ThrottleValue);
//int BrakingValue = analogRead(Braking); // read the value from the sensor
//SteeringValue = map(SteeringValue, 0, 1023, 0, 255); //middle steering value 125
int numLEDSLit = ThrottleValue / 126; //1023 / 9
for (int i = 0; i < numLEDSLit; i++)
{
bitSet(leds, i);
}
digitalWrite(latchPin_595, LOW);
// shift out the bits:
shiftOut(dataPin_595, clockPin_595, MSBFIRST, leds2);
shiftOut(dataPin_595, clockPin_595, MSBFIRST, leds);
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin_595, HIGH);
// We must set Clock Pin HIGH first in this chip CD4021 but not all of the chips!
digitalWrite(clockPin_4021,1);
// Set latch pin to 1 to get recent data into the CD4021
digitalWrite(latchPin_4021,1);
delayMicroseconds(20);
//Set latch pin to 0 to get data from the CD4021
digitalWrite(latchPin_4021,0);
//Get CD4021 register data in byte variable
RegisterValue = shiftIn(dataPin_4021, clockPin_4021, MSBFIRST);
// This is for first push button -----------------------
currentPushButton1 = debounce(lastPushButton1, 0);
if(lastPushButton1 == LOW && currentPushButton1 == HIGH)
{
led1On = !led1On;
}
lastPushButton1 = currentPushButton1;
// -------------------------------------------------
// This is for second push button
currentPushButton2 = debounce(lastPushButton2, 1);
if(lastPushButton2 == LOW && currentPushButton2 == HIGH)
{
led2On = !led2On;
}
lastPushButton2 = currentPushButton2;
// --------------------------------------------------
// This is for third push button
currentPushButton3 = debounce(lastPushButton3, 2);
if(lastPushButton3 == LOW && currentPushButton3 == HIGH)
{
led3On = !led3On;
}
lastPushButton3 = currentPushButton3;
// --------------------------------------------------
// This is for fourth push button
currentPushButton4 = debounce(lastPushButton4, 3);
if(lastPushButton4 == LOW && currentPushButton4 == HIGH)
{
led4On = !led4On;
}
lastPushButton4 = currentPushButton4;
// --------------------------------------------------
// This is for fifth push button
currentPushButton5 = debounce(lastPushButton5, 4);
if(lastPushButton5 == LOW && currentPushButton5 == HIGH)
{
led5On = !led5On;
}
lastPushButton5 = currentPushButton5;
// --------------------------------------------------
// This is for sixth push button
currentPushButton6 = debounce(lastPushButton6, 5);
if(lastPushButton6 == LOW && currentPushButton6 == HIGH)
{
led6On = !led6On;
}
lastPushButton6 = currentPushButton6;
// --------------------------------------------------
}
boolean debounce(boolean last, int bitToRead)
{
boolean current = bitRead(RegisterValue, bitToRead);
if(last != current)
{
delay(5);
current = bitRead(RegisterValue, bitToRead);
}
return current;
}