Hi I would appreciate help with the following codes. My project is a 12 LED clock, a LED is on for 5 mins. then next and start again. I can get LEDs blinking every whcih way but not a long string of 12 LED . I am studying Millis and trying to reduce Delay
Using Arduino Uno 1.8.18 The test setup is a simple 4 LED hook up with resistors
I am a bit mistified here as posting code within the tags , the code does not apeear in color
The first code is great but the Timer function freezes if more than 30 secs.
/*
TIMER freezes slower than 30 secs
ARRAYS https://www.arduino.cc/en/Tutorial/BuiltInExamples/Arrays
30,000 = 30 secs 300,000 = 5 mins 50,000 = .83 mins
int timer = 5UL*60UL*1000UL; // 1,200,000 = 20 mins // 1,800,000 = 30 mins
*/
int timer = 1000;
int ledPins[] = { 4, 5, 6, 7};
int pinCount = 4;
void setup()
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop()
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
/////////////////////////////////////////////
the next code is brilliant but my knowledge of C++ is insuffecient to change it
/* previous LED slow to turn off
a brilliant sketch by paulpaulson
from forum "how do I blink multiple LEDs in sequence"
*/
int ledPin9 = 9;
int ledPin8 = 8;
int ledPin7 = 7;
int ledPin6 = 6;
int ledPin5 = 5;
int ledPin4 = 4;
int ledPin3 = 3;
int ledPin2 = 2;
/// new
byte ledSequ [][8]
{
{1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 1},
};
// new
byte leds[] {ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7, ledPin8, ledPin9};
void setup()
{
Serial.begin(9600);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
const unsigned long blink = 1000;
static unsigned long blinkMillis;
if (millis() - blinkMillis >= blink ) {
static int nummer;
blinkMillis = millis();
Serial.println("blink");
for (unsigned int n = 0; n < sizeof(leds); n++) digitalWrite(leds[n], ledSequ[nummer][n]);
nummer++;
nummer = nummer % (sizeof(ledSequ) / sizeof(leds));
}
}
/////////////////////////////////////////////////
The next code is twoledswalking which I am unable to modify
/*
need single LEDs blinking in sequence
from wokwi twoledswalking.ino
*/
const int ledPin[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int numPins = sizeof(ledPin) / sizeof(ledPin[0]);
const int numStates = numPins / 2;
const unsigned long interval = 1000;
unsigned long previousTime;
int state;
void setup()
{
for ( int i = 0; i < numPins; ++i )
{
pinMode(ledPin[i], OUTPUT);
digitalWrite(ledPin[i], LOW );
}
digitalWrite(ledPin[0], HIGH );
digitalWrite(ledPin[1], HIGH );//
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousTime >= interval) {
previousTime = currentMillis;
digitalWrite(ledPin[state * 2], LOW);
digitalWrite(ledPin[state * 2 + 1], LOW);
state++;
if ( state >= numStates ) {
state = 0;
}
digitalWrite(ledPin[state * 2], HIGH);
digitalWrite(ledPin[state * 2 + 1], HIGH);
}
}
Thanks for having a look at this.
This a great forum with some very good programmers