Skip to content

Instantly share code, notes, and snippets.

@shfitz
Last active November 10, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shfitz/6c5b89bf6dcea613f43043c56a406aa1 to your computer and use it in GitHub Desktop.
Save shfitz/6c5b89bf6dcea613f43043c56a406aa1 to your computer and use it in GitHub Desktop.
// 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