#include <Servo.h>Servo myservo;int servo = 7;int piezo = 8;int pressure = A0;int servo_pos = 0;int current_pressure = 0;int current_note = 200;int current_spin = 1;void setup() {// put your setup code here, to run once:pinMode(servo, OUTPUT);pinMode(piezo, OUTPUT);pinMode(pressure, INPUT);myservo.attach(servo);Serial.begin(9600);}void loop() {// put your main code here, to run repeatedly:current_pressure = analogRead(pressure);Serial.print(current_pressure);Serial.print(” “);current_note = map(current_pressure, 0, 1024, 200, 2000);Serial.print(current_note);Serial.print(” “);current_spin = map(current_pressure, 0, 1024, 1, 20);Serial.println(current_spin);tone(piezo, current_note);myservo.write(servo_pos);servo_pos += 1*current_spin;}
Category Archives: Week 4
homework for week 4: Little Piano
This is a little piano with 3 keys — red stands for C, green stands for E, and blue stands for G. Control the knock strength to change the key.
int sensorPin = 0;
int ledPinC = 2;
int ledPinE = 3;
int ledPinG = 4;
int threshold = 100;
int keyboard;
int speaker = 12;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinE, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop() {
int val = analogRead(sensorPin);
if (val >= threshold) {
sensorValue = map(val, 180, 400, 100, 500);
if(sensorValue >= 100 && sensorValue < 200) {
keyboard = 131;
tone(speaker, keyboard);
digitalWrite(ledPinC, HIGH);
delay(100);
digitalWrite(ledPinC, LOW);
noTone(speaker);
}
else if(sensorValue >= 200 && sensorValue < 300) {
keyboard = 330;
tone(speaker, keyboard);
digitalWrite(ledPinE, HIGH);
delay(100);
digitalWrite(ledPinE, LOW);
noTone(speaker);
}
else if(sensorValue >= 300 && sensorValue < 400) {
keyboard = 784;
tone(speaker, keyboard);
digitalWrite(ledPinG, HIGH);
delay(100);
digitalWrite(ledPinG, LOW);
noTone(speaker);
}
Serial.println(keyboard);
}
}
week 4 – No, he doesn’t want to be touched
Hi everyone.
for this homework I made a toy that doesn’t want to be touched. This is the early stage of a more complex idea where a toy can give you remote information from your house.
Since to do that require more time and knowledge, for now I connected a Sharp IR sensor to detect proximity connected with an LED and a speaker. at certain distance the puppet start complaining.
I found my father playing with him this morning in the kitchen.
// Uses a PIR sensor to detect movement, a speaker and an LED
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(0);
if (pirState == LOW) {
// we have just turned on
Serial.println(“Motion detected!”);
// We only want to print on the output change, not state
pirState = HIGH;
Serial.println(val);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println(“Motion ended!”);
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
BoxHead
I used a flex sensor to change tones coming out of the piezo. I used an additional potentiometer in the circuit to gain access to more variety/manipulation of tones.
CODE:
#include “pitches.h”
int speaker = 12;
int sensor = A0;
int sensorValue = 0;
void setup(){
Serial.begin(9600); //baud rate
pinMode(sensor, INPUT);
pinMode(speaker, OUTPUT);
}
void loop(){
sensorValue = analogRead(sensor);
Serial.println(sensorValue);
sensorValue = map(sensorValue, 1, 35, 31, 4978);
Serial.println(sensorValue);
tone(speaker, sensorValue);
delay(1000/8);
noTone(speaker);
}
homework_week5
It’s instrument box. When you move it, tilt it or press its sensor, the sound and RGB LED will change.
int piezo = 9;
int speaker = 9;
int sensor = A0;
int sensorValue = 0;
int sensorValue2 = 0;
int ledPin1 = 6;
int ledPin2 = 3;
int ledPin3 = 5;
int sensorPress = A5;
void setup() {
Serial.begin(9600);
pinMode(sensor, INPUT);
pinMode(sensorPress, INPUT);
pinMode(speaker,OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensor);
Serial.println(sensorValue);
sensorValue2 = analogRead(sensorPress);
// Serial.println(sensorValue2);
analogWrite(ledPin1, sensorValue);
analogWrite(ledPin3, sensorValue2/2 + 50);
analogWrite(ledPin2, sensorValue2/2);
sensorValue = map(sensorValue,1,500,1000,4000);
tone(speaker,sensorValue);
// Serial.println(sensorValue);
}
HW3_FRITZING_YU_ZHANG
Fritzing
Homework 3
HAOTIAN_HOMEWORK
LUOBIN_HOMEWORK
is is the Fritzing sketch for my first project. I don’t know if I’m doing right because, in order to make a better looking schematic map, I have to check it every time I insert something to the breadboard. Otherwise the map will be messed up…
BREADBOARD______________________________________________
PCB______________________________________________
SCHEMATIC______________________________________________