openobject.org

S3198413 Final Arduino Sketch

From Physical Programming

This is the sketch that i used in my final project. What it does is that as the flex sensor is bent the value changes and the speaker plays a different note plays. This is because each note has a different value so as the sensor reaches that value it plays it.


//this is the library selection (it is an external library)//
#include <Tone.h>

//this is selecting what pins the flex sensors using//
int flexPin1 = 1;
int flexPin2 = 2;

//this identifys that the tone is to be sent to a speaker and names the speaker further use in the sketch//
Tone speakerOne;
Tone speakerTwo;

//Here the notes are chosen for each sensor making sure to chose notes that are in the right number value//
int notesOne[] = { NOTE_A3, NOTE_C3, NOTE_G3, NOTE_D3, NOTE_B3 };
int notesTwo[] = { NOTE_F3, NOTE_C4, NOTE_E4, NOTE_D3, NOTE_D4 };

void setup(){
  
 //This part of the sketch is telling the arduino that each speaker will playing from pin 11 and 10//
 Serial.begin(9600);
 speakerOne.begin(11);
 speakerTwo.begin(10);
}

void loop(){
 int flexRead1 = analogRead(flexPin1);
 int flexRead2 = analogRead(flexPin2);

//This is mapping the values that the flex sensor reads off and the number of notes that it will play//
 int toneValue1 = map(flexRead1, 140, 346, 0, 6);
 int toneValue2 = map(flexRead2, 125, 297, 0, 6);
 
 //This was used to read the serial numbers being created by the sensors so that they could be mapped//
 Serial.print("Flex 1: ");
 Serial.println(flexRead1);
 Serial.print("Flex 2: ");
 Serial.println(flexRead2);
 delay(30);
 
 //This tells the sketch that speakerOne will play notesOne by reading the values in toneValue1//
 speakerOne.play(notesOne[toneValue1]);
 speakerTwo.play(notesTwo[toneValue2]);
}