//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 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; delay(50); // Serial.println("SWITCH"); // debug } if(switchMem == true && digitalRead(!switchPin)){ analogWrite(ledPin, brightness); delay(50); // Serial.println("ON"); //debug } else if(switchMem == false || switchMem == false && digitalRead(switchPin)){ analogWrite(ledPin, LOW); reportValue = 0; delay(50); // Serial.println("OFF"); //debug } /* debug if (digitalRead(switchPin)){ //triggers when the button is pressed and held down switchMem = true; analogWrite(ledPin, 0); delay(10); } else if(!digitalRead(switchPin) & switchMem){ //only triggers everytime the button is released, won't update the brightness if button isn't released again analogWrite(ledPin, brightness); switchMem = false; delay(10); Serial.println("TWO"); } */ } void serialEvent(){ String userInput = Serial.readString(); finalString = "The led is at " + String(reportValue) + "%"; if(userInput == Word){ Serial.println(finalString); } }