08 Digital hourglass code solution

Hey folks. I got the Arduino starter kit, and started to do the projects from the book. Now I am on 08 the Digital hourglass, and struggled a bit with the code. I made another reset (led=2;) into IF statement, where we decide what to do on led == 7.

I share my revised code, and will be happy on any feedback. Especially the "blink" part. One more question is it possible to run the program line by line?

Thanks!

const int switchPin = 8;

unsigned long previousTime=0;

int switchState = 0;
int prevSwitchState = 0;

int led=2;

long interval=1000;

 
void setup() {
  Serial.begin(9600);
 for(int x=2;x<8;x++){
  pinMode(x,OUTPUT); //Stablishing all the LEDs as outputs
 }
 pinMode(switchPin,INPUT); //Stablishing the switch as an input
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentTime = millis();

  if (currentTime - previousTime > interval) { 
    
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    Serial.print("Set ON led number ");
    Serial.println(led);
    led++;
    }
       
    if (led > 7)
      {  
        Serial.println("Set LEDs to blink"); 
        delay(interval); //We need that so that the last LED stay a bit
        led=2;
          for(int x=0;x<5;x++) { //count how many blinks
              for(int x=2;x<8;x++){ digitalWrite(x, LOW); }
              delay(100);
              Serial.println("blink 1");
              for(int x=2;x<8;x++){ digitalWrite(x, HIGH); }
              delay(100);
              Serial.println("blink 2"); 
              for(int x=2;x<8;x++){ digitalWrite(x, LOW); } //we need LEDs to remain in LOW state, so that we can turn them HIGH for next count
              Serial.println("blink 3");  
          }

     }
    
    switchState = digitalRead(switchPin);
    
    if(switchState != prevSwitchState) {
        for(int x=2;x<8;x++){
        digitalWrite (x, LOW);
      }
        led=2;
        previousTime=currentTime;
      }
      
    prevSwitchState=switchState;

}

In order to make it easily accessible to the people reading this thread, I'll share a link to the unmodified sketch "p08_DigitalHourglass" sketch:

https://github.com/arduino/arduino-examples/blob/main/examples/10.StarterKit_BasicKit/p08_DigitalHourglass/p08_DigitalHourglass.ino

You will also find it in the Arduino IDE at File > Examples > 10.StarterKit_BasicKit > p08_DigitalHourglass

The short answer is: "No".

You can add code to your sketch to get more feedback via Serial.print, and even to have the sketch wait for you to send commands from the Arduino IDE Serial Monitor to arrive.


The long answer is: "Yes, but it would be very difficult to set up for the Uno board in the Arduino Starter Kit"

You would need to install software on your computer and it is likely to be a painful installation process, especially for Windows users. It also requires a separate piece of hardware (debug probe) connected to your Uno, and even I believe making a modification to the Uno board itself.

You can read some information about an attempt to use Arduino IDE 2.x debugger with the Arduino Uno in the discussion here:

It is a lot easier to do with some of the other Arduino boards. The beta stage Arduino IDE 2.0 has the debugging software integrated into the IDE installation. It currently can only be used with the SAMD architecture boards such as the Arduino Zero, Nano 33 IoT, MKR Boards. All except the Zero (which has one built in) still require you to have a debug probe connected between the board and your computer.

Thank you In0!

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