need help with Arduino sketch. and \"tools/Serial port\") 3. Start a new sketch
ID: 3678509 • Letter: N
Question
need help with Arduino sketch.
and "tools/Serial port") 3. Start a new sketch and write codes to accomplish the following task: Give an array of 5 arbitrarily float numbers (e.g.. 70.11, -42, 1.0, 5.3, 1465). Use "for" loop and "if" statements to find the max and the min numbers. Type '0' in Serial Monitor to display the min value in Serial Monitor, and type '9' to display the max number, Display a reminder message to user if any other number or character is being typed, such as "Unexpected input! Please type a number of 0 or 9: " Hint: 1. Define a float array to store the 5 numbers: float myNumbers = { … }; 2. Define two float variables, such as "myMaxNum" and "myMinNum" to temporarily store the larger and the smaller numbers when compare any two numbers. You can first temporarily assume the first number is myMaxNum and myMinNum, then later compare it with the rest number to update myMaxNum or myMinNum when needed. MyMaxNum myNumberslOl: myMinNum-myNumbers(01 for lint i-1; iExplanation / Answer
int length = 5;
//declared array
float myNumbers[length] = {70.11, -42, 1.0, -5.3, 1465};
void setup(){
Serial.begin(9600);
//printing array
Serial.println("Array contains:");
for (int i=0; i<length; i++){
Serial.print(myNumbers[i]);
Serial.print(", ");
}
float MyMaxNum = myNumbers[0];
float MyMinNum = myNumbers[0];
//for loop to getmin and max
for(int i=0;i<length;i++){
if(myNumbers[i] > MyMaxNum){
MyMaxNum = myNumbers[i];
}
if(myNumbers[i] < MyMionNum){
MyMinNum = myNumbers[i];
}
}
if(Serial.available()){ //checking any number types in serial monitor
char UserInput = Serial.read(); //read typed number from serial monitor
if(UserInput == '0' ||UserInput == '9' ){
if(UserInput == '0'){
Serial.println(MyMinNum);
}
else{
Serial.println(MyMaxNum);
}
}
else{ //handling wrong input
Serial.println("Unexpected input! Please type a number of 0 or 9 only!");
Serial.println(UserInput);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.