├── .gitignore ├── CapacitiveSensor ├── README.md ├── .gitignore ├── keywords.txt ├── CapacitiveSensor.h ├── examples │ └── CapacitiveSensorSketch │ │ └── CapacitiveSensorSketch.pde └── CapacitiveSensor.cpp ├── examples ├── help │ ├── Player │ │ ├── Sound files │ │ │ └── 0.wav │ │ └── Player.ino │ ├── TiltSwitch │ │ └── TiltSwitch.ino │ ├── CapacitiveSwitchTest │ │ └── CapacitiveSwitchTest.ino │ ├── StandardServo │ │ └── StandardServo.ino │ ├── IRArray │ │ └── IRArray.ino │ ├── ButtonGroup │ │ └── ButtonGroup.ino │ ├── Wheels │ │ └── Wheels.ino │ ├── ContinuousServo │ │ └── ContinuousServo.ino │ ├── UltrasonicSensor │ │ └── UltrasonicSensor.ino │ ├── Joystick │ │ └── Joystick.ino │ ├── LDRTest │ │ └── LDRTest.ino │ ├── PiezoKnockSensor │ │ └── piezoKnockSensor.ino │ ├── Melody │ │ ├── Melody.ino │ │ └── pitches.h │ ├── Button │ │ └── Button.ino │ ├── LDR │ │ └── LDR.ino │ ├── CapacitiveSwitch │ │ └── CapacitiveSwitch.ino │ └── vuMeter │ │ └── vuMeter.ino ├── Magic │ ├── BoomBox │ │ ├── Sound files │ │ │ ├── 0.wav │ │ │ ├── 1.wav │ │ │ └── 2.wav │ │ └── BoomBox.ino │ ├── Sequencer │ │ ├── Sound files │ │ │ ├── seq0.wav │ │ │ ├── seq1.wav │ │ │ ├── seq2.wav │ │ │ ├── seq3.wav │ │ │ └── seq4.wav │ │ └── Sequencer.ino │ ├── BinaryLP │ │ ├── Processing sketch │ │ │ └── binary_LP_disc │ │ │ │ ├── record.pdf │ │ │ │ └── binary_LP_disc.pde │ │ ├── BinaryLP.ino │ │ └── pitches.h │ ├── drawdio │ │ ├── drawdio.ino │ │ └── pitches.h │ ├── CookieMonster │ │ ├── CookieMonster.ino │ │ └── Processing │ │ │ └── CookieMonster │ │ │ └── CookieMonster.pde │ ├── POV │ │ └── POV.ino │ └── KnockKnockBox │ │ └── KnockKnockBox.ino ├── Robots │ ├── LineFollower │ │ └── LineFollower.ino │ ├── CrawlingRobot │ │ └── CrawlingRobot.ino │ ├── TickleRobot │ │ └── TickleRobot.ino │ ├── LightChaser │ │ └── LightChaser.ino │ ├── OpenBox │ │ └── OpenBox.ino │ └── CameraRobot │ │ ├── Processing │ │ └── CameraRobot │ │ │ └── CameraRobot.pde │ │ └── CameraRobot.ino └── Sports │ ├── basketball │ ├── basketball.ino │ └── pitches.h │ ├── racing │ └── racing.ino │ ├── react │ ├── react.ino │ └── pitches.h │ ├── fencing │ └── fencing.ino │ ├── pong │ └── pong.ino │ ├── SimonSays │ └── SimonSays.ino │ └── DigitalDie │ └── DigitalDie.ino ├── TiltSwitch.cpp ├── Joystick.cpp ├── Knob.cpp ├── LED.cpp ├── CapacitiveSwitch.cpp ├── LDR.cpp ├── UltrasonicSensor.cpp ├── piezoKnockSensor.cpp ├── README.md ├── ButtonGroup.cpp ├── Button.cpp ├── keywords.txt ├── Wheels.cpp ├── IRArray.cpp ├── pitches.h ├── Melody.cpp ├── Player.cpp ├── VUMeter.cpp └── BasicEducationShield.h /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /CapacitiveSensor/README.md: -------------------------------------------------------------------------------- 1 | CapacitiveSensor 2 | ================ 3 | 4 | CapacitiveSensor -------------------------------------------------------------------------------- /examples/help/Player/Sound files/0.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/help/Player/Sound files/0.wav -------------------------------------------------------------------------------- /examples/Magic/BoomBox/Sound files/0.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/BoomBox/Sound files/0.wav -------------------------------------------------------------------------------- /examples/Magic/BoomBox/Sound files/1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/BoomBox/Sound files/1.wav -------------------------------------------------------------------------------- /examples/Magic/BoomBox/Sound files/2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/BoomBox/Sound files/2.wav -------------------------------------------------------------------------------- /TiltSwitch.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | TiltSwitch::TiltSwitch(int pin, bool pressedValue):Button(pin,pressedValue){ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sound files/seq0.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/Sequencer/Sound files/seq0.wav -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sound files/seq1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/Sequencer/Sound files/seq1.wav -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sound files/seq2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/Sequencer/Sound files/seq2.wav -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sound files/seq3.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/Sequencer/Sound files/seq3.wav -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sound files/seq4.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/Sequencer/Sound files/seq4.wav -------------------------------------------------------------------------------- /examples/Magic/BinaryLP/Processing sketch/binary_LP_disc/record.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lemio/BasicEducationShield/master/examples/Magic/BinaryLP/Processing sketch/binary_LP_disc/record.pdf -------------------------------------------------------------------------------- /CapacitiveSensor/.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | -------------------------------------------------------------------------------- /CapacitiveSensor/keywords.txt: -------------------------------------------------------------------------------- 1 | CapacitiveSensor KEYWORD1 2 | capacitiveSensorRaw KEYWORD2 3 | capacitiveSensor KEYWORD2 4 | set_CS_Timeout_Millis KEYWORD2 5 | reset_CS_AutoCal KEYWORD2 6 | set_CS_AutocaL_Millis KEYWORD2 7 | -------------------------------------------------------------------------------- /Joystick.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | Joystick::Joystick(int x, int y){ 4 | this->x=x; 5 | this->y=y; 6 | } 7 | 8 | int Joystick::getX(){ 9 | int v = analogRead(x); 10 | if(v<300) return(-1); 11 | else if(v>700) return(1); 12 | else return(0); 13 | 14 | } 15 | int Joystick::getY(){ 16 | int v = analogRead(y); 17 | if(v<300) return(-1); 18 | else if(v>700) return(1); 19 | else return(0); 20 | } 21 | -------------------------------------------------------------------------------- /Knob.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | Knob::Knob(int pin){ 9 | this->pin=pin; 10 | } 11 | 12 | int Knob::getValue(){ 13 | return analogRead(this->pin); 14 | } 15 | 16 | void Knob::setLevels(int levels){ 17 | this->levels=levels; 18 | } 19 | int Knob::getLevel(){ 20 | int val=map(this->getValue(),0,1023,0,this->levels); 21 | if(val==this->levels)val--; 22 | return val; 23 | } 24 | -------------------------------------------------------------------------------- /LED.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | LED::LED(int pin){ 4 | this->pin=pin; 5 | } 6 | void LED::begin(){ 7 | pinMode(pin,OUTPUT); 8 | } 9 | 10 | void LED::on(){ 11 | digitalWrite(pin,HIGH); 12 | } 13 | void LED::off(){ 14 | digitalWrite(pin,LOW); 15 | } 16 | void LED::blink(int speed,int times){ 17 | //If times is set to 0, the LED will blink forever 18 | for(int i=0;ithreashold=threashold; } void CapacitiveSwitch::test(){ Serial.println(getValue()); } long CapacitiveSwitch::getValue(int min){ int val=sensor.capacitiveSensor(30); if(valthreashold; } 2 | -------------------------------------------------------------------------------- /LDR.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | LDR::LDR(int pin):Button(pin,HIGH){ 4 | } 5 | 6 | void LDR::config(int baseValue, int threashold){ 7 | //Base value should be the one when nothing is covering 8 | //Threashold should be between base and covering value 9 | 10 | this->base=baseValue; 11 | this->threashold=threashold; 12 | } 13 | 14 | void LDR::test(){ 15 | //Use the test to determin base and threashold first 16 | Serial.println(analogRead(pin)); 17 | } 18 | 19 | bool LDR::getState(){ 20 | int value=analogRead(pin); 21 | return abs(value-base)>abs(threashold-base); 22 | } 23 | -------------------------------------------------------------------------------- /UltrasonicSensor.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | UltrasonicSensor::UltrasonicSensor(int trig, int echo){ 9 | this->trig=trig; 10 | this->echo=echo; 11 | } 12 | 13 | int UltrasonicSensor::getDistance(){ 14 | pinMode(trig, OUTPUT); 15 | digitalWrite(trig,LOW); 16 | delayMicroseconds(2); 17 | digitalWrite(trig, HIGH); 18 | delayMicroseconds(5); 19 | digitalWrite(trig, LOW); 20 | 21 | pinMode(echo, INPUT); 22 | int duration = pulseIn(echo, HIGH,11800); 23 | int distance = duration/29/2; 24 | 25 | return distance; 26 | } 27 | -------------------------------------------------------------------------------- /examples/Robots/LineFollower/LineFollower.ino: -------------------------------------------------------------------------------- 1 | /* 2 | LineFollower 3 | The Line Follower does exactly what the name suggests, 4 | it follows a line. Make it go where ever you want by 5 | showing the way with a 3 cm wide, black line. 6 | 7 | (c) 2014 Arduino LLC 8 | */ 9 | 10 | 11 | #include 12 | //Servo.h is necessary to be included here 13 | #include 14 | 15 | //IRArray(IR1, IR2, IR3) 16 | IRArray ir = IRArray(A1, A2, A3); 17 | //Wheels(left, right) 18 | Wheels wheels=Wheels(10, 9); 19 | 20 | void setup(){ 21 | wheels.begin(); 22 | delay(1000); 23 | } 24 | 25 | void loop(){ 26 | int dir = ir.readLine(); 27 | wheels.follow(dir); 28 | } 29 | -------------------------------------------------------------------------------- /piezoKnockSensor.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | #if ARDUINO >= 100 4 | #include "Arduino.h" 5 | #else 6 | #include "WProgram.h" 7 | #endif 8 | 9 | PiezoKnockSensor::PiezoKnockSensor(int pin){ 10 | this->pin=pin; 11 | } 12 | 13 | void PiezoKnockSensor::config(int threshold, int debounceTime){ 14 | this->threshold=threshold; 15 | this->debounceTime=debounceTime; 16 | } 17 | 18 | bool PiezoKnockSensor::knocked(int timeout){ 19 | long begin=millis(); 20 | do{ 21 | if(analogRead(pin)>threshold){ 22 | delay(debounceTime); 23 | return true; 24 | } 25 | }while(millis()-begin>timeout || !timeout); 26 | return false; 27 | } 28 | 29 | void PiezoKnockSensor::test(){ 30 | int val=analogRead(A5); 31 | Serial.println(val); 32 | delay(debounceTime); 33 | } 34 | -------------------------------------------------------------------------------- /examples/Robots/CrawlingRobot/CrawlingRobot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Crawling Robot 3 | 4 | This small robot can crawl. It’s all it can do, and it isn’t 5 | very good at it. But it is super cute and great fun. 6 | 7 | (c) 2013 Arduino LLC 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | Servo front, back; 14 | 15 | void setup(){ 16 | //servos are initialized 17 | front.attach(9); 18 | back.attach(10); 19 | } 20 | 21 | void loop(){ 22 | //Make the robot crawl by setting the servos to opposite angles. 23 | //90 is the middle angle 24 | 25 | //First step of each pair of legs 26 | front.write(110); 27 | delay(200); 28 | back.write(70); 29 | delay(200); 30 | 31 | //Second step of each pair of legs 32 | front.write(70); 33 | delay(200); 34 | back.write(110); 35 | delay(200); 36 | } 37 | -------------------------------------------------------------------------------- /examples/help/TiltSwitch/TiltSwitch.ino: -------------------------------------------------------------------------------- 1 | /* TiltSwitch 2 | * 3 | * Tilt Switch is a component that detects if it’s right side up 4 | * or tilted. 5 | * 6 | * (c) 2013 Arduino LLC 7 | */ 8 | 9 | #include 10 | 11 | //Declare the tilt switch. A TinkerKit tilt switch can be 12 | //connected to D10 or D9 (We use D10 here). 13 | TiltSwitch me=TiltSwitch(10); 14 | 15 | void setup(){ 16 | Serial.begin(9600); 17 | 18 | //Initialize the component. Must be called. 19 | me.begin(); 20 | } 21 | void loop(){ 22 | Serial.println("Please tilt..."); 23 | 24 | //pressed(timeout) 25 | // timeout: milliseconds within which the tilt switch should 26 | // be tilted. If tilted within time, the function 27 | // returns 1, otherwise 0. If not used, it waits 28 | // untill being pressed. 29 | // 30 | //Wait until the tilt switch is tilted. 31 | Serial.println(me.pressed()); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /examples/help/Player/Player.ino: -------------------------------------------------------------------------------- 1 | /* Player 2 | You can play a sound from an SD card using the Player class. 3 | The sound file need to be 8bit, uncompressed WAV. Follow this 4 | tutorial to record and prepare your sound correctly: 5 | http://cuartielles.com/LLC/en/Reference/PrepareWavSound 6 | 7 | Or use one of the .wav files in the BoomBox example. 8 | 9 | (c) 2014 Arduino LLC 10 | */ 11 | 12 | #include 13 | 14 | //We need to include the SD library to be able to read from an SD card 15 | #include 16 | 17 | 18 | Player player=Player(); 19 | 20 | void setup(){ 21 | //Initialize the sound player 22 | player.begin(); 23 | } 24 | 25 | void loop(){ 26 | //play(filename) 27 | // filename: filename of the sound stored in SD card 28 | // must be a .wav file 29 | // 30 | //Play the file named "0.wav" on SD card 31 | player.play("0.wav"); 32 | delay(1000); 33 | } 34 | -------------------------------------------------------------------------------- /examples/help/CapacitiveSwitchTest/CapacitiveSwitchTest.ino: -------------------------------------------------------------------------------- 1 | /* CapacitiveSwitch Test 2 | * 3 | * When making use of sensor readings from CapacitiveSwitch, 4 | * This sketch helps you decide the threshold by showing 5 | * capacitive values of different materials. 6 | * 7 | * Just touch the objects you're going to experiment with, 8 | * take note of the biggest and smallest values. In your 9 | * project code, use these values to map to your desired 10 | * outcome. 11 | * 12 | * It can also be used for adjusting the threshold when using 13 | * capacitive sensor as a button. 14 | * 15 | * (c) 2013 Arduino LLC 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | //The sensor is connected between 2 and 3 by default 22 | CapacitiveSwitch me=CapacitiveSwitch(2,3); 23 | 24 | void setup(){ 25 | Serial.begin(9600); 26 | } 27 | void loop(){ 28 | //Print out the value of the sensor 29 | me.test(); 30 | delay(30); 31 | } 32 | -------------------------------------------------------------------------------- /examples/help/StandardServo/StandardServo.ino: -------------------------------------------------------------------------------- 1 | /* 2 | StandardServo 3 | 4 | 180º standard servos are a type of motor which have a limited 5 | rotation angle. It has precise control over the angle of rotation. 6 | 7 | The ones provided for Basic Education Shield projects are 8 | microservos. Microservos behave the same as a standard servo, 9 | but are slimmer in size and exert less turning power. 10 | 11 | (c) 2013 Arduino LLC 12 | */ 13 | #include 14 | 15 | //It's necessary to include Servo.h if servo is used 16 | #include 17 | 18 | Servo me; 19 | 20 | void setup(){ 21 | //Servo is initialized, 22 | //in this example connected to digital pin 9 23 | me.attach(9); 24 | } 25 | 26 | void loop(){ 27 | //write(angle): 28 | // angle: the angle to which you want to rotate 29 | // the servo. It is ranged between 0 and 180. 30 | // 31 | //Make the servo rotate to 76 degrees. 32 | me.write(76); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /examples/help/IRArray/IRArray.ino: -------------------------------------------------------------------------------- 1 | /* 2 | IRArray 3 | This program will print each IR sensor reading to the 4 | serial monitor. 5 | 6 | When you hold the IRarray over a white surface all sensors 7 | should give you a value of 400. When held over a black surface 8 | it should go down to around 330. Make sure that you've 9 | connected all sensor pins right by alternate each sensor 10 | between black and white and see the that values of the 11 | corresponding IR sensor is changing. E.g. if you move IR1 from 12 | black to white but only the value of IR2 is changed you need 13 | to either change the analog pins in the code or change the 14 | connections to the analog pins on the Arduino. 15 | 16 | (c) 2014 Arduino LLC 17 | */ 18 | 19 | #include 20 | 21 | //IRArray(IR1, IR2, IR3) 22 | IRArray ir = IRArray(A1, A2, A3); 23 | 24 | void setup(){ 25 | Serial.begin(9600); 26 | } 27 | 28 | void loop(){ 29 | ir.test(); 30 | } 31 | -------------------------------------------------------------------------------- /examples/help/ButtonGroup/ButtonGroup.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ButtonGroup 3 | ButtonGroup is used for making a few buttons to work together. 4 | 5 | (c) 2013 Arduino LLC 6 | */ 7 | #include 8 | 9 | //Declaring the button group 10 | ButtonGroup bg; 11 | 12 | //Delcaring the pins to which the buttons are connected 13 | int buttons[]={4,5,6}; 14 | 15 | void setup(){ 16 | Serial.begin(9600); 17 | //initialize the button group. The first parameter is 18 | //number of buttons, the seconds is the array containing 19 | //pins used by these buttons. 20 | bg.begin(3,buttons); 21 | } 22 | void loop(){ 23 | Serial.println("Please press a button"); 24 | //pressed(timeout) 25 | // timeout:milliseconds within which one button should 26 | // be pressed. If pressed within time, the function 27 | // returns number of the button within inital list. 28 | // Otherwise -1. If not used, it waits untill being 29 | // pressed. 30 | // 31 | //Wait until one button is pressed. 32 | Serial.println(bg.pressed()); 33 | delay(10); 34 | } 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Basic Education Shield 2 | ======== 3 | 4 | Basic Education Shield library 5 | 6 | This is an educational project carried by Arduino LLC. It is a collection of exercises commissioned by the Center of Resources for Education at Castilla La Mancha to be tried out in the classrooms during the Spring 2013. 7 | 8 | The content of this repository is the materials generated for a first try out with schools and includes exercises, texts, and code. 9 | 10 | Team 11 | ==== 12 | 13 | The team creating this collection of exercises is made by: 14 | 15 | - X. Yang (IxD Manager) 16 | - C. Leivas (IxD Junior Designer) 17 | - T. Hansson (IxD Junior Designer) 18 | - L. Balboa (Art Direction) 19 | - K. Engdahl (3D Modelling Intern) 20 | - T. Pham (Typesetting and English Language Supervison) 21 | - A. Goransson (Programming Wizard) 22 | - D. Cuartielles (Coordination and Spanish Language Supervision) 23 | 24 | If you want to collaborate by adding translations to this collection of exercises, just send us a note to d.cuartielles AT arduino.cc 25 | 26 | IMPORTANT 27 | ========= 28 | 29 | Move the folder CapacitiveSensor and SoundWave to the libraries folder of your Arduino installation before using. Some features are dependent on them. 30 | -------------------------------------------------------------------------------- /examples/help/Wheels/Wheels.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Wheels 3 | 4 | If you ever want to use two continuous rotation servos as a pair 5 | of wheels we've made it easy for you. 6 | 7 | (c) 2013 Arduino LLC 8 | */ 9 | 10 | #include 11 | 12 | //Servo.h is necessary to be included here 13 | #include 14 | 15 | //Wheels(left, right) 16 | // Declares the wheels. 17 | // left: the digital pin where you connect the left wheel 18 | // right: the digital pin where you connect the right wheel 19 | Wheels wheels=Wheels(10, 9); 20 | 21 | void setup(){ 22 | //Initialize the servo wheels 23 | wheels.begin(); 24 | } 25 | 26 | void loop(){ 27 | //Makes the wheel spin forward 28 | wheels.goForward(); 29 | delay(2000); 30 | 31 | //Makes the wheels spin backwards 32 | wheels.goBackwards(); 33 | delay(2000); 34 | 35 | //Makes the wheels spin in opposite direction so that 36 | //the vehicle will spin left 37 | wheels.turnLeft(); 38 | delay(2000); 39 | 40 | //Makes the wheels spin in opposite direction so that 41 | //the vehicle will spin right 42 | wheels.turnRight(); 43 | delay(2000); 44 | 45 | //Makes the wheels stop 46 | wheels.standStill(); 47 | delay(2000); 48 | } 49 | -------------------------------------------------------------------------------- /ButtonGroup.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | #if ARDUINO >= 100 4 | #include "Arduino.h" 5 | #else 6 | #include "WProgram.h" 7 | #endif 8 | 9 | ButtonGroup::ButtonGroup(){ 10 | for(int i=0;ibuttons[i]=-1; 12 | } 13 | } 14 | 15 | void ButtonGroup::begin(int length, int buttons[],bool pressedValue){ 16 | for(int i=0;ibuttons[i]=buttons[i]; 18 | //this->iStarted[i]=false; 19 | } 20 | this->buttonsCount=length; 21 | this->pressedValue=pressedValue; 22 | } 23 | 24 | int ButtonGroup::pressed(int timeout){ 25 | return this->checkPress(timeout, this->pressedValue); 26 | } 27 | 28 | int ButtonGroup::checkPress(int timeout, bool requiredValue){ 29 | bool iStarted[this->buttonsCount]; 30 | for(int i=0;ibuttonsCount;i++){ 31 | iStarted[i]=false; 32 | } 33 | 34 | long timer=millis(); 35 | while(!timeout || millis()-timer<=timeout){ 36 | for(int i=0;ibuttonsCount;i++){ 37 | bool stat=digitalRead(this->buttons[i]); 38 | if(stat==!requiredValue && !iStarted[i]){ 39 | iStarted[i]=true; 40 | }else{ 41 | if(iStarted[i] && stat==requiredValue){ 42 | return i; 43 | 44 | } 45 | } 46 | } 47 | } 48 | return -1; 49 | } 50 | -------------------------------------------------------------------------------- /examples/help/ContinuousServo/ContinuousServo.ino: -------------------------------------------------------------------------------- 1 | /* ContinuousServo 2 | * 3 | * Continuous servo looks the same as a standard servo, but its 4 | * capable of rotating continuous like a normal motor. You can not 5 | * control which angle it´s pointing to, but you can specify how 6 | * fast it spins, and which direction it spins. 7 | * 8 | * To control two servos as wheels on a vehicle please see the 9 | * example Wheels in the help folder. 10 | * 11 | * (c) 2013 Arduino LLC 12 | */ 13 | 14 | #include 15 | 16 | //It's necessary to include Servo.h if servo is used 17 | #include 18 | Servo me; 19 | 20 | void setup(){ 21 | //Servo is initialized, 22 | //in this example connected to digital pin 9 23 | me.attach(9); 24 | } 25 | 26 | void loop(){ 27 | //write(speed): 28 | // speed: the speed in which you want to rotate 29 | // the servo. It is ranged between 0 and 180. 30 | // 180 is fastet speed in one direction and 0 31 | // fastest speed in the opposite direction. 32 | // 90 should make it stand still. This exact 33 | // value is slightly different for each servo. 34 | // 35 | //Make the servo rotate in speed 120. 36 | me.write(120); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /examples/Robots/TickleRobot/TickleRobot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Tickle Robot 3 | 4 | This is just a regular ticklish robot in a rocking chair. 5 | Come to think of it, that doesn't sound very regular at all. 6 | Tickle the robot on its heart and it will start wiggle like crazy. 7 | 8 | (c) 2013 Arduino LLC 9 | */ 10 | 11 | #include 12 | 13 | //Necessary to include Servo.h when using Servo 14 | #include 15 | 16 | //Necessary to include CapacitiveSensor.h when using capacitive sensor 17 | #include 18 | 19 | //Declare the servo for controlling the string robot 20 | Servo pull; 21 | 22 | //Declare the capacitive sensor 23 | CapacitiveSwitch sensor=CapacitiveSwitch(2,3); 24 | 25 | void setup(){ 26 | //initialize the capacitive sensor. Threshold is 400 27 | //See the example CapacitiveSwitchTest in the Help folder 28 | //to find the right thresshold 29 | sensor.config(400); 30 | 31 | //initialize the servo motor 32 | pull.attach(9); 33 | } 34 | void loop(){ 35 | if(sensor.getState()){ 36 | //If the capacitive sensor is touched, pull the strings 37 | pull.write(0); 38 | }else{ 39 | //Otherwise, loosen the strings 40 | pull.write(90); 41 | } 42 | delay(30); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /examples/Robots/LightChaser/LightChaser.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Light Chaser 3 | 4 | This little vehicle is on an impossible, never ending 5 | mission to catch the light. You can make it follw you 6 | by pointing a flashligt at it. (The one on your mobile 7 | phone eg.) It will always turn towards the light. 8 | 9 | (c) 2013 Arduino LLC 10 | */ 11 | 12 | #include 13 | 14 | //Servo.h is necessary to be included here 15 | #include 16 | 17 | //Declare the two wheels of robot, left wheel to D10 and 18 | // right wheel to D9 19 | Wheels wheels=Wheels(10, 9); 20 | 21 | //Declare the two LDR sensors 22 | LDR sensorLeft=LDR(A0); 23 | LDR sensorRight=LDR(A2); 24 | 25 | void setup(){ 26 | //initialize the LDR sensors 27 | sensorLeft.config(600,800); 28 | sensorRight.config(600,800); 29 | 30 | //initialize the servo motors 31 | wheels.begin(); 32 | } 33 | void loop(){ 34 | if(sensorLeft.getState()){ 35 | //Left ldr detects strong light, the vechile turns left 36 | wheels.turnLeft(); 37 | }else if(sensorRight.getState()){ 38 | //Right ldr detects strong light, the vechile turns right 39 | wheels.turnRight(); 40 | }else{ 41 | //No strong light detected, the vechile goes straight 42 | wheels.goForward(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /examples/help/UltrasonicSensor/UltrasonicSensor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Ultrasonic Sensor 3 | An ultrasonic sensor is used to detect the distance to the 4 | closest object in front of the sensor. It uses ultrasound, 5 | sound with a frequency higher than humans can hear. As you 6 | probably know sound can reflect from surfaces creating an 7 | echo. This echo is what the sensor uses to estimate the 8 | distance. The sensors sends out a burst of ultrasound and 9 | then waits for the echo. By knowing the speed of sound and 10 | the time between the burst and the detection we can 11 | calculate the distance. The shorter time it takes to detect 12 | the echo, the closer the object is. 13 | 14 | (c) 2014 Arduino LLC 15 | */ 16 | 17 | #include 18 | 19 | //Declare the ultrasonic sensor with the trigger pin connecter to 20 | // digital pin 11, and the echo pin to digital pin 12. 21 | UltrasonicSensor us = UltrasonicSensor(11, 12); 22 | 23 | void setup(){ 24 | //Initialize the serial communication 25 | Serial.begin(9600); 26 | } 27 | 28 | void loop(){ 29 | //Get the distance in cm 30 | int distance = us.getDistance(); 31 | 32 | //Print out the distance to the serial monitor 33 | Serial.println(distance); 34 | delay(100); 35 | } 36 | -------------------------------------------------------------------------------- /examples/help/Joystick/Joystick.ino: -------------------------------------------------------------------------------- 1 | /* Joystick 2 | * The joystick is one component but uses two analog sensors and 3 | * gives you two values. One x value and one y value. The value 4 | * for each direction is zero if it’s in the middle, -1 on one side 5 | * and 1 on the other side. 6 | * 7 | * The joystick returns the position it is being pushed to. 8 | * It returns two values, one X value and one Y value. That is the 9 | * position on the X axis (left to right) and the position on the 10 | * Y axis (top to bottom).Each value can be -1, 0 or 1 where 0 is 11 | * the middle position. E.g. x=-1 and y=1 means that the joysticks 12 | * position is left top, x=0 and y=-1 means that the position is 13 | * middle bottom. 14 | * 15 | * (c) 2013 Arduino LLC 16 | */ 17 | 18 | #include 19 | 20 | //The joystick uses two analog pins. One for X and one for Y. 21 | Joystick me=Joystick(A1,A0); 22 | 23 | void setup(){ 24 | //Start serial comunication to be able to print values to the serial monitor 25 | Serial.begin(9600); 26 | } 27 | void loop(){ 28 | Serial.print("X: "); 29 | Serial.print(me.getX()); //Get the X value and print it. 30 | Serial.print(" Y: "); 31 | Serial.println(me.getY()); //Get the Y value and print it. 32 | delay(200); 33 | } 34 | -------------------------------------------------------------------------------- /examples/help/LDRTest/LDRTest.ino: -------------------------------------------------------------------------------- 1 | /* LDR test 2 | * This sketch should be used prior to using the component in 3 | * Projects. Projects suitable for this library should have 2 4 | * states: uncovered and covered. That is to say, you want to 5 | * track objects covering/moving pass the sensor. 6 | * 7 | * HOW-TO: 8 | * Upload this sketch to Arduino, open the serial monitor. You 9 | * will see the current value of the ldr sensor. 10 | * 11 | * Make sure the sensor is in its desired setting and not 12 | * moving. When the value gets stable, take note of it(we call 13 | * it "baseValue"). 14 | * 15 | * Now cover the sensor with desired object. When the value 16 | * gets stable, take note of it(topValue). Remembe these values 17 | * and use them when configuring. 18 | * 19 | * "baseValue" and "threshold" will be used for defining the 20 | * LDR switch in your project. You can fine tune the threshold 21 | * to make the sensor more/less sensitive. The closer to 22 | * baseValue, the more sensitive it is. 23 | * 24 | * (c) 2013 Arduino LLC 25 | */ 26 | #include 27 | 28 | //Tinkerkit LDR is connected to analog 1. 29 | LDR sensor = LDR(A1); 30 | 31 | void setup(){ 32 | Serial.begin(9600); 33 | } 34 | 35 | void loop(){ 36 | //test() prints data to Serial port. 37 | sensor.test(); 38 | delay(100); 39 | } 40 | -------------------------------------------------------------------------------- /examples/help/PiezoKnockSensor/piezoKnockSensor.ino: -------------------------------------------------------------------------------- 1 | /* Piezo Knock Sensor 2 | * 3 | * Piezo can play music, as well as used as knock sensor. 4 | * 5 | * Connect the redone pin of the piezo to an analog pin 6 | * (A5 in this example), the other pin to GND. Connect a 7 | * 1m ohm resistor parallel with the piezo. 8 | * 9 | * (c) 2013 Arduino LLC 10 | */ 11 | 12 | #include 13 | 14 | //Remember: Piezo as speaker is connected to digital pin 15 | //while piezo as knock sensor is connected to analog pin. 16 | PiezoKnockSensor sensor=PiezoKnockSensor(A0); 17 | 18 | void setup(){ 19 | Serial.begin(9600); 20 | //define the threshold and debounce time of the knock 21 | //sensor. Threshold defines how hard you need to knock, 22 | //debounce time prevents the sensor from returning several 23 | //hits from one knock, but also limits how rapid you can knock. 24 | sensor.config(40,80); 25 | 26 | } 27 | void loop(){ 28 | Serial.println("Please knock..."); 29 | 30 | //knocked(timeout) 31 | // timeout: milliseconds within which the sensor should 32 | // be knocked. If knocked within time, the function 33 | // returns 1, otherwise 0. If not used, it waits 34 | // untill being knocked. 35 | // 36 | //Wait until the button is knocked. 37 | Serial.println(sensor.knocked()); 38 | } 39 | -------------------------------------------------------------------------------- /examples/Robots/OpenBox/OpenBox.ino: -------------------------------------------------------------------------------- 1 | /* 2 | OpenBox 3 | 4 | OpenBox is an open source box that contains open source 5 | electronics. Oh, and it automatically opens when you 6 | knock on it. 7 | 8 | (c) 2013 Arduino LLC 9 | */ 10 | 11 | #include 12 | 13 | //It's necessary to include Servo.h if servo is used 14 | #include 15 | 16 | //Declare the pezio knock sensor. Connected to A0 17 | PiezoKnockSensor sensor=PiezoKnockSensor(A0); 18 | 19 | //Declare the servo motor for opening the lid 20 | Servo lidOpener; 21 | 22 | void setup(){ 23 | //define the threshold and debounce time of the knock 24 | //sensor. Threshold defines how hard you need to knock, 25 | //debounce time prevents the sensor from detecting 26 | //false knocks, but also limits how rapid you can knock. 27 | //See the PiezoKnockSensor sketch in the help folder 28 | //to make sure your values are correct 29 | sensor.config(40,80); 30 | 31 | //initialize the servo 32 | lidOpener.attach(9); 33 | //Rotate servo to close lid 34 | lidOpener.write(60); 35 | } 36 | void loop(){ 37 | 38 | if(sensor.knocked()){ 39 | //rotate the servo motor to open the lid 40 | lidOpener.write(0); 41 | delay(3000); //Wait for 3 seconds 42 | //close the lid 43 | lidOpener.write(60); 44 | delay(200); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Button.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | Button::Button(int pin,bool pressedValue){ 9 | this->pin=pin; 10 | this->pressedValue=pressedValue; 11 | } 12 | void Button::begin(){ 13 | //Must be called in setup 14 | pinMode(pin,INPUT); 15 | } 16 | bool Button::pressed(int timeout){ 17 | //from unpressed to pressed 18 | return checkPress(timeout,pressedValue); 19 | } 20 | bool Button::released(int timeout){ 21 | //from pressed to unpressed 22 | return checkPress(timeout,!pressedValue); 23 | } 24 | bool Button::doublePressed(int timeout,int tolerance){ 25 | //two clicks within tolerance time 26 | if(pressed(timeout)){ 27 | return pressed(tolerance); 28 | }else{ 29 | return false; 30 | } 31 | } 32 | bool Button::checkPress(int timeout,bool requiredState){ 33 | //help function, check if the button has changed 34 | //from not "requiredState" to "requiredState" within timeout 35 | long timer=millis(); 36 | bool iStart=false; 37 | do{ 38 | if(!iStart){ 39 | if(getState()!=requiredState){ 40 | iStart=true; 41 | } 42 | }else{ 43 | if(getState()==requiredState){ 44 | return true; 45 | } 46 | } 47 | //delay(10); 48 | }while(millis()-timer<=timeout || !timeout ); 49 | 50 | return false; 51 | } 52 | 53 | bool Button::getState(){ 54 | return digitalRead(pin); 55 | } 56 | -------------------------------------------------------------------------------- /examples/help/Melody/Melody.ino: -------------------------------------------------------------------------------- 1 | /* Melody 2 | * 3 | * For playing sound with a piezo. 4 | * 5 | * Connect the one pin of the piezo to a digital pin 6 | * (8 in this example) and the other pin to ground. 7 | * 8 | * (c) 2013 Arduino LLC 9 | */ 10 | #include 11 | 12 | //pitches.h includes the definition of notes. Put it in your sketch 13 | //folder if you want to play music. 14 | #include "pitches.h" 15 | 16 | //Declaring the component. 8 is the pin the piezo is connected to. 17 | Melody me=Melody(8); 18 | 19 | void setup(){ 20 | } 21 | 22 | void loop(){ 23 | //Defining the notes used in the music. No more than 30. 24 | int notes[] = { 25 | NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; 26 | 27 | //Duration of each note. should be corresponding to the notes above. 28 | int noteDurations[] = { 29 | 4, 8, 8, 4,4,4,4,4 }; 30 | 31 | //play(length, notes, duration, speed) 32 | // length: number of notes in your music. 33 | // notes: the arry of notes. 34 | // noteDuration: the array of note duration 35 | // speed: how fast the music plays. No less than 1.0, the bigger the slower 36 | // 37 | //Play the notes defined above 38 | me.play(8,notes,noteDurations,1.4); 39 | 40 | delay(3000); 41 | 42 | //beep(length) 43 | // length: how long the beep goes. Default to 30. 44 | // 45 | //Make a beep sound 46 | me.beep(); 47 | 48 | delay(3000); 49 | } 50 | -------------------------------------------------------------------------------- /CapacitiveSensor/CapacitiveSensor.h: -------------------------------------------------------------------------------- 1 | /* 2 | CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring 3 | Copyright (c) 2008 Paul Bagder All rights reserved. 4 | Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix 5 | vim: set ts=4: 6 | */ 7 | 8 | // ensure this library description is only included once 9 | #ifndef CapacitiveSensor_h 10 | #define CapacitiveSensor_h 11 | 12 | #if ARDUINO >= 100 13 | #include "Arduino.h" 14 | #else 15 | #include "WProgram.h" 16 | #endif 17 | 18 | // library interface description 19 | class CapacitiveSensor 20 | { 21 | // user-accessible "public" interface 22 | public: 23 | // methods 24 | CapacitiveSensor(uint8_t sendPin, uint8_t receivePin); 25 | long capacitiveSensorRaw(uint8_t samples); 26 | long capacitiveSensor(uint8_t samples); 27 | void set_CS_Timeout_Millis(unsigned long timeout_millis); 28 | void reset_CS_AutoCal(); 29 | void set_CS_AutocaL_Millis(unsigned long autoCal_millis); 30 | // library-accessible "private" interface 31 | private: 32 | // variables 33 | int error; 34 | unsigned long leastTotal; 35 | unsigned int loopTimingFactor; 36 | unsigned long CS_Timeout_Millis; 37 | unsigned long CS_AutocaL_Millis; 38 | unsigned long lastCal; 39 | unsigned long total; 40 | uint8_t sBit; // send pin's ports and bitmask 41 | volatile uint8_t *sReg; 42 | volatile uint8_t *sOut; 43 | uint8_t rBit; // receive pin's ports and bitmask 44 | volatile uint8_t *rReg; 45 | volatile uint8_t *rIn; 46 | volatile uint8_t *rOut; 47 | // methods 48 | int SenseOneCycle(void); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /examples/help/Button/Button.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Button 3 | A button is a digital input. This means that it can have two 4 | states, either HIGH (5V) or LOW (0V). When you connect a button 5 | to your Arduino board you can read these states as 1 or 0. 6 | 7 | (c) 2013 Arduino LLC 8 | */ 9 | 10 | #include 11 | 12 | //Declare the button component. 13 | //9 is the pin number. If it's a tinkerkit button, use 9 or 10. 14 | //HIGH can be ignored mostly, which means when the button is 15 | //pressed, what state it is in. 16 | Button me=Button(9,HIGH); 17 | 18 | void setup(){ 19 | Serial.begin(9600); 20 | 21 | //Initialize the component. Must be called. 22 | me.begin(); 23 | } 24 | void loop(){ 25 | Serial.println("Please press..."); 26 | //pressed(timeout) 27 | // timeout: milliseconds within which the button should 28 | // be pressed. If pressed within time, the function 29 | // returns 1, otherwise 0. If not used, it waits 30 | // untill being pressed. 31 | // 32 | //Wait until the button is pressed. 33 | Serial.println(me.pressed()); 34 | 35 | 36 | Serial.println("Please release..."); 37 | //released(timeout) 38 | // timeout: same as in pressed(timeout). 39 | // 40 | //Wait untill the button is released. 41 | Serial.println(me.released()); 42 | 43 | 44 | Serial.println("Please double press..."); 45 | //doublepressed(timeout, tolerance) 46 | // timeout: milliseconds before the button should be 47 | // pressed for the first time. Same as others. 48 | // tolerance: milliseconds before it should be clicked 49 | // the second time. Defaults to 500. 50 | // 51 | //Wait untill the button is double clicked. If only clicked 52 | //once or action is too slow, it will return 0. 53 | Serial.println(me.doublePressed()); 54 | 55 | } 56 | -------------------------------------------------------------------------------- /examples/Magic/Sequencer/Sequencer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Sequencer 3 | Play beats and change the sequence in real time. Basically 4 | all you need to become a hiphop artist. That, and rapping 5 | skills. But that's another course. 6 | 7 | (c) 2014 Arduino LLC 8 | */ 9 | 10 | 11 | #include "BasicEducationShield.h" 12 | 13 | //We need to include the SD library to be able to read from an SD card 14 | #include 15 | 16 | //Declare the sound player 17 | Player player; 18 | 19 | //There're 4 slots for 4 sequences. We use 4 analog pins 20 | //to read them. 21 | int analog_pins[]={4,3,2,1}; 22 | int pin_count=4; 23 | 24 | //Template for the music file names. 25 | char filename[]="seq0.wav"; 26 | 27 | void setup(){ 28 | // Open serial communications and wait for port to open: 29 | Serial.begin(9600); 30 | 31 | // Initialize the sound player. Open the serial monitor to see 32 | //the sound files found on your micro SD card 33 | player.begin(); 34 | 35 | } 36 | void loop(){ 37 | //Loop through 4 pins and play the right sequence accordingly 38 | for(int i=0;i200 && analogVal<300){ //220 Ohm 54 | return 1; 55 | }else if(analogVal>360 && analogVal<460){ //470 Ohm 56 | return 2; 57 | }else if(analogVal>480 && analogVal<580){ //680 Ohm 58 | return 3; 59 | }else if(analogVal>600 && analogVal<700){ //1k2 Ohm 60 | return 4; 61 | }else{ //No resistor 62 | return 0; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /examples/Magic/drawdio/drawdio.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Drawdio 3 | 4 | Draw audio with drawdio. You might think that this pen is magic, 5 | and it might just be. Drawdio turns (almost) everything that is 6 | conductive into an instrument. You can either make music by 7 | drawing a picture or by touching the tip of it to different 8 | conductive things around you. 9 | 10 | (c) 2013 Arduino LLC 11 | Inspiration from Jay Siver's DRAWDIO 12 | http://web.media.mit.edu/~silver/drawdio/ 13 | 14 | */ 15 | 16 | #include 17 | #include "pitches.h" 18 | #include 19 | 20 | //Capacitive switch connected between 2 and 3 21 | CapacitiveSwitch me=CapacitiveSwitch(2,3); 22 | 23 | //A piezo speaker connected to digital pin 8 24 | Melody speaker=Melody(8); 25 | 26 | //The range of capacitive sensor values to be 27 | //mapped to music tones. See example 28 | //"help/CapacitiveSwitchTest" about how to get 29 | //the values 30 | int lowerThreshold=80; 31 | int upperThreshold=900; 32 | 33 | //Define the lowest and highest tone. Defined 34 | //in pitches.h 35 | int toneLow=NOTE_C3; 36 | int toneHigh=NOTE_B6; 37 | 38 | void setup(){ 39 | //Nothing to do here. Magic? 40 | } 41 | void loop(){ 42 | //Get value of the sensor. If it's smaller 43 | //than threshold, it'll be 0 44 | int value=me.getValue(lowerThreshold); 45 | 46 | //map the sensor value to tones. The map() 47 | //function maps values smaller than lower 48 | //threshold to the lowest desired value. If 49 | //you want to keep drawdio quiet when you're 50 | //not playing, you have to deal with 0 separately 51 | int pitch; 52 | if(value>0){ 53 | pitch=map(value,lowerThreshold,upperThreshold,toneLow,toneHigh); 54 | }else{ 55 | pitch=0; 56 | } 57 | 58 | //Play the tone to the speaker. 59 | speaker.playTone(pitch,10); 60 | } 61 | -------------------------------------------------------------------------------- /CapacitiveSensor/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | * CapitiveSense Library Demo Sketch 5 | * Paul Badger 2008 6 | * Uses a high value resistor e.g. 10M between send pin and receive pin 7 | * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values. 8 | * Receive pin is the sensor pin - try different amounts of foil/metal on this pin 9 | */ 10 | 11 | 12 | CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired 13 | CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil 14 | CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil 15 | 16 | void setup() 17 | { 18 | cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example 19 | Serial.begin(9600); 20 | } 21 | 22 | void loop() 23 | { 24 | long start = millis(); 25 | long total1 = cs_4_2.capacitiveSensor(30); 26 | long total2 = cs_4_6.capacitiveSensor(30); 27 | long total3 = cs_4_8.capacitiveSensor(30); 28 | 29 | Serial.print(millis() - start); // check on performance in milliseconds 30 | Serial.print("\t"); // tab character for debug windown spacing 31 | 32 | Serial.print(total1); // print sensor output 1 33 | Serial.print("\t"); 34 | Serial.print(total2); // print sensor output 2 35 | Serial.print("\t"); 36 | Serial.println(total3); // print sensor output 3 37 | 38 | delay(10); // arbitrary delay to limit data to serial port 39 | } 40 | -------------------------------------------------------------------------------- /examples/Magic/BinaryLP/BinaryLP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Binary LP 3 | This sort of works as an LP player. The difference is 4 | that instead of using a needle on a plastic disc, 5 | we use three IR sensors in a row to read a pattern from 6 | a paper disc. 7 | 8 | If you are both musically and digitally inclined, 9 | you will have lots of fun creating melodies with this. 10 | 11 | (c) 2014 Arduino LLC 12 | */ 13 | 14 | 15 | #include 16 | #include "pitches.h" 17 | 18 | //IRArray(left, middle, right); 19 | IRArray ir = IRArray(A1, A2, A3); 20 | 21 | int piezo = 8; //Piezo pin 22 | int lastReading = 0; 23 | 24 | void setup(){ 25 | pinMode(piezo, OUTPUT); 26 | } 27 | 28 | void loop(){ 29 | //Read the binary pattern and get a number from 0 to 7 30 | int reading = ir.readBinary(); 31 | 32 | //Play a note depending on the read value 33 | playNote(reading); 34 | 35 | } 36 | 37 | void playNote(int r){ 38 | //If the last note we played is the same as the new note 39 | //we make a short break so that two notes are distinguished 40 | if(lastReading==r){ 41 | noTone(piezo); 42 | delay(20); 43 | } 44 | 45 | //Play a different note depending on the value of b. 46 | //Check pitches.h to see which notes you can use. 47 | //In this case the scale is C Major. 48 | switch (r){ 49 | case 0: 50 | break; 51 | case 1: 52 | tone(piezo, NOTE_C4); 53 | break; 54 | case 2: 55 | tone(piezo, NOTE_D4); 56 | break; 57 | case 3: 58 | tone(piezo, NOTE_E4); 59 | break; 60 | case 4: 61 | tone(piezo, NOTE_F4); 62 | break; 63 | case 5: 64 | tone(piezo, NOTE_G4); 65 | break; 66 | case 6: 67 | tone(piezo, NOTE_A4); 68 | break; 69 | case 7: 70 | tone(piezo, NOTE_B4); 71 | break; 72 | } 73 | 74 | //If r is more than 0 we save that value to lastByte 75 | if(r>0)lastReading = r; 76 | } 77 | -------------------------------------------------------------------------------- /examples/help/LDR/LDR.ino: -------------------------------------------------------------------------------- 1 | /* 2 | LDR 3 | 4 | LDR is a sensor that reads the intensity of light. It gives 5 | analog reading. When connected with Arduino analog pins, 6 | its value ranges from 0 to 1023 depending on the amount of 7 | light shining on it. 8 | 9 | The BasicEducationShield library includes some features for 10 | using the LDR as a button, or get values directly. Se the 11 | example LDRtest fo more information about how to configure 12 | it correctly. 13 | 14 | (c) 2013 Arduino LLC 15 | */ 16 | 17 | 18 | #include 19 | 20 | //Tinkerkit LDR is connected to analog 1. 21 | LDR sensor = LDR(A1); 22 | 23 | void setup(){ 24 | 25 | Serial.begin(9600); 26 | 27 | //LDR needs to be configured with baseValue 28 | //and threshold. See LDRTest sketch for 29 | //details. 30 | sensor.config(700,900); 31 | } 32 | 33 | void loop(){ 34 | Serial.println("Please press..."); 35 | 36 | //pressed(timeout) 37 | // timeout: milliseconds within which the 38 | // LDR should be cover-uncovered. If 39 | // cover-uncovered within time, the 40 | // function returns 1, otherwise 0. If 41 | // not used, it waits untill being 42 | // cover-uncovered. 43 | // 44 | //Wait until the LDR gets cover-uncovered. 45 | Serial.println(sensor.pressed()); 46 | 47 | Serial.println("Please press..."); 48 | 49 | //released(timeout) 50 | // timeout: same as in pressed(timeout). 51 | // 52 | //Wait until the LDR gets uncover-covered. 53 | Serial.println(sensor.pressed()); 54 | 55 | 56 | While(true){ 57 | //getState() 58 | // returns whether the sensor readings has 59 | // passed the threshold. Returns true if the 60 | // threshold has been passed, false if not. 61 | // Unlike pressed or released, this function 62 | // does not stop the whole program. 63 | // 64 | //Continuously output whether the sensor has 65 | //passed threshold. 66 | Serial.println(sensor.getState()); 67 | delay(30); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /examples/Sports/basketball/basketball.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Basketball 3 | 4 | Score a goal! 5 | 6 | In this game, players will try to bounce a ping pong ball 7 | into a cup. Make five points to win. The score is tracked 8 | using a light dependent resistor (LDR). 9 | 10 | (c) 2013 Arduino LLC 11 | 12 | */ 13 | 14 | 15 | #include 16 | #include "pitches.h" 17 | /* 18 | An array of pin numbers to which LEDs are attached 19 | the defaults are 2 to 6 but you can choose any of the digital pins 20 | */ 21 | int ledPins[] = {2, 3, 4, 5, 6}; 22 | int pinCount = 5; 23 | VUMeter vuMeter; 24 | 25 | Melody piezo = Melody(8); // the piezo connected to digital pin 8 26 | LDR ldr = LDR(A1); //the ldr connected to analog pin 1 27 | 28 | int score = 0; 29 | 30 | void setup(){ 31 | //if your are using other pins than 2 to 6 you need to configure that here 32 | vuMeter.config(pinCount, ledPins); 33 | vuMeter.begin(); //does the same as pinMode, LEDs are outputs 34 | 35 | ldr.config(800, 600); //first run LDRtest example to see what values you need to put here 36 | } 37 | 38 | void loop(){ 39 | //if the ldr is covered the score increases with 1 40 | //and a sounds is played 41 | ldr.pressed(); 42 | score++; 43 | vuMeter.fill(score); //Turn on as many LEDs as the score 44 | 45 | int melody[] = { NOTE_GS4, NOTE_C5}; 46 | int noteDurations[] = { 8, 8}; 47 | int numberOfNotes = 2; 48 | piezo.play(numberOfNotes, melody, noteDurations, 1); 49 | 50 | delay(50); 51 | 52 | if(score>=pinCount) startOver(); //If the score equals the amount of LEDs you start over 53 | } 54 | 55 | 56 | void startOver(){ 57 | score=0; //reset the score 58 | 59 | int melody[] = { NOTE_C5, NOTE_G4,NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5}; 60 | int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 }; 61 | int numberOfNotes = 8; 62 | piezo.play(numberOfNotes, melody, noteDurations, 1); 63 | 64 | vuMeter.blinkAll(50,10); 65 | 66 | } 67 | 68 | -------------------------------------------------------------------------------- /examples/Magic/BoomBox/BoomBox.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Boom Box 3 | 4 | This is your first step towards theese kinds of skills: 5 | http://www.youtube.com/watch?v=FcJCxe1VSLA&noredirect=1 6 | The boom box is a small sample player you can use to make music, 7 | or just random sound. It comes with three prerecorded samples 8 | but we probably don’t have the same creative cleverness when it 9 | comes to recording as you do so you should definitely record your 10 | own samples too! 11 | 12 | (c) 2013 Arduino LLC 13 | */ 14 | 15 | #include "BasicEducationShield.h" 16 | 17 | //We need to include the SD library to be able to read from an SD card 18 | #include 19 | 20 | //Declare a button group with 3 buttons. The 21 | //sound player secrectly takes pin 3, 4 and 11, 22 | //so don't use them, 23 | ButtonGroup bg; 24 | int buttons[]={5,6,7}; 25 | 26 | //There're 3 buttons in the button group. 27 | int buttonsCount=3; 28 | 29 | //Declare the sound player 30 | Player player=Player(); 31 | 32 | void setup(){ 33 | // Open serial communications and wait for port to open: 34 | Serial.begin(9600); 35 | 36 | // Initialize the sound player. Open the serial monitor to see 37 | //the sound files found on your micro SD card 38 | player.begin(); 39 | 40 | //Initialize the button group. 41 | bg.begin(buttonsCount,buttons); 42 | } 43 | 44 | void loop(){ 45 | //Wait for one of the buttons to be pressed. 46 | //According to which button is pressed, it 47 | //returns either 0, 1 or 2 48 | int pressedButton=bg.pressed(); 49 | 50 | //Play a different sound according to the 51 | //button pressed. 52 | switch(pressedButton){ 53 | case 0: 54 | Serial.println("sound 0"); 55 | player.play("0.wav"); 56 | break; 57 | case 1: 58 | Serial.println("sound 1"); 59 | player.play("1.wav"); 60 | break; 61 | case 2: 62 | Serial.println("sound 2"); 63 | player.play("2.wav"); 64 | break; 65 | } 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map BasicEducationShield 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | VUMeter KEYWORD1 10 | Melody KEYWORD1 11 | Button KEYWORD1 12 | LDR KEYWORD1 13 | TiltSwitch KEYWORD1 14 | CapacitiveSwitch KEYWORD1 15 | LED KEYWORD1 16 | PiezoKnockSensor KEYWORD1 17 | Player KEYWORD1 18 | Knob KEYWORD1 19 | Joystick KEYWORD1 20 | Wheels KEYWORD1 21 | IRArray KEYWORD1 22 | UltrasonicSensor KEYWORD1 23 | 24 | ####################################### 25 | # Methods and Functions (KEYWORD2) 26 | ####################################### 27 | 28 | config KEYWORD2 29 | begin KEYWORD2 30 | test KEYWORD2 31 | getValue KEYWORD2 32 | pressed KEYWORD2 33 | released KEYWORD2 34 | doublePressed KEYWORD2 35 | getState KEYWORD2 36 | 37 | ### VUMeter ### 38 | on KEYWORD2 39 | off KEYWORD2 40 | scrollLeft KEYWORD2 41 | scrollRight KEYWORD2 42 | fill KEYWORD2 43 | fillFrom KEYWORD2 44 | blink KEYWORD2 45 | blinkAll KEYWORD2 46 | clear KEYWORD2 47 | 48 | ### Melody ### 49 | play KEYWORD2 50 | beep KEYWORD2 51 | playTone KEYWORD2 52 | effect_win KEYWORD2 53 | effect_gameover KEYWORD2 54 | effect_score KEYWORD2 55 | 56 | ### Button ### 57 | pressed KEYWORD2 58 | released KEYWORD2 59 | doublePressed KEYWORD2 60 | getState KEYWORD2 61 | 62 | ### PiezoKnockSensor ### 63 | knocked KEYWORD2 64 | 65 | ### Knob ### 66 | setLevels KEYWORD2 67 | getLevels KEYWORD2 68 | 69 | ### Joystick ### 70 | getX KEYWORD2 71 | getY KEYWORD2 72 | 73 | ### Wheels ### 74 | goForward KEYWORD2 75 | goBackwards KEYWORD2 76 | turnLeft KEYWORD2 77 | turnRight KEYWORD2 78 | standStill KEYWORD2 79 | follow KEYWORD2 80 | 81 | ### IRArray ### 82 | readBinary KEYWORD2 83 | readLine KEYWORD2 84 | 85 | ### UltrasonicSensor ### 86 | getDistance KEYWORD2 87 | 88 | ####################################### 89 | # Constants (LITERAL1) 90 | ####################################### -------------------------------------------------------------------------------- /Wheels.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | Wheels::Wheels(int lpin, int rpin){ 9 | this->lpin=lpin; 10 | this->rpin=rpin; 11 | } 12 | 13 | void Wheels::begin(){ 14 | //Must be called in setup 15 | top = 120; 16 | low = 60; 17 | still = 90; 18 | 19 | fromL = still; 20 | fromR = still; 21 | toL = still; 22 | toR = still; 23 | 24 | left.attach(lpin); 25 | delay(1000); 26 | right.attach(rpin); 27 | } 28 | 29 | void Wheels::go(int tl, int tr){ 30 | this->tl=tl; 31 | this->tr=tr; 32 | 33 | int lSpeed = fromL; 34 | int rSpeed = fromR; 35 | 36 | for(int i=0; i<(top-low); i++){ 37 | if(lSpeedtl) lSpeed--; 39 | if (rSpeedtr) rSpeed--; 41 | 42 | left.write(lSpeed); 43 | right.write(rSpeed); 44 | delay(20); 45 | if(lSpeed==tl && rSpeed==tr) i=top-low; 46 | } 47 | fromL=tl; 48 | fromR=tr; 49 | } 50 | 51 | void Wheels::goForward(){ 52 | toL = low; 53 | toR = top; 54 | go(toL, toR); 55 | } 56 | 57 | void Wheels::goBackwards(){ 58 | toL = top; 59 | toR = low; 60 | go(toL, toR); 61 | } 62 | void Wheels::turnLeft(){ 63 | toL = top; 64 | toR = top; 65 | go(toL, toR); 66 | } 67 | void Wheels::turnRight(){ 68 | toL = low; 69 | toR = low; 70 | go(toL, toR); 71 | } 72 | 73 | void Wheels::standStill(){ 74 | toL = still; 75 | toR = still; 76 | go(toL, toR); 77 | } 78 | 79 | void Wheels::follow(int d){ 80 | int leftSpeed = constrain(ROBOT_SPEED+d, -100, 100); 81 | int rightSpeed = constrain(ROBOT_SPEED-d, -100, 100); 82 | 83 | leftSpeed = map(-leftSpeed, -100, 100, 40, 140); 84 | rightSpeed = map(rightSpeed, -100, 100, 40, 140); 85 | 86 | Serial.print(leftSpeed); 87 | Serial.print(" "); 88 | Serial.println(rightSpeed); 89 | 90 | left.write(leftSpeed); 91 | right.write(rightSpeed); 92 | } 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/Magic/CookieMonster/CookieMonster.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | /* 6 | * Elektor Monster Jar February 2013 7 | * 8 | * File Elektor_feb13_monsterjar.ino 9 | * 10 | * Read the value of the a capacitive sensor between pins 2 and 3 and 11 | * turn on a ultra-bright LED to light up the scene as well as send 12 | * a character "p" (as in "picture") over serial when detecting the 13 | * value on the touch sensor went over a threshold. 14 | * 15 | * We place a 1M resistor between pins 2 and 3 and use pin 3 as touchpad. 16 | * Pin 9 is connected to the ultra-bright white LED. 17 | * 18 | * You will have to configure the value of the THRESHOLD constant for the 19 | * program to operate properly. Follow these steps: 20 | * 21 | * 1) connect your circuit to the aluminium folie 22 | * 2) measure the default value, depending on how much folie you have this 23 | * value could be in the range of 200 24 | * 3) touch the sensitive area and observe the measurement in the Serial Monitor 25 | * 4) make THRESHOLD = (MAX_VALUE + DEFAULT_VALUE) / 2 and change it in the code 26 | * 27 | * (c) 2013 B&D Cuartielles 28 | * 29 | * This code is Free Software, licensed under GPLv3 30 | * Based on code by Paul Badger 2008 31 | * 32 | */ 33 | 34 | //Define the capacitive sensor 35 | CapacitiveSwitch sensor=CapacitiveSwitch(2,3); 36 | //Use Tinkerkit LED(or Tinkerkit LED matrix) here. 37 | //Deinfe the LED 38 | LED led=LED(9); 39 | 40 | void setup() 41 | { 42 | // configure the serial port 43 | Serial.begin(9600); 44 | 45 | //initialize components 46 | sensor.config(200); 47 | led.begin(); 48 | } 49 | 50 | void loop(){ 51 | //When someone attempts to open the 52 | //cookie jar, the sensor is activated 53 | if(sensor.pressed()){ 54 | //Turn on the LED 55 | led.on(); 56 | //Sends signal to processing, so a picture 57 | //will be captured 58 | Serial.print('p'); 59 | //Wait 2 seconds before turning the LED off 60 | delay(2000); 61 | led.off(); 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /examples/Sports/racing/racing.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Racing 3 | 4 | Run with your fingers as fast as you can. 5 | 6 | In this game, the player must tap two sensors repeatedly 7 | in order to run laps. One LED will light up per lap. 8 | When all laps are completed, LEDs will blink in victory. 9 | 10 | (c) 2013 Arduino LLC 11 | */ 12 | 13 | #include 14 | #include 15 | 16 | /* 17 | An array of pin numbers to which LEDs are attached 18 | the defaults are 2 to 6 but you can choose any of the digital pins 19 | just remember to leave digital pin 9 and 10 for the buttons 20 | */ 21 | int ledPins[] = {9, 10, 11, 12, 13}; 22 | int pinCount = 5; 23 | //This variable will let us keep track on which LED to turn on 24 | int LED; 25 | VUMeter scoreBoard; 26 | 27 | //Configure the capacitive sensors 28 | int capacitiveSensorThreshold=400; 29 | CapacitiveSwitch leftFoot=CapacitiveSwitch(2,3); 30 | CapacitiveSwitch rightFoot=CapacitiveSwitch(2,4); 31 | 32 | int score; 33 | 34 | void setup(){ 35 | //initializing the game, set up all the components and variables 36 | score=0; 37 | 38 | //Connect scoreboard 39 | scoreBoard.config(pinCount,ledPins); 40 | scoreBoard.begin(); 41 | 42 | //initialize left and right "foot" 43 | leftFoot.config(capacitiveSensorThreshold); 44 | rightFoot.config(capacitiveSensorThreshold); 45 | } 46 | 47 | void loop(){ 48 | //Wait for the left foot to be pressed 49 | leftFoot.pressed(); 50 | scoreBoard.on(LED); 51 | 52 | //Wait for the right foot to be pressed 53 | rightFoot.pressed(); 54 | scoreBoard.off(LED); 55 | 56 | score=score+1; //After both feet are pressed, add one point 57 | 58 | //Every 20 points light up a led 59 | LED =score/20; 60 | scoreBoard.fill(LED); 61 | 62 | //When you get 100 points, you win 63 | if(score>100){ 64 | //if you win, blink all leds for celebration 65 | //See vuMeter in refence list to make your own blink animation 66 | scoreBoard.blinkAll(50,5); 67 | //and reset the game 68 | score=0; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /examples/help/CapacitiveSwitch/CapacitiveSwitch.ino: -------------------------------------------------------------------------------- 1 | /* CapacitiveSwitch 2 | * 3 | * CapacitiveSwitch can be constructed in various ways. 4 | * Typically a 1 million ohm resistor between sender pin 5 | * and receiver pin(we use 3 as sender, 2 as receiver 6 | * here), and conductive material connected to receive 7 | * pin. 8 | * 9 | * When you touch the conductive material with hand, or 10 | * other objects(should be conductive), the sensor will 11 | * get a different reading. 12 | * 13 | * The library includes some features for using the 14 | * capacitive sensor as a button, or get values directly. 15 | * 16 | * (c) 2013 Arduino LLC 17 | */ 18 | 19 | //Always import CapacitiveSensor.h when using CapacitiveSwitch 20 | #include 21 | 22 | #include 23 | 24 | //The sensor is connected between 2 and 3 by default 25 | CapacitiveSwitch me=CapacitiveSwitch(2,3); 26 | 27 | void setup(){ 28 | Serial.begin(9600); 29 | 30 | //Config the sensor, set threshold for the sensor used as 31 | //button. If sensor reading passes it, it's counted as 32 | //being pressed/released 33 | me.config(400); 34 | } 35 | void loop(){ 36 | Serial.println("Please press..."); 37 | //pressed(timeout) 38 | // timeout: milliseconds within which the sensor should 39 | // be pressed. If pressed within time, the function 40 | // returns 1, otherwise 0. If not used, it waits 41 | // untill being pressed. 42 | // 43 | //Wait until the capacitive sensor is pressed. 44 | Serial.println(me.pressed()); 45 | 46 | delay(1000); 47 | 48 | Serial.println("Please release..."); 49 | //released(timeout) 50 | // timeout: same as in pressed(timeout). 51 | // 52 | //Wait untill the capacitive sensor is released. 53 | Serial.println(me.released()); 54 | 55 | do{ 56 | //getValue(mininum) 57 | // minimum: minimum value that needs to be returned. If 58 | // the sensor reading is smaller than minimum, it'll 59 | // return 0. 60 | // 61 | //Print values bigger than 10. Print 0 otherwise. 62 | Serial.println(me.getValue(20)); 63 | }while(true); 64 | } 65 | -------------------------------------------------------------------------------- /examples/Robots/CameraRobot/Processing/CameraRobot/CameraRobot.pde: -------------------------------------------------------------------------------- 1 | /** 2 | * Camera Robot 3 | * 4 | * This program initializes a webcam connected to a PC. 5 | * If a "P" is pressed it will then take a picture with 6 | * the webcam and store it locally in the sketch folder. 7 | * 8 | * Based on Cookie Monster by David Cuartielles 9 | */ 10 | 11 | import processing.video.*; 12 | 13 | Capture cam; 14 | 15 | void setup() { 16 | size(640, 480, P2D); 17 | 18 | initializeCamera(0); 19 | } 20 | 21 | void draw() { 22 | if (cam.available() == true) { 23 | cam.read(); 24 | } 25 | image(cam, 0, 0); 26 | 27 | String timeStamp=createTimeStamp(); 28 | 29 | text(timeStamp, 10, height-10); 30 | 31 | 32 | 33 | // for the keyboard detection to work, you need to have 34 | // clicked on the application window first (aka focus) 35 | if(keyPressed) { 36 | if (key == 'p' || key == 'P') { 37 | captureImage(timeStamp); 38 | } 39 | } 40 | 41 | } 42 | 43 | 44 | 45 | 46 | void initializeCamera(int camNum){ 47 | String[] cameras = Capture.list(); 48 | 49 | if (cameras.length == 0) { 50 | println("There are no cameras available for capture."); 51 | exit(); 52 | } else { 53 | println("Available cameras:"); 54 | for (int i = 0; i < cameras.length; i++) { 55 | println("["+i+"] "+cameras[i]); 56 | } 57 | 58 | // The camera can be initialized directly using an element 59 | // from the array returned by list(): 60 | cam = new Capture(this, cameras[camNum]); 61 | cam.start(); 62 | } 63 | 64 | } 65 | 66 | String createTimeStamp(){ 67 | String timeStamp = String.format("%02d", hour()); 68 | timeStamp += ":" + String.format("%02d", minute()); 69 | timeStamp += ":" + String.format("%02d", second()); 70 | timeStamp += " " + year(); 71 | timeStamp += "/" + String.format("%02d", month()); 72 | timeStamp += "/" + String.format("%02d", day()); 73 | 74 | return timeStamp; 75 | } 76 | 77 | void captureImage(String timeStamp){ 78 | saveFrame("pic-######.png"); 79 | println("capturing Frame at: " + timeStamp); 80 | } 81 | -------------------------------------------------------------------------------- /examples/Sports/react/react.ino: -------------------------------------------------------------------------------- 1 | /* 2 | React 3 | Test your reaction time! 4 | 5 | In this game, one of three LEDs will randomly light up. 6 | You must tap the corresponding capacitive sensor as quick 7 | as possible. If you don’t react fast enough, the game is over. 8 | 9 | (c) 2013 Arduino LLC 10 | */ 11 | 12 | #include 13 | #include 14 | #include "pitches.h" 15 | 16 | //Define the 3 LEDs 17 | int ledPins[] = {9, 10, 11}; 18 | int pinCount = 3; 19 | VUMeter LEDs; 20 | 21 | //There're 3 pads for pressing 22 | CapacitiveSwitch pad[3]; 23 | 24 | //You have 500 milliseconds to press the pad 25 | int reactTime = 500; 26 | 27 | // the piezo connected to digital pin 8 28 | Melody piezo = Melody(8); 29 | 30 | void setup(){ 31 | LEDs.config(pinCount, ledPins); 32 | LEDs.begin(); 33 | 34 | //Configure the pads 35 | pad[0] = CapacitiveSwitch(2,3); 36 | pad[1] = CapacitiveSwitch(2,4); 37 | pad[2] = CapacitiveSwitch(2,5); 38 | pad[0].config(100); 39 | pad[1].config(100); 40 | pad[2].config(100); 41 | } 42 | 43 | void loop(){ 44 | //Wait for a random time before each turn begins 45 | delay(random(50, 2000)); 46 | 47 | //pick a target between the 3 pads 48 | int target = random(0, 3); 49 | //Light up the LED 50 | LEDs.on(target); 51 | 52 | //If the pad corresponding to the LED is pressed 53 | if(pad[target].pressed(reactTime)){ 54 | LEDs.off(target); 55 | 56 | //Play the winning sound 57 | int melody[] = { NOTE_GS4, NOTE_C5}; 58 | int noteDurations[] = { 8, 8}; 59 | int numberOfNotes = 2; 60 | piezo.play(numberOfNotes, melody, noteDurations, 1); 61 | } 62 | else{ 63 | //Else if the reaction is too slow, run the function gameOver() 64 | gameOver(); 65 | } 66 | } 67 | 68 | void gameOver(){ 69 | //Turn all LEDs on 70 | LEDs.fill(pinCount); 71 | 72 | //Play a melody 73 | int melody[] = { NOTE_E2, NOTE_C2}; 74 | int noteDurations[] = { 2, 1}; 75 | int numberOfNotes = 2; 76 | piezo.play(numberOfNotes, melody, noteDurations, 1); 77 | 78 | LEDs.blinkAll(100, 10); 79 | LEDs.fill(0); //Tun all LEDs off 80 | } 81 | -------------------------------------------------------------------------------- /examples/Sports/fencing/fencing.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Fencing 3 | Test your reaction time against an opponent! 4 | 5 | In this game, two players will hold tilt switch swords. 6 | When the green LED randomly lights up, the first person 7 | to swing their sword wins. 8 | (c) 2013 Aduino LLC 9 | */ 10 | 11 | #include 12 | 13 | //Position of the leds in VU-meter is represented 14 | //by their names here. So we can use names to find LEDs later 15 | #define YELLOW_LED_1 0 16 | #define GREEN_LED 1 17 | #define RED_LED 2 18 | #define YELLOW_LED_2 3 19 | 20 | //An array stores which pins the VU-meter is connected 21 | int ledPins[]={2,3,4,5}; 22 | //How many pins are used by VU-meter 23 | int pinCount=4; 24 | VUMeter lights; 25 | 26 | TiltSwitch player_1 = TiltSwitch(9); 27 | TiltSwitch player_2 = TiltSwitch(10); 28 | 29 | void setup(){ 30 | lights.config(pinCount,ledPins); 31 | 32 | //Initializing components 33 | lights.begin(); 34 | player_1.begin(); 35 | player_2.begin(); 36 | 37 | //We need this for generating random number later 38 | randomSeed(analogRead(0)); 39 | } 40 | void loop(){ 41 | lights.clear(); 42 | 43 | //Red led means both of you should hold the tilt switch sword up right 44 | lights.on(RED_LED); 45 | 46 | //Wait for a random period of time, between 3 seconds 47 | //And 6 seconds. Get ready! 48 | delay(random(3000,6000)); 49 | 50 | lights.off(RED_LED); 51 | //When the green led turns on, game starts 52 | lights.on(GREEN_LED); 53 | 54 | //Swing your swords as fast as you can, the faster one 55 | //will be returned by getWinner() 56 | int winner=getWinner(); 57 | 58 | //The yellow led by side of the winner will light up 59 | if(winner==1){ 60 | lights.on(YELLOW_LED_1); 61 | }else{ 62 | lights.on(YELLOW_LED_2); 63 | } 64 | delay(5000); 65 | } 66 | 67 | //The function below waits for either of the tilter 68 | //switch to be swang. The first one to swing 69 | //will be returned by its number 70 | int getWinner(){ 71 | do{ 72 | if(player_1.pressed(1)){ 73 | return 1; 74 | }else if(player_2.pressed(1)){ 75 | return 2; 76 | } 77 | }while(true); 78 | } 79 | -------------------------------------------------------------------------------- /examples/Robots/CameraRobot/CameraRobot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Camera Robot 3 | 4 | Spy on your cat with this robot camera. Mount the camera robot and 5 | control it with a small joystick. You can run everything from the 6 | computer and when you see your cat doing something suspicious, 7 | take a picture! 8 | 9 | You need to run the processing sketch included in the sketch folder 10 | to be able to take photos with a webcam connected to your computer. 11 | 12 | (c) 2013 Arduino LLC 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | //The joystick uses two analog pins. Here pin A0 and A1 19 | Joystick joystick = Joystick(A1,A0);//Joystick(XanalogPin, YanalogPin) 20 | 21 | Servo pan, tilt; 22 | 23 | int panAngle = 90; //Pan angle starts in the middle position 24 | int tiltAngle = 90; //Tilt angle starts in the middle position 25 | 26 | int panJoystick = 0; 27 | int tiltJoystick = 0; 28 | 29 | void setup(){ 30 | //servos are initialized 31 | pan.attach(9); 32 | tilt.attach(10); 33 | } 34 | void loop(){ 35 | panJoystick = joystick.getX(); //Get X value from joystick 36 | tiltJoystick = joystick.getY(); //Get Y value from joystick 37 | 38 | //If the joysticks X value isn't 0 we will pan 39 | if(panJoystick!=0){ 40 | //If the X value from the joystick equals 1 and 41 | //panAngle is less than 180 degrees, increase panAngle with 1 42 | if(panJoystick==1 && panAngle<180) panAngle++; 43 | //If the X value from the joystick equals -1 and 44 | //panAngle is more than 0 degrees, decrease panAngle with 1 45 | else if(panJoystick==-1 && panAngle>0) panAngle--; 46 | } 47 | //If the joysYicks Y value is 0 we will tilt 48 | if(tiltJoystick!=0){ 49 | //If the Y value from the joystick equals 1 and 50 | //tiltAngle is less than 180 degrees, increase tiltAngle with 1 51 | if(tiltJoystick==1 && tiltAngle<180) tiltAngle++; 52 | //If the Y value from the joystick equals -1 and 53 | //tiltAngle is more than 0 degrees, decrease tiltAngle with 1 54 | else if(tiltJoystick==-1 && tiltAngle>0) tiltAngle--; 55 | } 56 | pan.write(panAngle); //Set position of the pan servo 57 | tilt.write(tiltAngle); //Set position of the tilt servo 58 | delay(5); 59 | } 60 | -------------------------------------------------------------------------------- /examples/Magic/POV/POV.ino: -------------------------------------------------------------------------------- 1 | /* 2 | POV (Persistence of Vision) 3 | 4 | With POV, persistence of vision, you can take eg a bike wheel 5 | and make it into a display with just a few LEDs. Check out 6 | this video to see what we mean: 7 | http://www.youtube.com/watch?v=-TvGvVWS3IE 8 | 9 | In this project you will make one of these displays yourself. 10 | Wave it with long strokes or even hold it while running to 11 | display a text, a statement or a picture. The effect is most 12 | visible in a dark place so if you’re running, make sure not 13 | to run in to a tree or a friend. 14 | 15 | (c) 2013 Arduino LLC 16 | */ 17 | 18 | #include 19 | /* 20 | An array of pin numbers to which LEDs are attached 21 | the defaults are 2 to 6 but you can choose any of the digital pins 22 | */ 23 | int ledPins[] = {2, 3, 4, 5, 6}; 24 | int pinCount = 5; 25 | VUMeter vuMeter; 26 | 27 | int rowCount = 0; // Stores the number of rows 28 | int rowLength = 22; // Width of the message, copy this number to the message array 29 | int delayTime = 9; // Time it takes to show a row in milliseconds 30 | 31 | // The message where 0 is LOW and 1 is HIGH 32 | boolean message[5][22]={ 33 | // H H H H O O O O L L L L A A A A 34 | {0,1,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,1,1,0,0,0}, 35 | {0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,0}, 36 | {0,1,1,1,1,0,1,0,0,1,0,1,0,0,0,0,1,1,1,1,0,0}, 37 | {0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,0}, 38 | {0,1,0,0,1,0,0,1,1,0,0,1,1,1,1,0,1,0,0,1,0,0} 39 | 40 | }; 41 | 42 | void setup(){ 43 | // If your are using other pins than 2 to 6 you need to configure that here 44 | vuMeter.config(pinCount, ledPins); 45 | 46 | vuMeter.begin(); //does the same as pinMode, LEDs are outputs 47 | } 48 | 49 | void loop(){ 50 | // If the whole array has been drawn 51 | if(rowCount == rowLength) { 52 | rowCount = 0; // Reset the rowCount 53 | } else { 54 | // Shows the message 55 | for (int i = 0; i < pinCount; i++) { 56 | // Checks if the array says HIGH 57 | if (message[i][rowCount] == 1) { 58 | vuMeter.on(i); 59 | } else { 60 | vuMeter.off(i); 61 | } 62 | } 63 | rowCount++; 64 | } 65 | 66 | delay(delayTime);// This is the delay per row 67 | 68 | } 69 | -------------------------------------------------------------------------------- /IRArray.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | IRArray::IRArray(int IR1, int IR2, int IR3){ 9 | this->IR1=IR1; 10 | this->IR2=IR2; 11 | this->IR3=IR3; 12 | sensPins[0] = IR1; 13 | sensPins[1] = IR2; 14 | sensPins[2] = IR3; 15 | for(int i=0; i<3; i++){ 16 | sensorVal[i]=0; 17 | } 18 | } 19 | 20 | int IRArray::readBinary(){ 21 | for(int i=0; i<3; i++){ 22 | toBinary[i]=0; 23 | } 24 | 25 | bool check = true; 26 | while(check){ 27 | for(int i=0; i<3; i++){ 28 | sensorVal[i] = constrain(map(analogRead(sensPins[i]), 350, 400, 1, 0), 0, 1); 29 | } 30 | 31 | for(int i=0; i<3; i++){ 32 | if(sensorVal[i]) toBinary[i]=1; 33 | else if(!sensorVal[0]&&!sensorVal[1]&&!sensorVal[2]) check=false; //if all three are 0 34 | } 35 | } 36 | return translateBinary(); 37 | } 38 | 39 | int IRArray::translateBinary(){ 40 | byte o = (toBinary[2]?1:0)|(toBinary[1]?2:0)|(toBinary[0]?4:0); 41 | return o; 42 | } 43 | 44 | int IRArray::readLine(){ 45 | int sum=0; 46 | for(int i=0; i<3; i++){ 47 | int reading = constrain(analogRead(sensPins[i]), 60, 400); 48 | //int reading = constrain(analogRead(sensPins[i]), low[i], high[i]); 49 | 50 | sensorVal[i]=map(reading,60,400,0,127); 51 | sum+=sensorVal[i]; 52 | //Serial.println(sum); 53 | } 54 | int vel = 0; 55 | if(sum>LINE_THRESSHOLD) vel = calculateVelocity(sum); 56 | 57 | return vel; 58 | } 59 | 60 | int IRArray::calculateVelocity(int s){ 61 | s/=3; 62 | 63 | int diff = ((sensorVal[0]<<5)-(sensorVal[2]<<5))/s; 64 | diff = constrain(diff,-100,100); 65 | 66 | int velocity = (diff * KP)/10 + (diff-last_diff)*KD; 67 | velocity = constrain(velocity, -100, 100); 68 | //Serial.println(velocity); 69 | last_diff = diff; 70 | 71 | delay(INTEGRATION_TIME); 72 | 73 | return velocity; 74 | } 75 | 76 | void IRArray::test(){ 77 | for(int i=0; i<3; i++) sensorVal[i] = constrain(analogRead(sensPins[i]), 60, 400); 78 | 79 | Serial.print("IR1: "); 80 | Serial.print(sensorVal[0]); 81 | Serial.print(" IR2: "); 82 | Serial.print(sensorVal[1]); 83 | Serial.print(" IR3: "); 84 | Serial.println(sensorVal[2]); 85 | } 86 | -------------------------------------------------------------------------------- /pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/help/Melody/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/Magic/BinaryLP/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/Magic/drawdio/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/Sports/react/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/Sports/basketball/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 94 | 95 | 96 | -------------------------------------------------------------------------------- /Melody.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #include "pitches.h" 3 | 4 | #if ARDUINO >= 100 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | Melody::Melody(int pin){ 11 | this->pin=pin; 12 | /* 13 | for(int i=0;iplay(numberOfNotes, melody, noteDurations, 1); 48 | } 49 | void Melody::effect_gameover(){ 50 | int melody[] = { NOTE_E2, NOTE_C2}; 51 | int noteDurations[] = { 2, 1}; 52 | int numberOfNotes = 2; 53 | this->play(numberOfNotes, melody, noteDurations, 1); 54 | } 55 | void Melody::effect_score(){ 56 | int melody[] = { NOTE_GS4, NOTE_C5}; 57 | int noteDurations[] = { 8, 8}; 58 | int numberOfNotes = 2; 59 | this->play(numberOfNotes, melody, noteDurations, 1); 60 | } 61 | 62 | /* 63 | void Melody::setMelody(int length, int notes[],int duration[]){ 64 | this->length=length; 65 | for(int i=0;inotes[i]=notes[i]; 67 | this->duration[i]=1000/duration[i]; 68 | } 69 | } 70 | 71 | void Melody::play(){ 72 | for(int i=0;iduration[index]=duration; 92 | } 93 | int Melody::getDuration(int index){ 94 | return duration[index]; 95 | } 96 | */ 97 | -------------------------------------------------------------------------------- /examples/Magic/CookieMonster/Processing/CookieMonster/CookieMonster.pde: -------------------------------------------------------------------------------- 1 | /** 2 | * Hunting Cookie Monster 3 | * 4 | * Someone is eating our cookies, let's gather some proof 5 | * about that person and get him/her to confess. 6 | * 7 | * This program initializes a webcam connected to a PC 8 | * and listens to the serial port. If a "P" command arrives 9 | * it will then take a picture with the webcam and store it 10 | * locally in the sketch folder. 11 | * 12 | * (c) 2013 B. & D. Cuartielles in a quest for the truth 13 | * This is Free Software under GPLv3 14 | * Based on previous code by C. Reas and B. Fry 15 | */ 16 | 17 | import processing.video.*; 18 | import processing.serial.*; 19 | 20 | Capture cam; 21 | Serial myPort; // The serial port 22 | 23 | void setup() { 24 | size(640, 480, P2D); 25 | 26 | initializeCamera(0); 27 | 28 | // List all the available serial ports 29 | println(Serial.list()); 30 | // Open the port you are using at the rate you want: 31 | myPort = new Serial(this, Serial.list()[0], 9600); 32 | } 33 | 34 | void draw() { 35 | if (cam.available() == true) { 36 | cam.read(); 37 | } 38 | image(cam, 0, 0); 39 | 40 | String timeStamp=createTimeStamp(); 41 | 42 | text(timeStamp, 10, height-10); 43 | 44 | 45 | 46 | // for the keyboard detection to work, you need to have 47 | // clicked on the application window first (aka focus) 48 | if(keyPressed) { 49 | if (key == 'p' || key == 'P') { 50 | captureImage(timeStamp); 51 | } 52 | } 53 | 54 | if (myPort.available() > 0) { 55 | int inByte = myPort.read(); 56 | if(inByte == 'p') { 57 | captureImage(timeStamp); 58 | } 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | 66 | void initializeCamera(int camNum){ 67 | String[] cameras = Capture.list(); 68 | 69 | if (cameras.length == 0) { 70 | println("There are no cameras available for capture."); 71 | exit(); 72 | } else { 73 | println("Available cameras:"); 74 | for (int i = 0; i < cameras.length; i++) { 75 | println("["+i+"] "+cameras[i]); 76 | } 77 | 78 | // The camera can be initialized directly using an element 79 | // from the array returned by list(): 80 | cam = new Capture(this, cameras[camNum]); 81 | cam.start(); 82 | } 83 | 84 | // we don't need the camera at full blast 85 | frameRate(1); 86 | 87 | } 88 | 89 | String createTimeStamp(){ 90 | String timeStamp = String.format("%02d", hour()); 91 | timeStamp += ":" + String.format("%02d", minute()); 92 | timeStamp += ":" + String.format("%02d", second()); 93 | timeStamp += " " + year(); 94 | timeStamp += "/" + String.format("%02d", month()); 95 | timeStamp += "/" + String.format("%02d", day()); 96 | 97 | return timeStamp; 98 | } 99 | 100 | void captureImage(String timeStamp){ 101 | saveFrame("pic-######.png"); 102 | println("capturing Frame at: " + timeStamp); 103 | } 104 | -------------------------------------------------------------------------------- /examples/Sports/pong/pong.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Pong 3 | 4 | Play a simplified version of the classic arcade game, Pong! 5 | 6 | In this game, a “pong” will move across five LEDs (VU-meter) 7 | and bounce back and forth as players press the button. 8 | Players must press the button at the right time in order to 9 | return the pong. 10 | 11 | (c) 2013 Arduino LLC 12 | */ 13 | 14 | #include 15 | /* 16 | An array of pin numbers to which LEDs are attached 17 | the defaults are 2 to 6 but you can choose any of the digital 18 | pins. Just remember to leave digital pin 9 and 10 for the buttons. 19 | */ 20 | int ledPins[] = {2, 3, 4, 5, 6}; 21 | int pinCount = 5; 22 | VUMeter vuMeter; 23 | 24 | Button button1 = Button(9); //the button connected to digital pin 9 25 | Button button2 = Button(10); //the button connected to digital pin 10 26 | 27 | int ledTime = 100; //determines how fast the LEDs will switch 28 | int pressTime = 200; //determines how long time a player has to press the button 29 | int buttonNotPressed = 0; //this keep track on who missed to press the button 30 | 31 | void setup(){ 32 | //if your are using other pins than 2 to 6 you need to configure that here 33 | vuMeter.config(pinCount, ledPins); 34 | 35 | vuMeter.begin(); //does the same as pinMode, LEDs are outputs 36 | button1.begin(); //does the same as pinMode, buttons are inputs 37 | button2.begin(); //does the same as pinMode, buttons are inputs 38 | 39 | vuMeter.scrollLeft(ledTime, 1); //The game starts by scrolling the LEDs to the left 40 | } 41 | 42 | void loop(){ 43 | /* 44 | if button1 is pressed within the press time, the game will continue 45 | by scrolling the LEDs to the right 46 | else if button1 is not pressed, the program will jump to gameOver() 47 | */ 48 | if(button1.released(pressTime)){ 49 | vuMeter.scrollRight(ledTime, 1); 50 | } 51 | else{ 52 | buttonNotPressed = 1; //Keep track on where we are in the game 53 | gameOver(); 54 | } 55 | 56 | /* 57 | if button2 is pressed within the press time, the game will continue 58 | by scrolling the LEDs to the left 59 | else if button2 is not pressed, the program will jump to gameOver() 60 | */ 61 | if(button2.released(pressTime)){ 62 | vuMeter.scrollLeft(ledTime, 1); 63 | } 64 | else{ 65 | buttonNotPressed = 2; //Keep track on where we are in the game 66 | gameOver(); 67 | } 68 | } 69 | 70 | /* 71 | When a player doesn't press the right button within the right 72 | time it is game over. Inside the function gameOver() you can 73 | decide how the LEDs should blink. 74 | Use vuMeter.blink(LED,delayTime,numberOfBlinks) to make one specific LED blink 75 | Use vuMeter.blinkAll(delayTime,numberOfBlinks) to make all LEDs blink 76 | */ 77 | void gameOver(){ 78 | vuMeter.blinkAll(100,10); 79 | 80 | if(buttonNotPressed==1) vuMeter.scrollRight(ledTime, 1); //if button1 was not pressed, scroll LEDs to right to start over 81 | else if(buttonNotPressed==2) vuMeter.scrollLeft(ledTime, 1); //if button2 was not pressed, scroll LEDs to left to start over 82 | } 83 | -------------------------------------------------------------------------------- /Player.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | #if ARDUINO >= 100 3 | #include "Arduino.h" 4 | #else 5 | #include "WProgram.h" 6 | #endif 7 | 8 | Player::Player(){ 9 | //this->pin=pin; 10 | //this->pressedValue=pressedValue; 11 | } 12 | void Player::begin(){ 13 | //Must be called in setup 14 | initPlayer(); 15 | initSD(); 16 | } 17 | 18 | void Player::play(char* name) { 19 | this->name=name; 20 | int count=0; 21 | 22 | // open wave file from sdcard 23 | File myFile = SD.open(name); 24 | if (!myFile) { 25 | // if the file didn't open, print an error and stop 26 | Serial.println("error opening test.wav"); 27 | while (true); 28 | } 29 | 30 | const int S=512; // Number of samples to read in block 31 | byte buffer[S]; 32 | 33 | Serial.print("Playing"); 34 | // until the file is not finished 35 | int header = 128; 36 | byte data = 0; 37 | while (myFile.available()) { 38 | // read from the file into buffer 39 | //myFile.read(buffer, sizeof(buffer)); 40 | data = myFile.read(); 41 | header--; 42 | 43 | if (header <= 0) { 44 | header = 0; 45 | analogWrite(3, data); 46 | delayMicroseconds(70); 47 | } 48 | 49 | // Prepare samples 50 | // Dump the whole song to the serial port 51 | //Serial.write(buffer, sizeof(buffer)); 52 | 53 | // Every 100 block print a '.' 54 | count++; 55 | if (count == 1000) { 56 | Serial.print("."); 57 | count = 0; 58 | } 59 | } 60 | myFile.close(); 61 | 62 | Serial.println("End of file. Thank you for listening!"); 63 | 64 | } 65 | 66 | void Player::initPlayer() { 67 | // set the PWM on pin 3 to 31250 68 | TCCR2B = TCCR2B & 0b11111000 | 0x01; 69 | } 70 | 71 | void Player::initSD() { 72 | Serial.print("Initializing SD card..."); 73 | // On the Ethernet Shield, CS is pin 4. It's set as an output by default. 74 | // Note that even if it's not used as the CS pin, the hardware SS pin 75 | // (10 on most Arduino boards, 53 on the Mega) must be left as an output 76 | // or the SD library functions will not work. 77 | pinMode(10, OUTPUT); 78 | 79 | if (!SD.begin(4)) { 80 | Serial.println("initialization failed!"); 81 | return; 82 | } 83 | Serial.println("initialization done."); 84 | 85 | root = SD.open("/"); 86 | 87 | printDirectory(root, 0); 88 | 89 | Serial.println("done!"); 90 | } 91 | void Player::printDirectory(File dir, int numTabs) { 92 | while(true) { 93 | 94 | File entry = dir.openNextFile(); 95 | if (! entry) { 96 | // no more files 97 | //Serial.println("**nomorefiles**"); 98 | break; 99 | } 100 | for (uint8_t i=0; i 19 | 20 | //The number of knocks that can be recorded 21 | #define MAX_KNOCKS 30 22 | 23 | PiezoKnockSensor sensor=PiezoKnockSensor(A0); 24 | Melody speaker=Melody(8); 25 | 26 | //An array for remembering the knock pattern 27 | long timer[MAX_KNOCKS]; 28 | 29 | //If it has started recording 30 | boolean started; 31 | 32 | //Used for calculating if you have finished the pattern 33 | long timeoutBase; 34 | 35 | //If you stop knocking for the period of timeout, it'll 36 | //stop recording 37 | long timeout=2000; 38 | 39 | //Keep track of the number of knocks you've knocked 40 | int currentKnock; 41 | 42 | void setup(){ 43 | //define the threshold and debounce time of the knock 44 | //sensor. Threshold defines how hard you need to knock, 45 | //debounce time prevents the sensor from detecting 46 | //false knocks, but also limits how rapid you can knock. 47 | sensor.config(40,80); 48 | 49 | //initializing the values 50 | started=false; 51 | timeoutBase=0; 52 | currentKnock=0; 53 | clearArray(); 54 | } 55 | 56 | void loop(){ 57 | //Knock sensor waits for a short time if a knock is detected 58 | //and then move on. 59 | if(sensor.knocked(10)){ 60 | //If it's the first knock in the round, start recording 61 | if(!started){ 62 | started=true; 63 | } 64 | 65 | long currentTime=millis(); 66 | //Reset timeout 67 | timeoutBase=currentTime; 68 | //Save the amount of milliseconds that have 69 | //passed since the last knock 70 | timer[currentKnock]=currentTime; 71 | currentKnock++; 72 | } 73 | if(started){ 74 | //If recording has started and you stop 75 | //knocking for the time of "timeout", it'll 76 | //stop recording and play it back to you. 77 | if(millis()-timeoutBase>timeout){ 78 | playback(); 79 | //reset the parameters, so a new round begins 80 | started=false; 81 | clearArray(); 82 | currentKnock=0; 83 | } 84 | } 85 | } 86 | void clearArray(){ 87 | //clean up values in the timer array 88 | for(int i=0;i 13 | 14 | /* 15 | An array of pin numbers to which LEDs are attached 16 | the defaults are 2 to 6 but you can choose any of the digital 17 | pins. 18 | */ 19 | int ledPins[] = {2, 3, 4, 5, 6}; 20 | int pinCount = 5; 21 | VUMeter vuMeter; 22 | 23 | Knob pot=Knob(A0);//a knob is connected to A0 24 | 25 | Button button = Button(9); //a button is connected to port D9 26 | 27 | Melody piezo=Melody(8);//a piezo is connected to digital pin 8 28 | 29 | //Set the game parameters 30 | int turns_begin=2;//a new game starts with 2 turns 31 | int turns_max=10;//the most difficult game has 10 turns 32 | 33 | int game[10];//array for storing the "simon says" 34 | int turns=2;//for storing the number of turns in current game 35 | int blinkTime=500;//how fast does "simon says" blink 36 | 37 | void setup(){ 38 | //initialize the components 39 | vuMeter.config(pinCount, ledPins); 40 | vuMeter.begin(); 41 | pot.setLevels(5); 42 | button.begin(); 43 | 44 | //Create a random seed, so we can call random() later. 45 | randomSeed(analogRead(A5)); 46 | } 47 | void loop(){ 48 | newGame(); 49 | simonSays(); 50 | delay(1000); 51 | getInputs(); 52 | } 53 | 54 | void newGame(){ 55 | vuMeter.blinkAll(200, 3); 56 | vuMeter.clear(); 57 | delay(500); 58 | //Generate simon says, it'll be stored in an array 59 | //So we can compare with player's input later 60 | for(int i=0;i 22 | 23 | //Declare the VUMeter 24 | VUMeter me; 25 | 26 | //The pins used by the VUMeter, default to 2-6. Can be 27 | //customized as wish, but do change the pinCount if the 28 | //number of LEDs are changed. 29 | int pins[]={2,3,4,5,6}; 30 | 31 | //How many pins are used. Should reflect the pins array above. 32 | int pinCount=5; 33 | 34 | void setup(){ 35 | //Configure the VU meter using parameters defined previously. 36 | me.config(pinCount,pins); 37 | 38 | //initialize the component. Must be called. 39 | me.begin(); 40 | 41 | } 42 | void loop(){ 43 | //fill(number) 44 | // number: how many leds from first pin shall be turned on 45 | // 46 | //Fill 5 LEDs 47 | me.fill(5); 48 | delay(3000); 49 | 50 | //clear() 51 | // 52 | //turn all the LEDs off 53 | me.clear(); 54 | delay(1000); 55 | 56 | //on(index) 57 | // index: which LED being turned on 58 | // 59 | //Turn the 3rd LED on 60 | me.on(2); 61 | delay(1000); 62 | 63 | //off(index) 64 | // index: which LED being turned off 65 | // 66 | //Turn the 3rd LED off 67 | me.off(2); 68 | delay(1000); 69 | 70 | //scrollRight(speed, startIndex) 71 | // speed: milliseconds before the next LED turns on 72 | // startIndex: from which LED to the left it start 73 | // scrolling. If not specified, it's 1. 74 | // 75 | //One LED light up at a time, scroll from left to right 76 | me.scrollRight(700); 77 | 78 | //scrollLeft(speed, startIndex) 79 | // speed: milliseconds before the next LED turns on 80 | // startIndex: from which LED TO THE RIGHT it start 81 | // scrolling. If not specified, it's 1. 82 | // 83 | //And then scroll back from the 2nd on the right 84 | me.scrollLeft(700,1); 85 | 86 | //blink(index, speed, times) 87 | // index: which LED should blink 88 | // speed: milliseconds, of which the LED light on and off 89 | // times: how many times the LED blinks. Defaults to 1 90 | // 91 | //the 3rd led will be blinking for 10 times, each time 92 | //with 100 milliseconds on and 100 milliseconds off 93 | me.blink(2,100,10); 94 | 95 | //blinkAll(speed, times) 96 | // speed: milliseconds, of which all LEDs light on and off 97 | // times: how many times the LED blinks. Defaults to 1 98 | // 99 | //All LEDs will be blinking 10 times 100 | me.blinkAll(100,10); 101 | 102 | //fillFrom(leftIndex,rightIndex) 103 | // leftIndex: start filling from which LED 104 | // rightIndex: end filling to which LED 105 | // 106 | //The 2nd to 4th LED will be light up 107 | me.fillFrom(1,3); 108 | delay(2000); 109 | 110 | } 111 | -------------------------------------------------------------------------------- /VUMeter.cpp: -------------------------------------------------------------------------------- 1 | #include "BasicEducationShield.h" 2 | 3 | #if ARDUINO >= 100 4 | #include "Arduino.h" 5 | #else 6 | #include "WProgram.h" 7 | #endif 8 | 9 | VUMeter::VUMeter(){ 10 | //default pins 11 | //be 5 pins, 2~6 12 | pins[0]=2; 13 | pins[1]=3; 14 | pins[2]=4; 15 | pins[3]=5; 16 | pins[4]=6; 17 | for(int i=5;ipins[i]=-1; 19 | } 20 | } 21 | void VUMeter::config(int length, int pins[]){ 22 | //set the pins used for vu-meter 23 | //length is the number of pins used, 24 | //pins is an array of all pins 25 | for(int i=0;ipins[i]=pins[i]; 27 | } 28 | for(int i=length;ipins[i]=-1; 30 | } 31 | this->pinCount=length; 32 | } 33 | void VUMeter::begin(){ 34 | //Must be called before starting 35 | //set all the attached pins as OUTPUT 36 | for(int i=0;i-1;i--){ 55 | blinkOnce(i,speed,0); 56 | } 57 | } 58 | void VUMeter::scrollRight(int speed,int startIndex){ 59 | //leds will turn on one at a time, 60 | //each for the duration of speed in milliseconds, 61 | //start with the startIndex counting from left, 62 | //go towards right 63 | 64 | clear(); 65 | for(int i=0+startIndex;ipins[i]); 104 | digitalWrite(pins[i],HIGH); 105 | } 106 | 107 | } 108 | 109 | void VUMeter::blinkOnce(int index,int onTime,int offTime){ 110 | //help function, blink a led once with controlled time 111 | digitalWrite(pins[index],HIGH); 112 | delay(onTime); 113 | digitalWrite(pins[index],LOW); 114 | delay(offTime); 115 | } 116 | 117 | void VUMeter::clear(){ 118 | //help function, set all pins to low 119 | for(int i=0;i 16 | 17 | //declare the pins used for leds, 9 leds in total 18 | int pinCount=9; 19 | int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 11, 12}; 20 | 21 | /* 22 | declare the tilt switch, it's connected to tinkerkit 23 | port 9 24 | */ 25 | TiltSwitch ts=TiltSwitch(9); 26 | 27 | /* 28 | Define the patterns of die values. Each pattern is 29 | an array of 9 integers, indicating the on/off state 30 | of each led. 31 | And because there're 6 possible patterns, we need a 32 | 2-dimensional array to define all the data. It's a 33 | big array of 6 elements, each element is an array of 34 | 9 integers. 35 | */ 36 | int dice[6][9]={ 37 | //1 38 | { 39 | 0,0,0, 40 | 0,1,0, 41 | 0,0,0 42 | }, 43 | 44 | //2 45 | { 46 | 1,0,0, 47 | 0,0,0, 48 | 0,0,1 49 | }, 50 | 51 | //3 52 | { 53 | 1,0,0, 54 | 0,1,0, 55 | 0,0,1 56 | }, 57 | 58 | //4 59 | { 60 | 1,0,1, 61 | 0,0,0, 62 | 1,0,1 63 | }, 64 | 65 | //5 66 | { 67 | 1,0,1, 68 | 0,1,0, 69 | 1,0,1 70 | }, 71 | 72 | //6 73 | { 74 | 1,1,1, 75 | 0,0,0, 76 | 1,1,1 77 | } 78 | }; 79 | 80 | /* 81 | wait time between the die rolls to a different face. 82 | Notice it's using float type here? Read on! 83 | */ 84 | float waitTime=1; 85 | 86 | void setup(){ 87 | //Configure each pin as an output. 88 | for(int i=0;i= 100 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | #include "../CapacitiveSensor/CapacitiveSensor.h" 11 | #include "../Servo/Servo.h" 12 | #include "../SD/utility/SdFat.h" 13 | #include "../SD/utility/SdFatUtil.h" 14 | #include "../SD/SD.h" 15 | 16 | 17 | #define LED_LENGTH 20 18 | #define BUTTONGROUP_LENGTH 10 19 | 20 | class VUMeter{ 21 | public: 22 | VUMeter(); 23 | void config(int length, int pins[]); 24 | void begin(); 25 | void on(int index); 26 | void off(int index); 27 | void scrollLeft(int speed,int startIndex=0); 28 | void scrollRight(int speed,int startIndex=0); 29 | void fill(int length); 30 | void fillFrom(int leftIndex,int rightIndex); 31 | void blink(int index, int speed, int times=1); 32 | void blinkAll(int speed,int times=1); 33 | void clear(); 34 | 35 | void test(); 36 | private: 37 | void blinkOnce(int index,int onTime,int offTime); 38 | //int getPin(int index); 39 | int pins[LED_LENGTH]; 40 | int pinCount; 41 | }; 42 | 43 | #define MELODY_LENGTH 32 44 | 45 | class Melody{ 46 | public: 47 | Melody(int pin); 48 | void play(int length, int notes[],int diration[], float speed=1.3); 49 | void beep(int note=20, int length=30); 50 | void playTone(int note, int length=10); 51 | 52 | void effect_win(); 53 | void effect_gameover(); 54 | void effect_score(); 55 | 56 | private: 57 | int pin; 58 | 59 | }; 60 | 61 | class Button{ 62 | public: 63 | Button(int pin=8,bool pressedValue=HIGH); 64 | void begin(); 65 | bool pressed(int timeout=0); 66 | bool released(int timeout=0); 67 | bool doublePressed(int timeout=0,int tolerance=500); 68 | virtual bool getState(); 69 | 70 | protected: 71 | int pin; 72 | bool pressedValue; 73 | bool checkPress(int timeout, bool requiredState); 74 | 75 | }; 76 | 77 | class ButtonGroup{ 78 | public: 79 | ButtonGroup(); 80 | void begin(int length, int buttons[], bool pressedValue=HIGH); 81 | int pressed(int timeout=0); 82 | private: 83 | int buttons[BUTTONGROUP_LENGTH]; 84 | //bool iStarted[BUTTONGROUP_LENGTH]; 85 | int buttonsCount; 86 | bool pressedValue; 87 | 88 | int checkPress(int timeout, bool requiredState); 89 | }; 90 | 91 | class LDR : public Button{ 92 | public: 93 | LDR(int pin=A1); 94 | void config(int baseValue,int threashold); 95 | virtual bool getState(); 96 | void test(); 97 | 98 | protected: 99 | int base; 100 | int threashold; 101 | 102 | }; 103 | 104 | class TiltSwitch : public Button{ 105 | public: 106 | TiltSwitch(int pin, bool pressedValue=LOW); 107 | }; 108 | 109 | class CapacitiveSwitch : public Button{ 110 | public: 111 | CapacitiveSwitch(int pin_in=13, int pin=12); 112 | void config(int threashold); 113 | void test(); 114 | long getValue(int min=0); 115 | virtual bool getState(); 116 | 117 | protected: 118 | CapacitiveSensor sensor; 119 | int threashold; 120 | }; 121 | 122 | class LED{ 123 | //Mostly for the LED component in Tinkerkit 124 | public: 125 | LED(int pin=9); 126 | void begin(); 127 | void on(); 128 | void off(); 129 | void blink(int speed,int times=1); 130 | private: 131 | int pin; 132 | }; 133 | 134 | class PiezoKnockSensor{ 135 | public: 136 | PiezoKnockSensor(int pin=A0); 137 | void config(int threshold=40,int debounceTime=80); 138 | bool knocked(int timeout=0); 139 | void test(); 140 | private: 141 | int pin; 142 | int threshold; 143 | long debounceTime; 144 | }; 145 | class Player{ 146 | public: 147 | Player(); 148 | void begin(); 149 | void play(char* name); 150 | 151 | private: 152 | void initPlayer(); 153 | void initSD(); 154 | void printDirectory(File dir, int numTabs); 155 | File root; 156 | char* name; 157 | }; 158 | 159 | class Knob{ 160 | public: 161 | Knob(int pin); 162 | int getValue(); 163 | void setLevels(int levels); 164 | int getLevel(); 165 | private: 166 | int pin; 167 | int levels; 168 | }; 169 | 170 | class Joystick{ 171 | public: 172 | Joystick(int x, int y); 173 | int getX(); 174 | int getY(); 175 | private: 176 | int x; 177 | int y; 178 | }; 179 | 180 | class Wheels{ 181 | public: 182 | Wheels(int lpin=10, int rpin=9); 183 | void begin(); 184 | void goForward(); 185 | void goBackwards(); 186 | void turnLeft(); 187 | void turnRight(); 188 | void standStill(); 189 | void follow(int d); 190 | private: 191 | int top, low, still; 192 | void go(int tl, int tr); 193 | int lpin, rpin; 194 | Servo left, right; 195 | int fromL, fromR; 196 | int toL, toR; 197 | int tl, tr; 198 | }; 199 | 200 | #define KP 9 201 | #define KD 1 202 | #define LINE_THRESSHOLD 30 203 | #define ROBOT_SPEED 50 //In % [0..100] 204 | #define INTEGRATION_TIME 10 205 | 206 | class IRArray{ 207 | public: 208 | IRArray(int IR1=A1, int IR2=A2, int IR3=A3); 209 | void begin(); 210 | void test(); 211 | int readBinary(); 212 | int readLine(); 213 | private: 214 | int IR1, IR2, IR3; 215 | int sensPins[3]; 216 | int sensorVal[3]; 217 | int toBinary[3]; 218 | int translateBinary(); 219 | int calculateVelocity(int s); 220 | int last_diff; 221 | 222 | }; 223 | 224 | class UltrasonicSensor{ 225 | public: 226 | UltrasonicSensor(int trig=11, int echo=12); 227 | int getDistance(); 228 | private: 229 | int trig, echo; 230 | }; 231 | 232 | 233 | #endif 234 | -------------------------------------------------------------------------------- /CapacitiveSensor/CapacitiveSensor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring 3 | Copyright (c) 2009 Paul Bagder All right reserved. 4 | Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix 5 | vim: set ts=4: 6 | */ 7 | 8 | #if ARDUINO >= 100 9 | #include "Arduino.h" 10 | #else 11 | #include "WProgram.h" 12 | #include "pins_arduino.h" 13 | #include "WConstants.h" 14 | #endif 15 | 16 | #include "CapacitiveSensor.h" 17 | 18 | // Constructor ///////////////////////////////////////////////////////////////// 19 | // Function that handles the creation and setup of instances 20 | 21 | CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin) 22 | { 23 | uint8_t sPort, rPort; 24 | 25 | // initialize this instance's variables 26 | // Serial.begin(9600); // for debugging 27 | error = 1; 28 | loopTimingFactor = 310; // determined empirically - a hack 29 | 30 | CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000; 31 | CS_AutocaL_Millis = 20000; 32 | 33 | // Serial.print("timwOut = "); 34 | // Serial.println(CS_Timeout_Millis); 35 | 36 | // get pin mapping and port for send Pin - from PinMode function in core 37 | 38 | #ifdef NUM_DIGITAL_PINS 39 | if (sendPin >= NUM_DIGITAL_PINS) error = -1; 40 | if (receivePin >= NUM_DIGITAL_PINS) error = -1; 41 | #endif 42 | 43 | sBit = digitalPinToBitMask(sendPin); // get send pin's ports and bitmask 44 | sPort = digitalPinToPort(sendPin); 45 | sReg = portModeRegister(sPort); 46 | sOut = portOutputRegister(sPort); // get pointer to output register 47 | 48 | rBit = digitalPinToBitMask(receivePin); // get receive pin's ports and bitmask 49 | rPort = digitalPinToPort(receivePin); 50 | rReg = portModeRegister(rPort); 51 | rIn = portInputRegister(rPort); 52 | rOut = portOutputRegister(rPort); 53 | 54 | // get pin mapping and port for receive Pin - from digital pin functions in Wiring.c 55 | noInterrupts(); 56 | *sReg |= sBit; // set sendpin to OUTPUT 57 | interrupts(); 58 | leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin 59 | lastCal = millis(); // set millis for start 60 | } 61 | 62 | // Public Methods ////////////////////////////////////////////////////////////// 63 | // Functions available in Wiring sketches, this library, and other libraries 64 | 65 | long CapacitiveSensor::capacitiveSensor(uint8_t samples) 66 | { 67 | total = 0; 68 | if (samples == 0) return 0; 69 | if (error < 0) return -1; // bad pin 70 | 71 | 72 | for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter 73 | if (SenseOneCycle() < 0) return -2; // variable over timeout 74 | } 75 | 76 | // only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline 77 | // this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal 78 | 79 | if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total - leastTotal) < (int)(.10 * (float)leastTotal) ) { 80 | 81 | //Serial.println(); // debugging 82 | // Serial.println("auto-calibrate"); 83 | // Serial.println(); 84 | // delay(2000); */ 85 | 86 | leastTotal = 0x0FFFFFFFL; // reset for "autocalibrate" 87 | lastCal = millis(); 88 | } 89 | /*else{ // debugging 90 | Serial.print(" total = "); 91 | Serial.print(total); 92 | 93 | Serial.print(" leastTotal = "); 94 | Serial.println(leastTotal); 95 | 96 | Serial.print("total - leastTotal = "); 97 | x = total - leastTotal ; 98 | Serial.print(x); 99 | Serial.print(" .1 * leastTotal = "); 100 | x = (int)(.1 * (float)leastTotal); 101 | Serial.println(x); 102 | } */ 103 | 104 | // routine to subtract baseline (non-sensed capacitance) from sensor return 105 | if (total < leastTotal) leastTotal = total; // set floor value to subtract from sensed value 106 | return(total - leastTotal); 107 | 108 | } 109 | 110 | long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples) 111 | { 112 | total = 0; 113 | if (samples == 0) return 0; 114 | if (error < 0) return -1; // bad pin - this appears not to work 115 | 116 | for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter 117 | if (SenseOneCycle() < 0) return -2; // variable over timeout 118 | } 119 | 120 | return total; 121 | } 122 | 123 | 124 | void CapacitiveSensor::reset_CS_AutoCal(void){ 125 | leastTotal = 0x0FFFFFFFL; 126 | } 127 | 128 | void CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){ 129 | CS_AutocaL_Millis = autoCal_millis; 130 | } 131 | 132 | void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){ 133 | CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000; // floats to deal with large numbers 134 | } 135 | 136 | // Private Methods ///////////////////////////////////////////////////////////// 137 | // Functions only available to other functions in this library 138 | 139 | int CapacitiveSensor::SenseOneCycle(void) 140 | { 141 | noInterrupts(); 142 | *sOut &= ~sBit; // set Send Pin Register low 143 | 144 | *rReg &= ~rBit; // set receivePin to input 145 | *rOut &= ~rBit; // set receivePin Register low to make sure pullups are off 146 | 147 | *rReg |= rBit; // set pin to OUTPUT - pin is now LOW AND OUTPUT 148 | *rReg &= ~rBit; // set pin to INPUT 149 | 150 | *sOut |= sBit; // set send Pin High 151 | interrupts(); 152 | 153 | while ( !(*rIn & rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value 154 | total++; 155 | } 156 | 157 | if (total > CS_Timeout_Millis) { 158 | return -2; // total variable over timeout 159 | } 160 | 161 | // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V 162 | noInterrupts(); 163 | *rOut |= rBit; // set receive pin HIGH - turns on pullup 164 | *rReg |= rBit; // set pin to OUTPUT - pin is now HIGH AND OUTPUT 165 | *rReg &= ~rBit; // set pin to INPUT 166 | *rOut &= ~rBit; // turn off pullup 167 | 168 | *sOut &= ~sBit; // set send Pin LOW 169 | interrupts(); 170 | 171 | while ( (*rIn & rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout 172 | total++; 173 | } 174 | // Serial.println(total); 175 | 176 | if (total >= CS_Timeout_Millis) { 177 | return -2; // total variable over timeout 178 | } else { 179 | return 1; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /examples/Magic/BinaryLP/Processing sketch/binary_LP_disc/binary_LP_disc.pde: -------------------------------------------------------------------------------- 1 | boolean[][] recordArray = new boolean[30][3]; 2 | int hypPointer; 3 | 4 | //w and h of page (A4 proportions) 5 | int w = 595; 6 | int h = 841; 7 | 8 | //have a line every "changeDegree" degree 9 | int changeDegree = 30; 10 | 11 | int strokeColor = 0; 12 | 13 | // button placement parameters 14 | int buttonPosition=300; 15 | int buttonWidth=100; 16 | int buttonHeight=50; 17 | int topLimit = -(buttonPosition-(buttonHeight/2)); 18 | int bottomLimit = -(buttonPosition+(buttonHeight/2)); 19 | int leftLimit = -buttonWidth/2; 20 | int rightLimit = buttonWidth/2; 21 | 22 | float Hypotenuse; 23 | float newX,newY; 24 | int rotateValue; 25 | 26 | int arrayCounter = 0; 27 | ArrayList recordData; 28 | 29 | 30 | 31 | boolean drawMark; 32 | boolean record = false; 33 | 34 | import processing.pdf.*; 35 | 36 | 37 | 38 | void setup(){ 39 | 40 | size(w,h); 41 | 42 | for(int i=0;i<30;i++){ 43 | for(int j=0;j<3;j++){ 44 | recordArray[i][j] = false; 45 | } 46 | } 47 | 48 | println(recordArray[1][1]); 49 | 50 | 51 | // Deletes existing record.pdf file, if any 52 | String fileName = sketchPath("record.pdf"); 53 | File file = sketchFile(fileName); 54 | 55 | // Checks if file exists 56 | if (file.exists()){ 57 | println("The file exists"); 58 | } 59 | else{ 60 | println("The file doesn't exist"); 61 | } 62 | // 63 | 64 | System.gc(); // the key to success 65 | 66 | boolean success = file.delete(); 67 | 68 | if (success) { 69 | println("The file was deleted"); 70 | } 71 | // 72 | 73 | recordData = new ArrayList(); 74 | 75 | } 76 | 77 | void draw() { 78 | 79 | background(255); 80 | 81 | // Calculates new coordinates based new origin 82 | newX = mouseX-w/2; 83 | newY = -(mouseY-h/2); 84 | // 85 | 86 | 87 | // Starts record if Save PDF button is selected 88 | if(record){ 89 | beginRecord(PDF, "record.pdf"); 90 | } 91 | // 92 | 93 | // Must place these here because beginRecord resets translate and mode to CORNER 94 | translate(w/2,h/2); // Moves origin to center of frame 95 | ellipseMode(CENTER); 96 | rectMode(CENTER); 97 | // 98 | 99 | redrawBackgroundImage(); // Draws the background circular record disc 100 | 101 | redrawButton(); // Draws the "Press to save as PDF" button 102 | 103 | calculateHypotenuse(); // Calculates the distance of the cursor from the translated center 104 | 105 | measureAngle(); 106 | 107 | drawMarkers(); 108 | 109 | // Stops record after one frame and exits 110 | if(record){ 111 | endRecord(); 112 | record=false; 113 | exit(); 114 | } 115 | // 116 | 117 | } // end draw() 118 | 119 | 120 | //********************************************************************************* 121 | // 122 | // Activates when mouse is clicked 123 | // 124 | // Checks if PDF save button is clicked or if marker is placed on record 125 | // 126 | //********************************************************************************* 127 | void mouseClicked() { 128 | if(newYbottomLimit && newX>= leftLimit && newX<=rightLimit){ 129 | record=true; 130 | } 131 | 132 | if(drawMark)recordArray[rotateValue/12][hypPointer] = !recordArray[rotateValue/12][hypPointer]; // Reverses current marking 133 | 134 | } // end mouseClicked() 135 | 136 | 137 | //********************************************************************************* 138 | // 139 | // Draws recordMarker matrix 140 | // 141 | // Checks matrix and places a mark if true 142 | // 143 | //********************************************************************************* 144 | void drawMarkers(){ 145 | fill(0); 146 | for(int i=0;i<30;i++){ 147 | if(recordArray[i][0]){ 148 | rotate(radians(i*12)); 149 | rect(68, 0, 24,7); 150 | rotate(-radians(i*12)); 151 | } 152 | if(recordArray[i][1]){ 153 | rotate(radians(i*12)); 154 | rect(104, 0, 24,13); 155 | rotate(-radians(i*12)); 156 | } 157 | if(recordArray[i][2]){ 158 | rotate(radians(i*12)); 159 | rect(141, 0, 24,18); 160 | rotate(-radians(i*12)); 161 | } 162 | } 163 | } 164 | 165 | 166 | //********************************************************************************* 167 | // 168 | // Measures angle of mouse to new center 169 | // 170 | // Calculates the angle and adjusts it to mathematic degree system 171 | // 172 | //********************************************************************************* 173 | void measureAngle(){ 174 | if (newX != 0){ 175 | if(newX>0 && newY>=0){ 176 | rotateValue = 360-round(degrees(atan(newY/newX))/12)*12; 177 | } 178 | if(newX<0 && newY>0){ 179 | rotateValue = 180 + -round(degrees(atan(newY/newX))/12)*12; 180 | } 181 | if(newX<0 && newY<0){ 182 | rotateValue = 180-round(degrees(atan(newY/newX))/12)*12; 183 | } 184 | if(newX>0 && newY<0){ 185 | rotateValue = -round(degrees(atan(newY/newX))/12)*12; 186 | } 187 | } 188 | else{ 189 | if (newY>0){ 190 | rotateValue = 270; 191 | } 192 | else{ 193 | rotateValue = 90; 194 | } 195 | } 196 | if(rotateValue==360)rotateValue=0; 197 | } 198 | 199 | 200 | 201 | //********************************************************************************* 202 | // 203 | // Calculates the distance of the cursor from the translated center 204 | // 205 | // Calculates the distance from the center and adjusts so that the box will only draw 206 | // within the white rings 207 | // 208 | //********************************************************************************* 209 | void calculateHypotenuse(){ 210 | 211 | Hypotenuse=sqrt(sq(newX)+sq(newY)); // Calculates "hypotenuse"/radius from XY location 212 | 213 | if (Hypotenuse>306/2 || Hypotenuse<113/2){ 214 | drawMark=false; 215 | } 216 | else{ 217 | drawMark=true; 218 | } 219 | 220 | if(Hypotenuse>=258/2){ 221 | Hypotenuse=306/2; 222 | } 223 | else{ 224 | if(Hypotenuse>=186/2){ 225 | Hypotenuse=233/2; 226 | } 227 | else{ 228 | if(Hypotenuse>=113/2||Hypotenuse<113/2){ 229 | Hypotenuse=161/2; 230 | } 231 | } 232 | } 233 | Hypotenuse=Hypotenuse-12; 234 | 235 | if (Hypotenuse == 141)hypPointer=2; 236 | if (Hypotenuse == 104)hypPointer=1; 237 | if (Hypotenuse == 68)hypPointer=0; 238 | 239 | } // end calculateHypotenuse() 240 | 241 | 242 | 243 | //********************************************************************************* 244 | // 245 | // Draws the "Press to save as PDF" button 246 | // 247 | // Draws the button and changes the color with hover over 248 | // 249 | //********************************************************************************* 250 | void redrawButton(){ 251 | 252 | // Calcuates if mouse is hovering over button and changes the fill accordingly 253 | if(newYbottomLimit && newX>= leftLimit && newX<=rightLimit){ 254 | fill(0); 255 | } 256 | else{ 257 | fill(100); 258 | } 259 | // 260 | 261 | rect(0,300,buttonWidth,buttonHeight); // Draws rectangular button 262 | 263 | // Draws the text on the button 264 | fill(255); 265 | textAlign(CENTER,CENTER); 266 | text("Press here",0,283); 267 | text("to save as",0,298); 268 | text("PDF file",0,313); 269 | // 270 | 271 | } //end redrawButton() 272 | 273 | 274 | 275 | //********************************************************************************* 276 | // 277 | // Draws the background circular record disc 278 | // 279 | // Draws the gray and white circles successively to produce ring pattern 280 | // 281 | //********************************************************************************* 282 | 283 | void redrawBackgroundImage() { 284 | 285 | noStroke(); 286 | 287 | fill(200); 288 | ellipse(0, 0, 354, 354); 289 | 290 | fill(255); 291 | ellipse(0, 0, 306, 306); 292 | 293 | fill(200); 294 | ellipse(0, 0, 258, 258); 295 | fill(255); 296 | ellipse(0, 0, 233, 233); 297 | 298 | fill(200); 299 | ellipse(0, 0, 186, 186); 300 | 301 | fill(255); 302 | ellipse(0, 0, 161, 161); 303 | 304 | fill(200); 305 | ellipse(0, 0, 113, 113); 306 | 307 | stroke(200); 308 | for(int i=0;i<30;i++){ 309 | rotate(radians(i*12)); 310 | line(50,0,175,0); 311 | rotate(-radians(i*12)); 312 | } 313 | 314 | stroke(strokeColor); 315 | strokeWeight(1); 316 | ellipse(0, 0, 101, 101); 317 | 318 | 319 | 320 | for(int j=360;j>=changeDegree;j=j-changeDegree){ 321 | rotate(radians(changeDegree)); 322 | line(0,0,50,0); 323 | } 324 | noStroke(); 325 | fill(255); 326 | ellipse(0, 0, 74, 74); 327 | 328 | } //end redrawBackgroundImage() 329 | 330 | class recordMarkers{ 331 | float markerAngle; 332 | float markerHypotenuse; 333 | } 334 | --------------------------------------------------------------------------------