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 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...
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.
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...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT