Notes for midterm project:

These notes are meant to help me explore the following steps for my instrument. There’s some more hardware I could use to improve it’s functionality. It was great to attend to Office Hours with Danny who talk to me through this topic/ideas and helped me visualize further options.

read this is what it sounds like

Week 4 Labs:

For the lab I already started coding and connecting my circuit. I am measuring the state of the button, I feel like this concept could have been really useful for a previous ICM assignment haha. But it’s great to be able to save data and states from hardware too! I feel it will be very useful.

IMG_5749.JPG

Captura de Pantalla 2025-09-25 a la(s) 14.33.51.png

This is the code I used for this program to work, following the lab.

int lastButtonState = LOW;

int buttonPresses = 0;

void setup() {
  Serial.begin(9600);
  // make pin 2 an input
pinMode(2,INPUT);

}

void loop() {
  // read the pushbutton
  int buttonState = digitalRead(2);

  //check if the current button state is different than the last state
  if(buttonState != lastButtonState){
    // do stuff if it is different here
    if(buttonState == HIGH){
      buttonPresses++;
      Serial.print("Button was just pressed ");
      Serial.print(buttonPresses);
      Serial.println(" times. ");
    }
  }
  //save button state for next comparison
  lastButtonState = buttonState;
}

Now I tried to measure three states for a button. This was the code.

//input pin
int buttonPin = 2;

// the lenght of the presses in millis
int longPress = 750;
int shortPress = 250;

// variable for how long the user actually presses
long pressTime = 0;

//previous state of the button
int lastButtonState = LOW;

void setup() {
  // initialize serial and i/o pin
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the button
  int buttonState = digitalRead(buttonPin);

  if( buttonState != lastButtonState){ // if the button has changed
    if(buttonState == HIGH){// if the button is pressed, start a timer
      pressTime = millis();
    }
    if(buttonState == LOW){ //if its released, stop the timer
      long holdTime = millis() - pressTime;
      // take action for longpress, short press or tap
      if(holdTime > longPress){
        Serial.println("long press");
      } else if(holdTime > shortPress){
        Serial.println("short press");
      } else {
        Serial.println("Tap");
      }
    }
  }
  lastButtonState = buttonState;
}

Now for the threshold cross by the sensor I did this circuit and replicated the code.

int lastSensorState = LOW; // sensor's previous state
int threshold = 512; // an arbitrary threshold value

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the sensor
  int sensorState = analogRead(A0);

  // if it's above the threshold:
  if(sensorState >= threshold){
    //check that the previous value was below the threshold
    if(lastSensorState < threshold){
      //the sensor just crossed the threshold
      Serial.println("Sensor crossed the threshold");
    }
  }
// save the button state for next comparison
lastSensorState = sensorState;
}

IMG_5752.JPG

And then for catching and learning peak values.