// pins 3, 5, 9, 11, and any pin with ~ beside it, are all capable of PWM
int led1Pin = 3,
led2Pin = 5,
led3Pin = 9,
led4Pin = 11;
void setup() {
// put your setup code here, to run once:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
}
void loop() {
/* put your main code here, to run repeatedly:
* PWM --> using digital signal to control analog signal (digital -> analog)
* map(value, fromLow, fromHigh, toLow, toHigh).
* This function is used to remap a value,
* which will return a new value whose percentage in the range of
* toLow-toHigh is equal to the percentage of "value"
* in the range of fromLow-fromHigh.
* fromLow - fromHigh is the duty cycle --> ratio of pulse width to period.
* voltage of analog signal also corresponds to duty cycle
* toLow - toHigh is the range --> (0, 255)
*/
analogWrite(led1Pin, map(2, 0, 100, 0, 255));
analogWrite(led2Pin, map(10, 0, 100, 0, 255));
analogWrite(led3Pin, map(50, 0, 100, 0, 255));
analogWrite(led4Pin, map(100, 0, 100, 0, 255));
}