In week 2 recitation we are tasked with building basic circuits just like week 1 recitation, but this time we included the use of Arduino code. Specifically an automated fading led, an music player, and a led racing game with the optional improvement for adding multiplayer.(Schematics below)
Process:
For this weeks recitation I haven’t ran into any problems, we built the circuits and ran the code, everything went smoothly. We are also extra careful during the process and made sure no connection was loose, unplug power source when editing circuit, etc. I was able to complete the optional circuit fairly quickly too, we left our LED racing circuit as it is, and added another groups circuit to our initial circuit. instead of the pins assigned from the original code, I attached the buttons to `5 and `6 and the LEDs to 12 and 13 on the Arduino, and then modified the code to match the pin slots as well as adding win conditions and variables for the newly added player3 and player4(Hand Drawn diagram and video of working circuit below).
Modified code:
//led race game modified for 4 players
int buzzerPin = 8;
int button1 = 11;
int button2 = 10;
int button3 = 5;
int button4 = 6;
int led1 = 3;
int led2 = 2;
int led3 = 13;
int led4 = 12;
int goal = 10;
int buttonState1 = LOW;
int previousState1 = LOW;
int buttonState2 = LOW;
int previousState2 = LOW;
int buttonState3 = LOW;
int previousState3 = LOW;
int buttonState4 = LOW;
int previousState4 = LOW;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
boolean winner1 = false;
boolean winner2 = false;
boolean winner3 = false;
boolean winner4 = false;
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
Serial.println("******************* RACE THE LED *******************");
delay(1000);
Serial.println("READY");
delay(1000);
Serial.println("SET");
delay(1500);
Serial.println("GO!!!!!!!!!!!!!!!!");
}
void loop() {
// put your main code here, to run repeatedly:
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);
//Serial.println(buttonState1);
//this checks the times player 01 has pressed the button
if (counter1 < goal && winner2 == false && winner3 == false && winner4 == false) {
if (buttonState1 != previousState1 && millis() - time > debounce) {
if (buttonState1 == HIGH) {
counter1++;
Serial.print("player 01: ");
Serial.println(counter1);
time = millis();
}
}
previousState1 = buttonState1;
if (counter1 == goal && winner2 == false && winner3 == false && winner4 == false) {
winner1 = true;
digitalWrite(led1, HIGH);
Serial.println("PLAYER 01 WINS");
playMelody();
}
}
//this checks the times player 02 has pressed the button
if (counter2 < goal && winner1 == false && winner3 == false && winner4 == false) {
if (buttonState2 != previousState2 && millis() - time > debounce) {
if (buttonState2 == HIGH) {
counter2++;
Serial.print("player 02: ");
Serial.println(counter2);
time = millis();
}
}
previousState2 = buttonState2;
if (counter2 == goal && winner2 == false && winner1 == false && winner3 == false && winner4 == false) {
winner2 = true;
digitalWrite(led2, HIGH);
Serial.println("PLAYER 02 WINS");
playMelody();
}
}
//for player 3
if (counter3 < goal && winner1 == false && winner3 == false && winner4 == false) {
if (buttonState3 != previousState3 && millis() - time > debounce) {
if (buttonState3 == HIGH) {
counter3++;
Serial.print("player 03: ");
Serial.println(counter3);
time = millis();
}
}
previousState3 = buttonState3;
if (counter3 == goal && winner3 == false && winner1 == false && winner2 == false && winner4 == false) {
winner3 = true;
digitalWrite(led3, HIGH);
Serial.println("PLAYER 03 WINS");
playMelody();
}
}
//for player 4
if (counter4 < goal && winner1 == false && winner3 == false && winner4 == false) {
if (buttonState4 != previousState4 && millis() - time > debounce) {
if (buttonState4 == HIGH) {
counter4++;
Serial.print("player 04: ");
Serial.println(counter4);
time = millis();
}
}
previousState4 = buttonState4;
if (counter4 == goal && winner4 == false && winner1 == false && winner3 == false && winner2 == false) {
winner4 = true;
digitalWrite(led4, HIGH);
Serial.println("PLAYER 04 WINS");
playMelody();
}
}
}
void playFreq(double freqHz, int durationMs) {
//Calculate the period in microseconds
int periodMicro = int((1 / freqHz) * 1000000);
int halfPeriod = periodMicro / 2;
//store start time
int startTime = millis();
//(millis() - startTime) is elapsed play time
while ((millis() - startTime) < durationMs) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(halfPeriod);
}
}
void playMelody() {
playFreq(262, 100);
playFreq(294, 100);
playFreq(349, 100);
playFreq(294, 100);
playFreq(440, 200);
delay(100);
playFreq(440, 400);
playFreq(392, 400);
delay(300);
playFreq(262, 100);
playFreq(294, 100);
playFreq(349, 100);
playFreq(294, 100);
playFreq(392, 200);
delay(100);
playFreq(392, 400);
playFreq(349, 400);
}
Reflection:
I really liked this weeks recitation. Since I have previous experiences in coding, I was able to complete the tasks very smoothly. Learning form last weeks mistakes, I am now always extra careful during the construction to make sure that the connections are all secured, cause as the circuits that I am making gets more and more complex, it gets harder to keep track of every single thing, and as long as one part of the connection is not done properly, the whole thing stops working. I feel like building a circuit is very similar to coding, one part goes wrong and you have to spend a long time trying to find where the error is.
Documentation Questions:
Q1:Reflect how you use technology in your daily life and on the circuits you just built. Use the text Physical Computing and your own observations to define interaction.
I believe that technology plays a great role in my daily life, either for communication or transportation or even for entertainment. I guess for this weeks LED racing game, it would be for the entertainment for users. Just like how I would play video games on my computer. As for defining interaction, I agree with Chris Crawford on how the computers have similar phases of interaction just like humans: listening, thinking, and speaking. But unlike humans, computers use sensors to listen, CPU to think, and some kind of output to “speak” back to us.
Q2:If you have 100,000 LEDs of any brightness and color at your disposal, what would you make and where would you put it?
If I have 100,000 LEDs, I will probably create a huge art project with it. to build a huge screen of lights and display images through these LEDs.