//Partial Solution to Arduino exercise //A program allows you to manipulate the brightness of an led by turning a knob hooked up the the arduino, it also has a button that turns the led completely on or off. //Additionally, users can enter the string "report" into the serial monito and have the program report back to the the precentage of brightness that the led is at. //Variables for ours pins byte ledPin = 9; //identify the pin for the led byte switchPin = 8; //identify the pin for the pushbutton void setup(){ Serial.begin(9600); //starts the Serial pinMode(switchPin, INPUT); //declares our button/switch as an Input pinMode(ledPin, OUTPUT); //declares our led as an output } //Declare variables int sensorValue; //Stores the value from the potentiometer, values from 0-1023 float voltage; //Converts the sensorValue into voltage, values from 0-5.0 float brightness; //Converts our voltage into values the led can handle, values from 0-255 int reportValue; //The value we will report to our user, values from 0-100 boolean switchMem = false; //set the initial switch memory to false boolean ledState = false; //false for off, true for on String finalString; String Word = "report";//used for our serialEvent void loop(){ //Assign values to the variables we declared earlier. sensorValue = analogRead(A0); voltage = sensorValue * (5.0 / 1023.0); brightness = voltage * 51.0; reportValue = voltage * 20; //Starts our switchPin functionality if (digitalRead(switchPin) && !switchMem){ switchMem = true; // Serial.println("SWITCH"); // debug } else if(switchMem && !digitalRead(switchPin)) { if (ledState == false) { ledState = true; } else if (ledState == true) { analogWrite(ledPin, LOW); ledState = false; } switchMem = false; delay(10); } if (ledState == true) analogWrite(ledPin, brightness); } void serialEvent(){ String userInput = Serial.readString(); finalString = "The led is at " + String(reportValue) + "%"; if(userInput == Word){ Serial.println(finalString); } }