Recitation 3: Sensors โ€“ Andres Malaga

For this recitation, we were asked to work with a sensor of our choice. I worked with a vibration sensor and used the knock example. Because I used the vibration sensor, I was asked to use an output that was not my Arduinoโ€™s LED, I chose a speaker. I first built a circuit without the speaker to test the sensor, using the knock example. I then tweaked the knock example to have the buzzer play a certain tone after a certain threshold had been passed. When I was doing that, I thought about Earthquake alarm systems, which I think use this principle. For the code, I tweaked the knock example and added a conditional so that the buzzer buzzed after a certain threshold had been surpassed.

Code:

  1. const int knockSensor = A0; // the piezo is connected to analog pin 0

  2. const int threshold = 20; // threshold value to decide when the detected sound

  3. is a knock or not

  4. int sensorReading = 0; // variable to store the value read from the sensor pin

  5. void setup() {

  6. pinMode(8, OUTPUT);

  7. Serial.begin(9600); // use the serial port

  8. }

  9. void loop() {

  10. // read the sensor and store it in the variable sensorReading:

  11. sensorReading = analogRead(knockSensor);

  12. Serial.println(sensorReading);

  13. // if the sensor reading is greater than the threshold:

  14. if (sensorReading >= threshold) {

  15. // send the string “Knock!” back to the computer, followed by newline

  16. Serial.println(“Knock!”);

  17. delay(1000);

  18. tone(8, 440, 1000);

  19. delay(1000);

  20. // stop the tone playing:

  21. noTone(8);

  22. }

  23. delay(100); // delay to avoid overloading the serial port buffer

  24. }

Questions:

  1. I intended to assemble a vibration sensor that, once it detected a vibration above a specific threshold, would activate a speaker connected to it. If this were to be used for a purpose, it would be, if built on a larger and more precise scale, an earthquake alarm similar to those that are found in Chile and Mexico. The idea of these alarms is that they go off once they detect an earthquake above a magnitude deemed unsafe, allowing for a faster evacuation and less casualties, as they would be able to notify the population of the impending threat, giving them enough time to get somewhere they deem safe.
  2. I think code is often compared to a recipe because that is what it essentially is. When we code something, we tell the program that we want it to do A, B, C, in a certain way and following certain parameters, and when we run it, we see that the program does A, B, and C. Just like a recipe, the ingredients or procedures (parameters/functions) may often need to be changed or tweaked in order to achieve the product (program) we want after we bake (run) it.
  3. Computers have made us rely less on memory and more on browsing and looking up stuff when we need answers. For example, before computers, you could ask a doctor any question related to their specialty and they would be able to describe it perfectly and recite the answer like if they were reading it from a book, it was fresh in their memory. Nowadays, doctors have a simplified idea of the answer to the question, and if they, for some reason, need to get deeper into the problem, they would google it or look it up on a book.

Leave a Reply