#Blog 4 Recitation 2 ArduinošŸ§©

Recitation 2 Arduino

Circuit 1: Fade

The mistake we had is that we forgot to change the port into USB, so the first attempt to upload the code was unsuccessful. After we changed it, tried again, but the light still wasnā€™t on. We thought that maybe the LED was burnt, so we changed the LED and it worked.

 

The code from Arduino > File > Examples > 03. Analog > Fading:

/*
  Fading

  This example shows how to fade an LED using the analogWrite() function.

  The circuit:
  - LED attached from digital pin 9 to ground through 220 ohm resistor.

  created 1 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fading
*/

int ledPin = 9;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

Circuit 2: toneMelody

This one is rather easy, so we finished it very quickly.

 

Code from Arduino > File > Examples > 02.Digital > toneMelody:

/*
  Melody

  Plays a melody

  circuit:
  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
*/

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

Circuit 3: Speed Game

We sign in Tinkercad and followed the picture to build our circuit. I was confused whether the digital pins can be considered as a positive or a negative pole. Prof. Marcela told me that it should be considered as a positive pole because the digital pin can either release 5V or 0V. Because this circuit is a bit complicated, it took us about 13 minutes to finish the ā€œconstructionā€.

And then we checked it through by comparing it with the picture from right to left. Be copied code from Tinkercad and paste it in Arduino. Our first upload failed. It was because we didnā€™t delete all the things in the Arduino platform. We deleted it clearly and paste again. We opened the Serial Monitor and played.

 

 

Own drawing of the schematic for circuit 3:

Code from Tinkercad:

int buzzerPin = 8;
int buzzerPin2 = 7;
int button1 = 11;
int button2 = 10;
int button3 = 12;
int button4 = 13;
int led1 = 3;
int led2 = 2;
int led3 = 4;
int led4 = 5;

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(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buzzerPin2, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  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 && winner3 == false && winner4 == false) {
      winner2 = true;
      digitalWrite(led2, HIGH);
      Serial.println("PLAYER 02 WINS");
      playMelody();
    }
  }

    //this checks the times player 03 has pressed the button
  if (counter3 < goal && winner1 == false && winner2 == 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 && winner1 == false && winner2 == false && winner4 == false) {
      winner3 = true;
      digitalWrite(led3, HIGH);
      Serial.println("PLAYER 03 WINS");
      playMelody();
    }
  }

  //this checks the times player 04 has pressed the button

  if (counter4 < goal && winner1 == false && winner2 == false && winner3 == 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 && winner1 == false&& winner2 == false && winner3 == 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
  long 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);

}

Question 1: Propose another kind of creative button you could use in Circuit 3 to make the game more interactive. Read and use some material from the Introduction Chapter (pages xvii to xxix) of Physical Computing to explain why this button would make this game more interactive.

I want to make a button into a pinky ass shape rubber one. So the action of pressing the button turns into patting the ass as quick as you can. Author and game programmer Chris Crawford defines interaction as ā€œan iterative process of listening, thinking, and speaking between two or more actors.ā€ The shape of an ass will surely add on more fun to the ā€œthinkingā€ process and may trigger more rounds of interaction. Also, in the book it said that to change how the computer reacts to us, we have to change how it sees us. This proposal can change our often dull appearance to the computer (finger clicking the buttons) into a wickedly funny organ. In this way, it can make the game more interactive.

 

Question 2: Why did we use a 10K resistor with each push button? (Read the short explanation about pull-down resistors here)

Pull-down resistor can prevent short circuits when the switch is closed while still biasing the pin to 0V when the switch is open.

Feb 18, 2022, Jingqiao, Younian Liu

Leave a Reply

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