Question

In: Computer Science

Make an Arduino device that detects light levels and causes a change in pitch on a...

Make an Arduino device that detects light levels and causes a change in pitch on a piezo depending on the proximity of the light to the sensor.

I'm struggling to produce the code that is needed. Here is what I have but it isn't right at all and isn't working...

const int dark = 340; //set dark parameters
const int sound = 60; //set noise to play

void setup() {
pinMode(3, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600); }
  
void loop() {
int light = analogRead(A0);
  
if (light < dark) {
Serial.print(light);
Serial.println(" It's dark");
}
else {
Serial.print(light);
}
  
Serial.println(" It's light");
tone(3, sound, 10);

}

Solutions

Expert Solution

Make an Arduino device that detects light levels and causes a change in pitch on a piezo depending on the proximity of the light to the sensor.

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3

int sound = 250;


void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(buzzer, OUTPUT);

}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;


if (distance <= 30) {
    digitalWrite(led, HIGH);
    sound = 250;
}
else {
    digitalWrite(led,LOW);
}
if (distance < 25) {
      digitalWrite(led2, HIGH);
      sound = 260;
}
else {
      digitalWrite(led2, LOW);
}
if (distance < 20) {
      digitalWrite(led3, HIGH);
      sound = 270;
}
else {
    digitalWrite(led3, LOW);
}
if (distance < 15) {
    digitalWrite(led4, HIGH);
    sound = 280;
}
else {
    digitalWrite(led4,LOW);
}
if (distance < 10) {
    digitalWrite(led5, HIGH);
    sound = 290;
}
else {
    digitalWrite(led5,LOW);
}
if (distance < 5) {
    digitalWrite(led6, HIGH);
    sound = 300;
}
else {
    digitalWrite(led6,LOW);
}

if (distance > 30 || distance <= 0){
    Serial.println("Out of range");
    noTone(buzzer);
}
else {
    Serial.print(distance);
    Serial.println(" cm");
    tone(buzzer, sound);

}
delay(500);
}


Related Solutions

Write an Arduino Program that detects a reflective surface using the line sensor array of a...
Write an Arduino Program that detects a reflective surface using the line sensor array of a QTR-3RC (*Must be specific to this sensor! please this is important*) Must include a function that when called returns.. 0 (line to the left of the robot) 1 (line under the robot on the left side) 2 (line under the robot on the right side) 3 (line to the right of the robot) Output the results to the serial port in 0.5 second intervals...
Write an Arduino Program that detects a reflective surface using the line sensor array of a...
Write an Arduino Program that detects a reflective surface using the line sensor array of a QTR-3RC (*Must be specific to this sensor! please this is important*) Must include a function that when called returns.. 0 (line to the left of the robot) 1 (line under the robot on the left side) 2 (line under the robot on the right side) 3 (line to the right of the robot) Output the results to the serial port in 0.5 second intervals...
design a sequence detector that detects the sequence: 110. The device has one input x and...
design a sequence detector that detects the sequence: 110. The device has one input x and one output Y. When the input sequence is set to 1 followed by a 1 followed by a 0, then Y is set to 1 otherwise Y is set to 0. Use J-K flip-flops and minimum number of states is designing this detector and show the followings: a) Show the state diagram b)Show the state table for this detector
A life saving device that detects silent heart attacks by Akash manoj Summarize and write what...
A life saving device that detects silent heart attacks by Akash manoj Summarize and write what did you learn
INTERNET OF THINGS (IOT) 25. Some argue that Arduino is preferable low power IoT device to...
INTERNET OF THINGS (IOT) 25. Some argue that Arduino is preferable low power IoT device to Raspberry Pi for small scale home projects. Would you agree or disagree? Differentiate between them based on at least three features (i.e. that makes one preferable than the other for a small scale home project.)
I need an Arduino uno code to measure high low levels with hcsr04 of a tank...
I need an Arduino uno code to measure high low levels with hcsr04 of a tank and display it on i2c 20x4 lcd.
Which of the following are causes of evolutionary change?
Part A Which of the following are causes of evolutionary change? Select all that apply. genetic drift natural selection gene flow mutation  Part B Generation-to-generation change in the allele frequencies in a population is ______. mutation natural selection microevolution genetic drift macroevolution
We want to make a machine that detects an alternating pattern. It should indicate whether three...
We want to make a machine that detects an alternating pattern. It should indicate whether three or more alternating bits have been observed on our serial input X. So, if X has 010 or 101, the output Z will be high, otherwise the output will be low. Assume that when the machine starts up or is reset, is has not seen any bits of data to work with and the output cannot go high until it sees the third bit...
There is something wrong with my arduino lab. I want my four leds to light up...
There is something wrong with my arduino lab. I want my four leds to light up one after the other. Then the speaker plays music, and 4 leds change with music. But my code only makes one of them work. Please help me modify my code const int switchPin = 8; unsigned long previousTime = 0; int switchState = 0; int prevSwitchState = 0; int led = 2; // 600000 = 10 minutes in milliseconds long interval = 1000; int...
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red,...
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red, blue, and green. Use ‘analogWrite’ command inside the 'for loop.' Use the value ranges from 0-255 for this command. So, to activate any of the pins, the value should be 255. To turn off 0. pattern: First for loop Red light – delay – Second for loop for blue light – delay - Third for loop for green light - delay. (The resulting lights...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT