const int led1 = 3;
const int led2 = 2;
const int button = 12;
const int dim = A0;
void setup() {
// put your setup code here, to run once:
pinMode(dim, INPUT);
pinMode(button,INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//----------PWM---------------------//
int dim_value= analogRead(dim);
int brightness = map(dim_value,0, 1023, 0,255); // map takes 5 things , 1- value 2- old range 3- new range because analogue is 1024 value but pwm is 255 as max
if (digitalRead(button)== HIGH)
{
if (brightness <= 1)
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Serial.println(brightness);
}
else if (brightness > 1)
{
digitalWrite(led1, brightness);
digitalWrite(led2, brightness);
Serial.println(brightness);
}
}
else
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Serial.println("button is off");
}
// else if (brightness == 0)
//{
// digitalWrite(led, LOW);
//}
}