// Timing of 4 things, starting 40ms after each other, staying active 140ms.
//
// Forum: https://forum.arduino.cc/t/asking-questions-for-i2c-channel-communication/1186203/16
// This Wokwi project: https://wokwi.com/projects/380692972187150337
//
// I can think of 4 good solutions.
// This is with millis.
// The code will do that sequence at the press of a button.
// table:
// first: time in ms, table must be sequential
// second: pin number
// third: HIGH or LOW
const int table[8][3] =
{
{ 0, 4, HIGH},
{ 40, 5, HIGH},
{ 80, 6, HIGH},
{ 120, 7, HIGH},
{ 140, 4, LOW},
{ 180, 5, LOW},
{ 220, 6, LOW},
{ 260, 7, LOW},
};
const int buttonPin = 2;
unsigned long previousMillis;
unsigned long interval;
bool enable = false;
int index = 0;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
int button = digitalRead(buttonPin);
if(button == LOW and !enable)
{
enable = true; // enable the millis timer
index = 0; // start with the first action
interval = table[index][0]; // to start at the beginning of the table
previousMillis = currentMillis; // start here and now
}
if(enable)
{
if(currentMillis - previousMillis >= interval)
{
digitalWrite(table[index][1], table[index][2]);
index++;
if(index >= 8) // the table is only 0...7
{
// stop the sequence
enable = false;
}
else
{
// set to the next interval
interval = table[index][0];
}
}
}
}