Question

In: Computer Science

ARDUINO Create a function that will ask the user to enter the values for each of...

ARDUINO

Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.

Solutions

Expert Solution

Please up vote .comment if any query . Thanks for question . Be safe .

Note : check attached image for output .code compiled and tested in Arduino IDE.

Program :

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //uart starts at baudrate 9600
delay(100); //delay to settle uart

getColorCode(); //function to get color code

}

void loop() {


}


void getColorCode()
{

int red=-1,green=-1,blue=-1; //assign -1 to color code integer

while(red>255 || red<0) //if red <0 or >255 keep asking for color
{
    red=serialData("red"); //function takes code for red color and return integer value of code
}

while(green>255 || green<0) //keep asking till valid green color code
{
    green=serialData("green"); //pass color name to get value
}

while(blue>255 || blue<0) //blue color code
{
    blue=serialData("blue");
}

Serial.print("Red color code is : "); //print user input color code
Serial.println(red); //red color code

Serial.print("Green color code is : ");
Serial.println(green); //green color

Serial.print("Blue color code is : ");
Serial.println(blue); //blue color code

}


int serialData(String colorName) //argument a string color name
{
String color="";
Serial.println("Enter color code of "+colorName+" : "); //prompt for color code passed by user
//when data not available serial.available return -1 else number of data available on serial
while(!Serial.available()); //wait till did not input data on serial

while(Serial.available()) //read serial data
{
    color+=(char)Serial.read(); //read data from serial as char and add to color
}
Serial.println("Your entered code is : "+color); //print user input
return color.toInt(); //convert string to integer and return
}

Output :

Please comment if any changes , and up vote .


Related Solutions

Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function...
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function that will receive the word, use the word to compile a new word in Robbery language and return the new Robbery language word. In the function: Use a while loop and a newline character (‘\0’) to step through the characters in the original string. How to compile a word in Robbery language: Double each consonant and put the letter ‘o’ between the two consonants....
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
Create a console app c#. Using a List, ask the user to enter all of their...
Create a console app c#. Using a List, ask the user to enter all of their favorite things. Once they are done, randomly pick a value from the list and display it.
Q3) ​Write a main function to test your SortTriple will ask the user to enter the...
Q3) ​Write a main function to test your SortTriple will ask the user to enter the three values, then uses ​SortTriple ​to reorder the values if required. The main function should print the value in the correct order and a message to indicate if the values were in the correct order or not. ​[20 points] ● You are NOT allowed to use loops or arrays in your code​ ​[- 30 points] ● You are ​NOT​ allowed to use global variables....
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
Use a while(true) loop to ask the user the following 2 values “Enter a rate r...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r =” “Enter a nonnegative integer (enter negative integer to quit):” If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the geometric series, namely 1 + r + rˆ2 + rˆ3 + . . . +rˆn. Use...
Use a while(true) loop to ask the user the following 2 values “Enter a value x...
Use a while(true) loop to ask the user the following 2 values “Enter a value x “ “Enter a nonnegative integer n (enter negative integer to quit): “ If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the Riemann zeta series for the geometric series, namely 1 + 2ˆ-x + 3ˆ-x +...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in a list called myList. Compute and print out the sum and average of the items in the list. Print the numbers that are divisible by 2 from myList. b. Instead of using a list to store the numbers, create a loop to accept the numbers from the user, and find the average of all the numbers. Hint: use an accumulator to store the numbers...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT