Jaycar-Electronics / Duinotech-Starter-Kit-For-Arduino

Arduino Start Kit Projects
GNU General Public License v3.0
29 stars 13 forks source link

Fix for Potentiometer code #8

Open Jenjustjen opened 4 years ago

Jenjustjen commented 4 years ago

The Code for the potentiometerr is incorrect - to make this project work using the wiring layout provided and adjust an internal LED you should use LED_BUILTIN as the output.

I have found that this only give me an on/off state when using the potentiometer knob though, to see dimming you need to adjust so you have an led on the breadboard.

`int potPin = A0; // Input pin from the potentiometer int ledPin = LED_BUILTIN; // Output pin to the LED int potVal = 0; // A variable to store the potentiometer value int brightness = 0;

void setup() // Runs once when sketch start { pinMode(ledPin, OUTPUT); // Setting the LED pin as an output }

void loop() // Runs repeatedly { //read from the pot pin, and store it into potval potVal = analogRead(potPin);

//"map" the brightness, so that potval (0->1023) // corresponds with a value between (0->255)

brightness = map(potVal , 0, 1023, 0, 255);

//analog write uses PWM to control the brightness of the LED. // it will only work on the PWM pins, which pin 9 is. analogWrite(ledPin, brightness); }`

PeterJRiches commented 8 months ago

This is @Jenjustjen's code, as above, but with syntax highlighting.

int potPin = A0; // Input pin from the potentiometer
int ledPin = LED_BUILTIN; // Output pin to the LED
int potVal = 0; // A variable to store the potentiometer value
int brightness = 0;

void setup() // Runs once when sketch start
{
pinMode(ledPin, OUTPUT); // Setting the LED pin as an output
}

void loop() // Runs repeatedly
{
//read from the pot pin, and store it into potval
potVal = analogRead(potPin);

//"map" the brightness, so that potval (0->1023)
// corresponds with a value between (0->255)

brightness = map(potVal , 0, 1023, 0, 255);

//analog write uses PWM to control the brightness of the LED.
// it will only work on the PWM pins, which pin 9 is.
analogWrite(ledPin, brightness);
}