homework_week5

It’s instrument box. When you move it, tilt it or press its sensor, the sound and RGB LED will change.

IMG_1195 IMG_1197


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);

}

Leave a Reply

Your email address will not be published.