Question

In: Computer Science

Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN...

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

Solutions

Expert Solution

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

Related Solutions

code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature to Fahrenheit temperature. The formula for converting Celsius temperature into Fahrenheit temperature is:    F = (9 / 5) * C + 32 Create integer constants for the 3 numbers in the formula (9, 5, and 32).  Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. Convert the 9 to a double when converting the temperature (use type casting). Have a...
1. in the code below, 2 variables (largest and smallest) are declared. use these variables to...
1. in the code below, 2 variables (largest and smallest) are declared. use these variables to store the largest and smallest of three integer values. you must decide what other variables you will need and initialize them if appropriate. 2. write the rest of the program using assignment statements, if statements, or if else statements as appropriate. There are comments in the code that tell you where you should write your statements 3. compile and execute. Output should be: The...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Please change this code to follow the rules. The program must not use global variables. In...
Please change this code to follow the rules. The program must not use global variables. In another words, it must use local variables and pass-by-value or pass-by-reference parameters. #include <iostream> #include <string> #include <algorithm> using namespace std; struct expense { string Desc; double exp; }; expense arr[100]; int c = 0; void menu(); //1. show all void showArray(){ if (c>0){ for(int i=0;i<c;i++){ cout<<"AMOUNT("<<arr[i].exp<<") DESC"<<arr[i].Desc<<")"<<endl; } }else{ cout<<"There is no expense entry available"; } menu(); } //2. spend void addArray(){ string...
use repil.it intro to C-programin be sure Make code very basic and add good comments thank...
use repil.it intro to C-programin be sure Make code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input in each spot in the array. 2-Add a function to the program, called get_mean that determines the mean, which is the average of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT