int led1 = D2;
int sensor = A0;
int tiltSwitch = D3;
void setup() {
pinMode(led1, OUTPUT);
pinMode(tiltSwitch, INPUT);
Serial.begin(9600);
}
void loop() {
int switchRead = digitalRead(tiltSwitch);
int val = analogRead(sensor);
int brightness = map(val, 0, 4095, 0, 255);
if (switchRead == HIGH){
analogWrite(led1, brightness);
Serial.print(brightness);
}
else {
digitalWrite(led1, LOW);
}
}
Click to Expand