Arduino 08 Extension - Reversing the lights

Hi all!
I've been moving through the arduino projects slowly but surely, but I am now stuck on the last section of the 08 extension asking to reverse the lights upon a shake "Unlike an hourglass filled with sand, the lights go either up or down dependingon the orientation of the switch. Can you figure out how you can use the switchState variable to indicate what direction the lights should go?" I've not been able to get the arduino to do anything except turn all the lights on at the ends. Code is below. Any help is appreciated. Thank you!

unsigned long PreviousTime=0;//Using the unsigned long variable as expleined before
int InterruptorState=0;//Initialtating variables
int PreviousStateInterruptor=0;
int Led=2;//Variable used to count which LED will be the next on lighting
long TimeIntervalocadaLed=1000;//Every 1 second(s) a LED will light
int c=0;
void setup() {
 for(int x=2;x<8;x++){
  pinMode(x,OUTPUT); //Stablishing all the LEDs as outputs
 }
 pinMode(PinInterruptor,INPUT); //Stablishing the switch as an input
}

void loop() {
 unsigned long ActualTime=millis();//Knowing how long the program has been working
 if(ActualTime-PreviousTime>TimeIntervalocadaLed){ //Veryfing if has transcurred the required time to switch on another LED
  PreviousTime=ActualTime;
  digitalWrite(Led,HIGH);//Lighting a new LED
  Led++;//Preparing to light the next one
  if(Led==7){ //In case that there is just one LED switch off advise by blinking all LEDs when the time is about to finish(2.5 seconds before)
    delay(2500);
    c=0;
    while(c<5){
     for(int x=2;x<8;x++){
      digitalWrite(x,LOW);
     }
     delay(500);
     for(int x=2;x<8;x++){
      digitalWrite(x,HIGH);
     } 
     delay(500);
     c++;  
    }
  }
 }
 InterruptorState=digitalRead(PinInterruptor); //Read sensor state
  if(InterruptorState !=PreviousStateInterruptor){ //Puting all variables at zero if the state has changed
    for(int x=2;x<8;x++){
     digitalWrite(x,LOW);
    }
    Led=2;
    PreviousTime=ActualTime;
  }
  PreviousStateInterruptor=InterruptorState; //Stablishing the actual state as the previous one
}

Your code does not compile. You are missing some declarations, as well as have re-declarations.

Would you verify this: It looks like you want your code to light the LEDs one at a time, moving in one direction... with the question, "How do you change the direction with a "shake" (button press)?"

I added a pushbutton to your code. Here are the files to use on WOKWI.COM...

"sketch.ino" tab:

unsigned long PreviousTime = 0; //Using the unsigned long variable as expleined before
int InterruptorState = 0; //Initialtating variables
int PreviousStateInterruptor = 0;
int Led = 2; //Variable used to count which LED will be the next on lighting
long TimeIntervalocadaLed = 1000; //Every 1 second(s) a LED will light
int c = 0;
int PinInterruptor = 9;

void setup() {
  for (int x = 2; x < 8; x++) {
    pinMode(x, OUTPUT); //Stablishing all the LEDs as outputs
  }
  // pinMode(PinInterruptor, INPUT); //Stablishing the switch as an input
  pinMode(PinInterruptor, INPUT_PULLUP); // better button
}

void loop() {
  unsigned long ActualTime = millis(); //Knowing how long the program has been working
  if (ActualTime - PreviousTime > TimeIntervalocadaLed) { //Veryfing if has transcurred the required time to switch on another LED
    PreviousTime = ActualTime;
    digitalWrite(Led, HIGH); //Lighting a new LED
    Led++;//Preparing to light the next one
    if (Led == 7) { //In case that there is just one LED switch off advise by blinking all LEDs when the time is about to finish(2.5 seconds before)
      // delay(2500);
      c = 0;
      while (c < 5) {
        for (int x = 2; x < 8; x++) {
          digitalWrite(x, LOW);
        }
        // delay(500);
        for (int x = 2; x < 8; x++) {
          digitalWrite(x, HIGH);
        }
        // delay(500);
        c++;
      }
    }
  }

  InterruptorState = digitalRead(PinInterruptor); //Read sensor state
  delay(150);
  if (InterruptorState != PreviousStateInterruptor) { //Puting all variables at zero if the state has changed
    for (int x = 2; x < 8; x++) {
      digitalWrite(x, LOW);
    }
    Led = 2;
    PreviousTime = ActualTime;
  }
  PreviousStateInterruptor = InterruptorState; //Stablishing the actual state as the previous one
}

"diagram.json" tab:

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 33.6, "left": 47.5, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -80.4,
      "left": 71,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -80.4,
      "left": 51.8,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led5",
      "top": -80.4,
      "left": 147.8,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led6",
      "top": -80.4,
      "left": 128.6,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led7",
      "top": -80.4,
      "left": 109.4,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led8",
      "top": -80.4,
      "left": 90.2,
      "attrs": { "color": "red", "flip": "" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -4.8,
      "left": 85.85,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -4.8,
      "left": 95.45,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r5",
      "top": -4.8,
      "left": 105.05,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r6",
      "top": -4.8,
      "left": 114.65,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r7",
      "top": -4.8,
      "left": 124.25,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r8",
      "top": -4.8,
      "left": 133.85,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -22.6,
      "left": 28.8,
      "attrs": { "color": "green" }
    }
  ],
  "connections": [
    [ "led5:A", "r8:1", "green", [ "v9.6", "h76.8" ] ],
    [ "r7:1", "led6:A", "green", [ "h-76.8" ] ],
    [ "r6:1", "led7:A", "green", [ "h0" ] ],
    [ "r5:1", "led8:A", "green", [ "h-105.6" ] ],
    [ "r4:1", "led1:A", "green", [ "h0" ] ],
    [ "r3:1", "led2:A", "green", [ "h0" ] ],
    [ "r8:2", "nano:2", "green", [ "h0" ] ],
    [ "r7:2", "nano:3", "green", [ "h0" ] ],
    [ "nano:4", "r6:2", "green", [ "v0" ] ],
    [ "nano:5", "r5:2", "green", [ "v0" ] ],
    [ "r4:2", "nano:6", "green", [ "h0" ] ],
    [ "nano:7", "r3:2", "green", [ "v0" ] ],
    [ "nano:GND.2", "led5:C", "black", [ "v-9.6", "h-96.4" ] ],
    [ "led5:C", "led6:C", "green", [ "v0" ] ],
    [ "led6:C", "led7:C", "green", [ "v0" ] ],
    [ "led7:C", "led8:C", "green", [ "v0" ] ],
    [ "led8:C", "led1:C", "green", [ "v0" ] ],
    [ "led1:C", "led2:C", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn1:1.r", "black", [ "v-9.6", "h-67.2", "v-38.4" ] ],
    [ "btn1:2.r", "nano:9", "green", [ "h0" ] ]
  ],
  "dependencies": {}
}

Why not post a link to your work? Copy pasting two files into the wokwi is stuff we eat for breakfast but seems an unusual bar to make the OP get over.

Or me, for that matter; yes I am bone lazy…

a7

Sometimes a simulation has been repeated so often, I feel one more blinking #13 would clutter my space. Also, for the future forum readers to have files to paste if wokwi were to crash, or more probable; I change something in the code that breaks the simulation and I do not correct my mistake. For this topic specifically; I do not see this OP returning.

I feel you.

I have an ability to deal with clutter that is above average. There are ppl nearby who could testify to that. :expressionless:

I am actually working right now to determine which of the 592 projects I have might be deleted…

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.