LED Pulse 2.0 (Carlos)

Since we went over my code in class, I now knew how to fix it so that the lights would only pulse once per button press. So in order to not waste those 40 minutes that I spent looking at my code with the help of an IM major friend with no success, I decided to go back in and try out some new ideas on the same pulsing lights concept.

I already had a digital sensor in the button, so the first thing I did was to add an analogue sensor (the potentiometer). The potentiometer would be used to kind of “charge” the pulse. I made it so that this charge would be defined by how much you turned the knob from one moment to another, rather than the resistance value of the potentiometer. So the more you turned the knob in between button presses, the stronger the pulse would feel. I would nail down what “stronger” in the context of this pulse means a little later…

In order to give feedback on the knob, I added a speaker. The speaker would give a higher pitch tone the more the user “charged” the pulse (i.e. the more the knob was twisted). The speaker then shuts down after the pulse has been delivered, to indicate that holding the button does not do anything. Lastly, I made it so that the speaker would give a slightly higher pitch tone when the button is pressed, so that it feels like some sort of release.

In order to make it feel like the pulse became more powerful the more it was charged, I did several things. The first thing I did was make it so that the lights would light up with less delay in between each other the more the pulse was charged. In order to make this happen, I used the map function to make it so that high values in the “charge” were equal to low values in the delay function. So the higher the charge, the less the delay between lights turning on. Then, I made it so that the lights would stay on longer the more it was charged. This was simply done by adding a delay before switching off the lights, by making the delay time increase with the charge value, by using the map function. Finally, I made it so that the LED’s would turn brighter the higher the charge value was, by using analogue write.

After this analogue write, I suddenly needed to add a digital write! Oh no! So I decided to add like a “panic mode”! If the button is pressed for too long, there would then be a digital write! And then there would be an alarm sound from the speaker! Yay, assignment completed!!

 

            Problems

The big problem had been the LED continuing to pulse after holding the button because of that semicolon after the ELSE IF statement that we found in class. Once that was fixed, everything was good.

Before I was also using pin 0 as a digital input. This made it so that I would not be able to upload the program from my laptop without unplugging, and I did not realize that was the case until we brought it up in class.

Some problems with the final version:

            Too many cables!! I’m not sure how I can make the potentiometer have less cables surrounding it. At the moment it’s really hard to turn the knob because of all the cables.

            For some reason, if the potentiometer is put at maximum, the speaker will make a really high pitch sound when the button is pressed. I don’t know why. Maybe it’s because of some irregularities in the potentiometer.

            The first pulse ends up being based on the initial level of the potentiometer, rather than how much it’s been turned. This could be fixed by always starting the program with the potentiometer at one of the ends. But I think this could also be fixed by making previousCharge be the resistance of the potentiometer during setup. I will put this in the code and if it works, then we will find out in class!

 

 

CODE: 

int button; //reads whether the button is pressed or not
bool pressed; //detects whether the button has been released or not
float charge; //reads the value of the potentiometer
float previousCharge; //records the value of the potentiometer when the button is pressed
float boom = 0; //timer for alarm


void setup() {
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, INPUT);
  previousCharge = analogRead(A3);
}

void loop() {

  button = digitalRead(12);
  charge = analogRead(A3);
  //Serial.println(charge);

  //reads how much the knob has been turned
  float chargeDiff = charge - previousCharge;
  chargeDiff = abs(chargeDiff);

  //variable for the pitch of the speaker
  float noise = chargeDiff;


  //the "pulse" that happens when the button is pressed
  if (button == 1 && !pressed) {

    float wait = map(chargeDiff, 0, 1023, 300, 50); //waits more if the chargeDiff is lower
    float brightness = map(chargeDiff, 0, 1023, 25, 255);

    tone(11, noise + 30); //aural feedback

    analogWrite(10, brightness);
    delay(wait);
    analogWrite(9, brightness);
    delay(wait);
    analogWrite(5, brightness);
    delay(wait);
    analogWrite(6, brightness);

    wait = map(chargeDiff, 0, 1023, 100, 1500);
    delay(wait);
    wait = map(chargeDiff, 0, 1023, 300, 50);

    while (noise > 32) {
      tone(11, noise);
      noise -= noise / 50;
      delay(50 / noise);
    }

    //pulse turning off
    noTone(11);
    digitalWrite(6, LOW);
    delay(wait);
    digitalWrite(5, LOW);
    delay(wait);
    digitalWrite(9, LOW);
    delay(wait);
    digitalWrite(10, LOW);

    //recording the current value of the charge, so that the knob needs to be turned again
    previousCharge = charge;
    pressed = true;

    //keeps pulse from repeating
  } else if (button == 0 && pressed) {
    Serial.println("yes");
    pressed = false;
    ////keeps tone from repeating if button is held
    //  } else if (button == 1) {
    //    noTone(11);
    //ensures lights are off if the button is not pressed
  } else if (button == 0) {
    boom = 0; //resets the timer for alarm
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    tone(11, noise); //makes the tone that signals the user how much they've turned the knob
  }

  if (button == 1) {
    Serial.println(boom);
    boom++; //sets up a timer that will start the alarm sequence after some time
    if (boom > 2500) { //alarm sequence
      delay(1000);
      digitalWrite(10, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      tone(11, 1024);
      delay(1000);
      tone(11, 200);
      digitalWrite(10, LOW);
      digitalWrite(9, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
    }
  }

}

Leave a Reply

Your email address will not be published. Required fields are marked *