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...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
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...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration. One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv Know that the loadAndConvert...
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...
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT