Once again, Do you remember how to read sensors
-digital sensor (PushButton)
-analog sensor (photoCell)
Challenge:
Connect push button or photocell to your Arduino and check the input on SerialMonitor.
——code———
int photocellValue=0;
int buttonValue=0;
int buttonPin=5;
int photocellPin=A4;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// specify the pin mode, is it input or output?
pinMode(buttonPin,INPUT);
pinMode(photocellPin,INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on photocellpin:
photocellValue = analogRead(photocellPin);
Serial.print(“photocell: “);
Serial.print(photocellValue);
// read the input on buttonPin:
buttonValue= digitalRead(buttonPin);
// print out the value you read:
Serial.print(” button: “);
Serial.println(buttonValue);
delay(100); // delay in between reads for stability
}
——————-
More analog sensors
Resistive
- flex sensor
- soft pot
- temperature
Soft pot Round
Soft Pot straight
Flex (bend) sensor
Temperature sensor
Analog output
- proximate
- Gyro
- 3 Axis Accelerometer
3 Axis Accelerometer
More digital sensors
- motion sensor
- tilt switch
- Hall effect sensor
Motion sensor
Tilt Switch
TONE library
Now, we can connect small speaker and use TONE library to make music!
Well, it does not make a great sound, but it is a good start.
Open tonePitchFollower from Examples/Digital/tonePitchFollower
This sketch will “play a pitch that changes based on a changing analog input”
Let’s first play sound with potentiometer
Here is how you to connect:
What we cover:
tone()
Challenge:
Can you use other kinds of analog sensors to control the pitch of the sound produced?
Challenge:
Add Servo motor to make a strange instrument that plays music and moves at the same time
Challenge:
Can we use digital sensors to play sound like keyboard?