Learning Goal: I’m working on a computer science project and need a sample draft to help me learn.
This LAB relates to Module 5 and has three parts:
1- You follow section 3.1 of your Arduino workshop and set up your breadboard and control the Toggle switch using if-statement. (50 points)
2- Follow section 3.2 of your Arduino workshop, set up your breadboard, and write your code to change the structure of your code using While-loop. (50 points)
3- For the last part follow section 3.3 and use for-loop to control the flow of your program for your Arduino, using the Serial Monitor to display the data. (50 points)
In Module 5 learned about:
In this Module you will learn about:
By the end of this chapter, you will have created your very own calculator using different functions, loops, and Serial functions.
(447) Arduino Workshop – Chapter Three – Overview – YouTubeLinks to an external site.
In this section, we’ll dive into using ‘switch’ cases to implement comparative lists and outcomes.
(447) Arduino Workshop – Chapter Three – SWITCH CASE Statements – YouTubeLinks to an external site.
Source Code for ‘Potentiometer Switch Case’
// pin definitions int potPin = A0; // declare global variables int lastPotValue; void setup() { // set pin modes pinMode(potPin, INPUT); //initialise Serial port Serial.begin(9600); } void loop() { // read potPin and divide by 255 to give 5 possible readings int potValue = analogRead(potPin) / 255; // if something has changed since last value if(potValue != lastPotValue) { // enter switch case switch(potValue) { case 0: Serial.println("Very Low"); break; case 1: Serial.println("Low"); break; case 2: Serial.println("Moderate"); break; case 3: Serial.println("High"); break; case 4: Serial.println("Extreme"); break; default: Serial.println("error!"); break; } lastPotValue = potValue; } }
Wiring Diagram for ‘Potentiometer Switch Case’
In this section, we’ll look at how to use different mathematic functions and methods in Arduino.
(447) Arduino Workshop – Chapter Three – SWITCH CASE Statements – YouTubeLinks to an external site.
For example for ‘Maths Functions’
x = y + 3; x = y - 7; x = y * 6; x = y / 4;
In this section, we’ll be learning about using and creating your own functions to write modular, reusable code.
(447) Arduino Workshop – Chapter Three – Creating Functions – YouTubeLinks to an external site.
Source Code for ‘Hypotenuse Calculator’
*Update: I’ve updated the sketch for the hypotenuse calculator which is reflected in the code below. It provides rejection for non-integer values and some extra feedback.
void setup() { //initialise Serial port Serial.begin(9600); } void loop() { int a; int b; float result; //print instructions, and wait until there is something in the serial buffer Serial.print("Enter a side value: "); while(!Serial.available()); a = readSerial(); if(a == 0) { return; } Serial.print("Enter the other side value: "); while(!Serial.available()); b = readSerial(); if(b == 0) { return; } findSide(a,b); Serial.println(); } //readSerial takes the next integer in the Serial buffer, clears the buffer, then returns it int readSerial() { int i = Serial.parseInt(); //checks if the received value is a valid integer if(i < 1 || (i%1 != 0)) { Serial.println("That isn't a valid integer"); return 0; } Serial.println(i); Serial.parseInt(); return i; } void findSide(int x, int y) { //calculate C squared by A squared + B squared float hypotenuse = sqrt(x*x + y*y); //print out the result Serial.print("Hypotenuse = "); Serial.println(hypotenuse); }
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments