Fading leds controlled with tilt sensor

Hi! This is totally a beginner question because this is my first time using something like Arduino.
I'm trying to control 2 LEDs simultaneously and have them slowly light up and turn off when the tilt state changes. I got the code for controlling the LEDs with the tilt switch, though I'm not so sure how to write the fade in/out function within this code. Everything I've tried so far only gives me 2 blinking LEDs.

int ledPin = 12; 
int ledPin2 = 11;
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH; // the previous reading from the tilt sensor

long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup(){
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}

void loop(){
sensorValue = digitalRead(sensorPin);
// If the switch changed
if (sensorValue == lastTiltState) {
// reset the debouncing timer
lastDebounceTime = millis();
} 
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
lastTiltState = sensorValue;
}
digitalWrite(ledPin, lastTiltState);
digitalWrite(ledPin2, lastTiltState);

Serial.println(sensorValue);
delay(500);
}

PWM (analogWrite) your ledPins in relation to the tilt value/s.

I think there is a fade example with IDE using PwM as per previous poster

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.