In: Computer Science
Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN = 9; const int ECHO_PIN = 10; const int RED_PIN = 3; const int GREEN_PIN = 5; const int BLUE_PIN = 6; float duration, distance_in_cm, distance_in_feet; void setup() { //Setup pins for correct I/O pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); } void loop() { //Generate the ultrasonic waves digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); //Read in the echoed waves duration = pulseIn(ECHO_PIN, HIGH); //Convert from time to distance using the speed of sound distance_in_cm = (duration*.0343)/2; //TODO: Convert distance_in_cm to distance_in_feet //TODO: Replace distance_in_cm with distance_in_feet below //and update the comparison distances for appropriate social distancing if(distance_in_cm > 50){ //safe distance setColor(0, 255, 0); }else if(distance_in_cm > 25){ //getting a bit close now are we... setColor(255, 255, 0); }else if(distance_in_cm > 5){ //stay back! setColor(255, 0, 0); } } void setColor(int redValue, int greenValue, int blueValue){ analogWrite(RED_PIN, redValue); analogWrite(GREEN_PIN, greenValue); analogWrite(BLUE_PIN, blueValue); }
//Create variables to use later
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int RED_PIN = 3;
const int GREEN_PIN = 5;
const int BLUE_PIN = 6;
float duration, distance_in_cm, distance_in_feet;
void setup()
{
//Setup pins for correct I/O
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop()
{
//Generate the ultrasonic waves
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
//Read in the echoed waves
duration = pulseIn(ECHO_PIN, HIGH);
//Convert from time to distance using the speed of sound
distance_in_cm = (duration*.0343)/2;
//TODO(DONE in bellow): Convert distance_in_cm to distance_in_feet
distance_in_feet = distance_in_cm / 30.48;
//TODO(DONE in bellow): Replace distance_in_cm with distance_in_feet below //
//and update the comparison distances for appropriate social distancing
if(distance_in_feet > 50){ //safe distance
setColor(0, 255, 0);
}else if(distance_in_feet > 25){ //getting a bit close now are we...
setColor(255, 255, 0);
}else if(distance_in_feet > 5){ //stay back!
setColor(255, 0, 0);
}
}
void setColor(int redValue, int greenValue, int blueValue){
analogWrite(RED_PIN, redValue);
analogWrite(GREEN_PIN, greenValue);
analogWrite(BLUE_PIN, blueValue);
}