Last active
November 10, 2020 19:43
-
-
Save shfitz/6c5b89bf6dcea613f43043c56a406aa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// constants for the pins | |
const int ledPin = 2; | |
const int potPin = A7; | |
void setup() { | |
// set the led pin as an output | |
pinMode(ledPin, OUTPUT); | |
// open the serial port for communication | |
Serial.begin(9600); | |
while(!Serial){ | |
;; // do nothing until the serial port opens | |
} | |
} | |
void loop() { | |
// read the value of the pin and store it | |
// this will be in a range of 0-1023 | |
int potVal = analogRead(potPin); | |
// print the value to the monitor | |
Serial.print("pot value: "); | |
Serial.print(potVal); | |
// map the value to the range of analogOut | |
// store that in a new variable | |
int ledVal = map(potVal, 0, 1023, 0, 255); | |
// print the value to the monitor | |
Serial.print(", mapped value: "); | |
Serial.println(ledVal); | |
// write the value to the LED | |
analogWrite(ledPin, ledVal); | |
// give the ADC a moment to rest | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment