Week 4
In Class
During the class, we learnt a lot about circuits with speakers. We saw a circuit diagram with a speaker and a transistor, which essentially represents a circuit where current is amplified.
Then some more of my doubts were cleared in the class, which I noted down there in the class itself. In class, I gained a clearer understanding of the definition of Hertz, as I was able to see it more concretely through the circuit, mainly because we had a better grasp of the concept of delay. So if the delay is lower than the frequency of the sound, it directly correlates to the definition of Hertz in general.
Moreover, it was interesting to understand C as a language because, in comparison to Python, where you don't have to declare the size of a multidimensional array, in C, you have to declare the multidimensional array explicitly.
I also learned to see more clearly how capacitors work and the concept of decoupling.
I also gained a clearer understanding of how voltage regulators work.
Assignments
This lab was easy to follow since most of the concepts were already familiar to me.
While programming the Microcontroller to read the pushbutton’s state change, I encountered some syntax errors in the code. Because I'm so used to Python, I sometimes don't pay as much attention to '}' in C++, so I have to make it more of a routine for me.
I was wondering what the difference was between 'Serial.println()' and 'Serial.print()', but then I figured out that 'Serial.println()' prints the output on a new line.
int lastSensorState = LOW; // sensor's previous state
// other globals and the setup go here
int buttonPresses = 0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// make pin 2 an input:
pinMode(2, INPUT);
}
void loop() {
// read the sensor:
int sensorState = digitalRead(2);
// if it's changed:
if (sensorState != lastSensorState) {
// take action or run a more detailed check
if (sensorState == HIGH) {
Serial.println("Button was just pressed.");
}
}
// save sensor state for next comparison:
lastSensorState = sensorState;
}
While documenting, I realised I hadn't counted the number of button presses in my code. I had just printed whenever the button was pressed using Serial.print("Button has been pressed").
I personally didn't like the way the long press and short press were reading the serial numbers because it felt too fast. I wasn't sure how to change that in code, so it doesn't keep printing out 'Tap'.
// the input pin:
int buttonPin = 2;
// the length of the presses in ms:
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 the button has changed:
if (buttonState != lastButtonState) {
// if the button is pressed, start a timer:
if (buttonState == HIGH) {
pressTime = millis();
}
// if it's released, stop the timer:
if (buttonState == LOW) {
long holdTime = millis() - pressTime;
// take action for long press, short press, or tap:
if (holdTime > longPress) {
Serial.println("long press");
} else if (holdTime > shortPress) {
Serial.println("short press");
} else {
Serial.println("Tap");
}
}
}
// save button state for next time:
lastButtonState = buttonState;
}
The 'Read a Sensor Threshold Crossing' lab gave me ideas for what I want to do for my midterm, especially in the way that I want to control my variables during the midterm.