dimecres, 15 de novembre del 2017

BLOC 2. CONCEPTES. BEEP

BLOC 2. CONCEPTES. BEEP

Exemple 2.4.
/*
* _2.4_beep
* 
* The piezo makes click with a 2 second interval.
*
* (c) 2013-2016 Arduino LLC.
*/

int speakerPin = 8;

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  digitalWrite(speakerPin, HIGH);
  delay(1000);
  digitalWrite(speakerPin, LOW);
  delay(1000);
}
Exemple 2.5.
/*
* _2.5_beep
* 
* The piezo makes click with a 1 millisecond interval.
*
* (c) 2013-2016 Arduino LLC.
*/
int speakerPin = 8;

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  digitalWrite(speakerPin, HIGH);
  delay(1);
  digitalWrite(speakerPin, LOW);
  delay(1);
}
Exemple 2.6. 

/*
* _2.6_beep
* 
* The piezo uses the tone() function to generate an A with a frequency of 440 hertz.
*
* (c) 2013-2016 Arduino LLC.
*/
void setup() {
  tone(8, 440, 1000);
}

void loop() {

}

MELODY
/*  Melody
*
* For playing sound with a piezo.
*
* Connect the one pin of the piezo to a digital pin 
*       (8 in this example) and the other pin to ground.
*
*       (c) 2013-2016 Arduino LLC.
*/

#include <EducationShield.h>

//pitches.h includes the definition of notes. Put it in your sketch
//folder if you want to play music.
#include "pitches.h"

//Declaring the component. 8 is the pin the piezo is connected to.
Melody me=Melody(8);

void setup(){
}

void loop(){
  //Defining the notes used in the music. No more than 30.
  int notes[] = {
    NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

  //Duration of each note. should be corresponding to the notes above.
  int noteDurations[] = {
    4, 8, 8, 4,4,4,4,4 };

  //play(length, notes, duration, speed)
  //  length: number of notes in your music.
  //  notes: the arry of notes.
  //  noteDuration: the array of note duration
  //  speed: how fast the music plays. No less than 1.0, the bigger the slower
  //
  //Play the notes defined above
  me.play(8,notes,noteDurations,1.4);

  delay(3000);

  //beep(length)
  //  length: how long the beep goes. Default to 30.
  //
  //Make a beep sound
  me.beep();

  delay(1000);
  //effect_win()
  //
  //Make a win sound effect
  me.effect_win();

  delay(1000);
  //effect_gameover()
  //
  //Make a gameover sound effect
  me.effect_gameover();

  delay(1000);
  //effect_score()
  //
  //Make a scoring sound effect
  me.effect_score();
  

  delay(3000);
}

BLOC 2. CONCEPTES. BLINK

BLOC 2. CONCEPTES. BLINK

Exemple 2.1.
/*
* _2.1_blink
* 
* The on-board LED blinks with a 2 second interval, 
* being turned on for 1 second and then turned off for 1 second.
*       
* (c) 2013-2016 Arduino LLC.
*/

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}


Exemple 2.2.
/*
* _2.2_blink
* 
* An external LED blinks with a 2 second interval, 
* being turned on for 1 second and then turned off for 1 second.
*       
* (c) 2013-2016 Arduino LLC.
*/

void setup() {
  pinMode(5, OUTPUT);
}

void loop() {
  digitalWrite(5, HIGH);
  delay(1000);
  digitalWrite(5, LOW);
  delay(1000);
}

Exemple 2.3.
/*
* _2.3_blink
* 
* An external LED blinks with a 2 second interval, 
* being turned on for 1 second and then turned off for 1 second.
*       
* The variable ledPin is declared to hold the value 5, the number 
* of the digital pin you are using.
*
* (c) 2013-2016 Arduino LLC.
*/

int ledPin = 5;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

dilluns, 13 de novembre del 2017

Bloc 2. Projectes. Carreres

/*
* Racing
* 
*  Run with your fingers as fast as you can.
* 
* In this game, the player must tap two sensors repeatedly 
* in order to run laps. One LED will light up per lap. 
* When all laps are completed, LEDs will blink in victory. 
*
* (c) 2013-2016 Arduino LLC.
*/

#include <EducationShield.h>

/*
  An array of pin numbers to which LEDs are attached
  the defaults are 2 to 6 but you can choose any of the digital pins
  just remember to leave digital pin 2,3 and 4 for the capacitive switches
*/
int ledPins[] = {8, 9, 10, 11, 13};
int pinCount = 5;
//This variable will let us keep track on which LED to turn on
int LED = 0;
VUMeter scoreBoard;

//Configure the capacitive sensors
int capacitiveSensorThreshold=400;
CapacitiveSwitch leftFoot=CapacitiveSwitch(2,3);
CapacitiveSwitch rightFoot=CapacitiveSwitch(2,4);

int score;

void setup(){
  //initializing the game, set up all the components and variables
  score=0;

  //Connect scoreboard
  scoreBoard.config(pinCount,ledPins);
  scoreBoard.begin();

  //initialize left and right "foot"
  leftFoot.config(capacitiveSensorThreshold);
  rightFoot.config(capacitiveSensorThreshold);
}

void loop(){
  //Wait for the left foot to be pressed
   leftFoot.pressed();
   scoreBoard.on(LED);
  
  //Wait for the right foot to be pressed
  rightFoot.pressed();
  scoreBoard.off(LED);
  
  score=score+1; //After both feet are pressed, add one point
  
  //Every 20 points light up a led
  LED =score/20;
  scoreBoard.fill(LED);
  
 //When you get 100 points, you win
  if(score>100){
    //if you win, blink all leds for celebration
    //See vuMeter in refence list to make your own blink animation
    scoreBoard.blinkAll(50,5);
    //and reset the game
    score=0;
  }
}

Bloc 2. Projectes Pong

/*
* Pong
* 
* Play a simplified version of the classic arcade game, Pong!
* 
* In this game, a “pong” will move across five LEDs (VU-meter) 
* and bounce back and forth as players press the button. 
* Players must press the button at the right time in order to 
* return the pong. 
*
* (c) 2013-2016 Arduino LLC.
*/

#include <EducationShield.h>
/*
  An array of pin numbers to which LEDs are attached
  the defaults are 2 to 6 but you can choose any of the digital 
  pins. Just remember to leave digital pin 9 and 6 for the buttons.
*/
int ledPins[] = {2, 3, 4, 5, 7};
int pinCount = 5;
VUMeter vuMeter;

Button button1 = Button(9); //the button connected to digital pin 9
Button button2 = Button(6); //the button connected to digital pin 6

int ledTime = 100; //determines how fast the LEDs will switch
int pressTime = 200; //determines how long time a player has to press the button
int buttonNotPressed = 0; //this keep track on who missed to press the button

void setup(){
  //if your are using other pins than 2 to 6 you need to configure that here
  vuMeter.config(pinCount, ledPins);

  vuMeter.begin(); //does the same as pinMode, LEDs are outputs
  button1.begin(); //does the same as pinMode, buttons are inputs
  button2.begin(); //does the same as pinMode, buttons are inputs

  vuMeter.scrollLeft(ledTime, 1); //The game starts by scrolling the LEDs to the left
}

void loop(){
  /*
  if button1 is pressed within the press time, the game will continue
  by scrolling the LEDs to the right
  else if button1 is not pressed, the program will jump to gameOver()
  */
  if(button1.released(pressTime)){
    vuMeter.scrollRight(ledTime, 1);
  }
  else{
    buttonNotPressed = 1; //Keep track on where we are in the game
    gameOver();
  }

  /*
  if button2 is pressed within the press time, the game will continue
  by scrolling the LEDs to the left
  else if button2 is not pressed, the program will jump to gameOver()
  */
  if(button2.released(pressTime)){
    vuMeter.scrollLeft(ledTime, 1);
  }
  else{
    buttonNotPressed = 2; //Keep track on where we are in the game
    gameOver();
  }
}

/*
  When a player doesn't press the right button within the right
  time it is game over. Inside the function gameOver() you can 
  decide how the LEDs should blink. 
  Use vuMeter.blink(LED,delayTime,numberOfBlinks) to make one specific LED blink
  Use vuMeter.blinkAll(delayTime,numberOfBlinks) to make all LEDs blink
*/
void gameOver(){
  vuMeter.blinkAll(100,10);

  if(buttonNotPressed==1) vuMeter.scrollRight(ledTime, 1); //if button1 was not pressed, scroll LEDs to right to start over
  else if(buttonNotPressed==2) vuMeter.scrollLeft(ledTime, 1); //if button2 was not pressed, scroll LEDs to left to start over
}

Bloc 2. Projectes. Esgrima

/*
* Fencing
* 
*   Test your reaction time against an opponent!
* 
* In this game, two players will hold tilt switch swords. 
* When the green LED randomly lights up, the first person 
* to swing their sword wins. 
*
* (c) 2013-2016 Arduino LLC.
*/


#include <EducationShield.h>

//Position of the leds in VU-meter is represented
//by their names here. So we can use names to find LEDs later
#define YELLOW_LED_1 0
#define GREEN_LED 1
#define RED_LED 2
#define YELLOW_LED_2 3

//An array stores which pins the VU-meter is connected
int ledPins[]={2,3,4,5};
//How many pins are used by VU-meter
int pinCount=4;
VUMeter lights;

TiltSwitch player_1 = TiltSwitch(6);
TiltSwitch player_2 = TiltSwitch(9);

void setup(){
  lights.config(pinCount,ledPins);

  //Initializing components
  lights.begin();
  player_1.begin();
  player_2.begin();

  //We need this for generating random number later
  randomSeed(analogRead(0));
}
void loop(){
 lights.clear();

 //Red led means both of you should hold the tilt switch sword up right
 lights.on(RED_LED);

 //Wait for a random period of time, between 3 seconds
 //And 6 seconds. Get ready!
 delay(random(3000,6000));

 lights.off(RED_LED);
 //When the green led turns on, game starts
 lights.on(GREEN_LED);

 //Swing your swords as fast as you can, the faster one 
 //will be returned by getWinner()
 int winner=getWinner();

 //The yellow led by side of the winner will light up
 if(winner==1){
   lights.on(YELLOW_LED_1);
 }else{
   lights.on(YELLOW_LED_2);
 }
 delay(5000);
}

//The function below waits for either of the tilter
//switch to be swang. The first one to swing
//will be returned by its number
int getWinner(){
  do{
    if(player_1.pressed(1)){
      return 1;
    }else if(player_2.pressed(1)){
      return 2;
    }
  }while(true);
}

Bloc 2. Projectes. Dau digital

/*
* DigitalDie
* 
*   Use this digital die next time you play a board game. 
*
* You "throw" the die by shaking a tilt sensor. The LEDs 
* will show different numbers, waiting a longer and longer 
* time for each number,  until it finally stops. Don't be 
* too fast to cheer believing it stopped on your desired 
* number or you might get disappointed ...
*
* (c) 2013-2016 Arduino LLC.
*/
 
#include <EducationShield.h>

//declare the pins used for leds, 9 leds in total
int pinCount=9;
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 11, 13};

/*
declare the tilt switch, it's connected to tinkerkit 
port 9
*/
TiltSwitch ts=TiltSwitch(9);

/*
  Define the patterns of die values. Each pattern is
  an array of 9 integers, indicating the on/off state
  of each led.
  And because there're 6 possible patterns, we need a 
  2-dimensional array to define all the data. It's a 
  big array of 6 elements, each element is an array of
  9 integers.
*/
int die[6][9]={
  //1
  {
  0,0,0,
  0,1,0,
  0,0,0 
  },
  
  //2
  {
  1,0,0,
  0,0,0,
  0,0,1 
  },
  
  //3
  {
  1,0,0,
  0,1,0,
  0,0,1 
  },
  
  //4
  {
  1,0,1,
  0,0,0,
  1,0,1 
  },
  
  //5
  {
  1,0,1,
  0,1,0,
  1,0,1 
  },
  
  //6
  {
  1,1,1,
  0,0,0,
  1,1,1 
  }
};

/*
  wait time between the die rolls to a different face.
  Notice it's using float type here? Read on!
*/
float waitTime=1;

void setup(){
  //Configure each pin as an output. 
  for(int i=0;i<pinCount;i++){
    pinMode(ledPins[i],OUTPUT);
  }
  
  //initialize the tilt switch.
  ts.begin();
  
  //generate the random seed. We use the value from
  //A0, since it's not connected to anything, it should
  //generate some random noises. Perfect for our purpose
  randomSeed(analogRead(A0));
}
void loop(){
  //Reset the wait time
  waitTime=2;
  
  /*
    Imagine when you throw a die, it'll bounce around, 
    showing a few values before laying still. 
  
    Let's keep generating new values until it's stable
    (when time between new values become long enough)
  */
  while(waitTime<1000){
    /*
      Generate a random die value.
      The die value can be 1 to 6, in the array it's
      die[0] to die[5]. random(0,6) generate a value
      between 0 to 6, that would be 0 to 5 in effect.
    */
    int value;
    value=random(0,6);
    
    //Display the die value
    displayDie(value);
    
    /*
      See why waitTime have to be float? If it's an integer,
      multiply it by 1.3 will make it lose everything behind
      the decimal mark. We use 2 as the starting value, 
      2*1.3 should be 2.6, losing the fractional parts means
      it'll be 2 in the end, so 2*1.3=2! It'll 
    */
    waitTime=waitTime*1.3;    
    delay(waitTime);
  }
  
  /*
    Now the die is stable, wait untill the tilt switch is 
    activated again. ts.pressed() stops the whole program 
    until it's activated.
  */
  ts.pressed();
}

void displayDie(int num){
  //Show the die value by turning on/off the right leds
  for(int i=0;i<pinCount;i++){
    digitalWrite(ledPins[i],die[num][i]);
  }
}

Bloc 2. Projectes. Basket

/*
* Basketball
* 
* Score a goal!
* 
* In this game, players will try to bounce a ping pong ball 
* into a cup. Make five points to win. The score is tracked 
* using a LightSensor.
*
* (c) 2013-2016 Arduino LLC.
*/

#include <EducationShield.h>
#include "pitches.h"
/*
An array of pin numbers to which LEDs are attached
the defaults are 2 to 6 but you can choose any of the digital pins
*/
int ledPins[] = {2, 3, 4, 5, 6};
int pinCount = 5;
VUMeter vuMeter;

Melody piezo = Melody(8); // the piezo connected to digital pin 8
LightSensor sensor = LightSensor(A1); //the LightSensor connected to analog pin 1

int score = 0;

void setup(){
  //if your are using other pins than 2 to 6 you need to configure that here
  vuMeter.config(pinCount, ledPins);
  vuMeter.begin(); //does the same as pinMode, LEDs are outputs

  sensor.config(800, 600); //first run LightSensortest example to see what values you need to put here
}

void loop(){
  //if the LightSensor is covered the score increases with 1
  //and a sounds is played
    sensor.pressed();
    score++;
    vuMeter.fill(score); //Turn on as many LEDs as the score

    int melody[] = { NOTE_GS4, NOTE_C5};
    int noteDurations[] = { 8, 8};
    int numberOfNotes = 2;
    piezo.play(numberOfNotes, melody, noteDurations, 1);

    delay(50);

  if(score>=pinCount) startOver(); //If the score equals the amount of LEDs you start over
}


void startOver(){
  score=0; //reset the score

  int melody[] = { NOTE_C5, NOTE_G4,NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5};
  int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
  int numberOfNotes = 8;
  piezo.play(numberOfNotes, melody, noteDurations, 1);

  vuMeter.blinkAll(50,10);

}