Hi there,
currently, I am working on some code which will later be used to illuminate a model kit.
This Model kit already comes with some light-effects, which I want to rework and change...
I already got some help on the code, since I do have a few very specific cases - and due to the amount of different inputs and outputs, I slowly run out of pins on my Arduino.
Honestly speaking, I am not very familiar with the Arduino - I am still learning some very basic stuff - and my programming skills are also not very good (especially in case of C / C++) - So I really do have my difficulties with all that stuff.
Anyway - to solve the issue with not having enought IO Pins available, I thought, I should give it a try with an 74HC595 ... I think, it should be possible
Since my main code will have some "fading" LEDs, I want to keep the PWM enabled pins for these LEDs on the Arduino - and change all LED, which does not need any PWM functionallity to the 74HC595 IC.
But - again - due to my lack of knowledge, I am struggling with changing my Code (and the classes provided by some others here from the forum) - to work with the IC.
At least, right now, I do not really have any Idea on how to start...
Right now, this is some of the code I do have at the moment...
Since I am trying to rewrite the whole project, I decided to start again with a few basics...
/*
Shift Register with 74HC595 possible
*/
// const byte _srData = 13; // 74HC595-DS
// const byte _srLatch = 12; // 74HC595-STCP
// const byte _srClock = 8; // 74HC595-SHCP
// const byte _srClear; // 74HC595-MR
// PWM functionallity not required
const byte _torpedolight = 4; // will be moved to 74HC595-Q4
const byte _positionlight = 8; // will be moved to 74HC595-Q2
const byte _navigationlight = 12; // will be moved to 74HC595-Q1
const byte _beaconlight = 13; // will be moved to 74HC595-Q3
const byte _torpedolightButton = A0;
const unsigned long _torpedolightOnTime = 120;
const unsigned long _torpedolightOffTime = 450;
const unsigned long _navigationlightOnTime = 500;
const unsigned long _navigationlightOffTime = 1000;
const unsigned long _positionlightOnTime = 500;
const unsigned long _positionlightOffTime = 500;
const unsigned long _beaconlightOnTime = 20;
const unsigned long _beaconlightOffTime = 1000;
unsigned long lastNavigationTime;
unsigned long lastPositionTime;
const byte _torpedolightBlinkAmount = 3;
//---------------------------------------------------------------------------------------------------------------------------------
class Flasher {
protected:
const byte _ledPin;
const uint32_t _onPhase;
const uint32_t _offPhase;
uint32_t timeMarker = 0;
enum _step : byte {Start, OnPhase, OffPhase};
Flasher::_step step = _step::Start;
public:
Flasher(const byte ledPin, const unsigned long onPhase, const unsigned long offPhase): _ledPin(ledPin), _onPhase(onPhase), _offPhase(offPhase) {}
void run(byte & blinkAmount) {
uint32_t now = millis ();
switch(step) {
case _step::Start:
if(blinkAmount) {
digitalWrite(_ledPin, HIGH);
step = _step::OnPhase;
timeMarker = now;
}
break;
case _step::OnPhase:
if(now - timeMarker >= _onPhase) {
digitalWrite (_ledPin, LOW);
step = _step::OffPhase;
timeMarker = now;
}
break;
case _step::OffPhase:
if(now - timeMarker >= _offPhase) {
if(blinkAmount > 0) {
blinkAmount --;
step = _step::Start;
}
}
}
}
void init() {
pinMode(_ledPin, OUTPUT);
}
};
//---------------------------------------------------------------------------------------------------------------------------------
enum FlashingGroup : byte {TORPEDO, BEACON};
Flasher flashingGroup[] = {
{_torpedolight, _torpedolightOnTime, _torpedolightOffTime},
{_beaconlight, _beaconlightOnTime, _beaconlightOffTime},
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()
{
// Debug - might be disabled if we run out of pins...
Serial.begin(115200);
Serial.println(F("Start..."));
lastNavigationTime = millis();
lastPositionTime = lastNavigationTime;
// The Pins that will be used by the 74HC595 should be set to OUTPUT
// pinMode(_srData, OUTPUT);
// pinMode(_srClock, OUTPUT);
// pinMode(_srLatch, OUTPUT);
// Current PINs for the LED - set to Output
pinMode(_srClear, OUTPUT);
pinMode(_navigationlight, OUTPUT);
pinMode(_positionlight, OUTPUT);
// The Button that will trigger the "Torpedo Light" needs to be set as an Input...
pinMode(_torpedolightButton, INPUT_PULLUP);
// Init of the "Flash" Class, used for the Beacon Light and the Torpedo Light...
for(Flasher &flash : flashingGroup) {
flash.init();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
/*
Since the loop() will include much more code later, it should be considered to use as many single functions as possible...
*/
// I do have a single function for all blinking lights...
BlinkingLightGroup(1); // Blinking Beacon Light
BlinkingLightGroup(2); // Blinking Torpedo Light
BlinkingLightGroup(3); // Blinking Navigation & Position Lights
}
//---------------------------------------------------------------------------------------------------------------------------------
void BlinkingLightGroup(byte type) {
static byte _beaconflash = 0;
static byte _torpedoamount = 0;
switch (type) {
case 1: // This will set the beacon light to "infinite"
_beaconflash = 1;
break;
case 2: // The Torpedo Light should only be activated if a button is pressed, and then blink three times in a row
if(!digitalRead(_torpedolightButton))
{
_torpedoamount = _torpedolightBlinkAmount;
}
break;
case 3: // Due to the specific timings between position- and navigation lights, this will no longer be handled by the 'Flasher"-Class.
// Instead, I am using this functionallity - which can either be handled within this case-block, or moved to another function as well in order to get a better readability.
unsigned long istTime = millis();
if (istTime - lastNavigationTime >= _navigationlightOnTime && digitalRead(_navigationlight))
{
lastNavigationTime += _navigationlightOnTime;
digitalWrite(_navigationlight, LOW);
}
if (istTime - lastNavigationTime >= _navigationlightOffTime && !digitalRead(_navigationlight))
{
lastNavigationTime += _navigationlightOffTime;
digitalWrite(_navigationlight, HIGH);
}
if (istTime - lastPositionTime >= _positionlightOnTime && digitalRead(_positionlight))
{
lastPositionTime += _positionlightOnTime;
digitalWrite(_positionlight, LOW);
}
if (istTime - lastPositionTime >= _positionlightOffTime && !digitalRead(_positionlight))
{
lastPositionTime += _positionlightOffTime;
digitalWrite(_positionlight, HIGH);
}
break;
}
flashingGroup[BEACON].run(_beaconflash);
flashingGroup[TORPEDO].run(_torpedoamount);
}
//---------------------------------------------------------------------------------------------------------------------------------
So, can anyone provide some support, what / where and how I could do the changes to use the 74HC595?
Thank you so much - any help is much appreciated