Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active December 8, 2025 12:24
Show Gist options
  • Select an option

  • Save psychemist/e6c1b6a449a3d14eac14723be9b0cb2b to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/e6c1b6a449a3d14eac14723be9b0cb2b to your computer and use it in GitHub Desktop.
Arduino Starter Project 8
const int switchPin = 8;
unsigned long int prevTime = 0;
long interval = 300000;
int switchState = 0;
int prevSwitchState = 0;
int nextLED = 2;
void setup() {
// set direction of digital pins
for( int x = 2; x < 8; x++ ) {
pinMode(x, OUTPUT);
}
pinMode(switchPin, INPUT);
}
void loop() {
// save time whenn program started running
unsigned long currentTime = millis();
// evaluate amount of time that has passed since previous loop
if( currentTime - prevTime > interval ) {
// update previous Time to current Time
prevTime = currentTime;
// turn on LED, prepare for next one
digitalWrite(nextLED, HIGH);
nextLED++;
delay(500);
// at the end of time period,
if ( nextLED >= 8 ) {
// toggle even digital pins
for ( int x = 2; x < 8; x++ ) {
if ( x % 2 == 0 ) {
digitalWrite(x, HIGH);
// delay(50);
digitalWrite(x + 1, LOW);
}
}
// toggle odd digital pins
for ( int x = 3; x < 8; x++ ) {
if ( x % 2 == 1 ) {
digitalWrite(x, HIGH);
// delay(50);
digitalWrite(x - 1, LOW);
}
}
}
}
// read value of tilt switch
switchState = digitalRead(switchPin);
// reset variables to default, if switch state has changed
if ( switchState != prevSwitchState ) {
for( int x = 2; x < 8; x++ ) {
digitalWrite(x, LOW);
}
nextLED = 2;
prevTime = currentTime;
}
prevSwitchState = switchState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment