This small project is part 2 of 3. The first part see: https://create.arduino.cc/projecthub/MichDragstar/ultrasonic-sensor-with-alarm-lcd-and-temperature-a5dbab?ref=user&ref_id=410358&offset=0
In this part there are 6 chasing LEDS mounted in a circle to indicate the waterpump is pumping. In the real project the push button is replaced by a relay contact witch is activated by a magnet contact (Waterlevel high). The speed of the chasing LEDS can be adjust with the POT. I made this project first on the UNO, in the real project I use the TRINKET PRO 5V 16MHz. The OUTPUTS and the CODE does not change for the UNO or TRINKET.
byte ledPin[] = {8, 9, 10, 11, 12, 13}; // Create array for LED pins
int ledDelay; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2; // select the input pin for the potentiometer
int buttonPin = 4;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
for (int x=0; x<6; x++) { // set all pins to output
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop()
{
buttonState = digitalRead(buttonPin);
ledDelay = analogRead(potPin); // read the value from the pot
if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED() {
for (int x=0; x<6; x++ ) { // turn off all LED's
digitalWrite(ledPin[x], LOW);
}
if (buttonState == HIGH) {
digitalWrite(ledPin[currentLED], HIGH);} // turn on the current LED
else digitalWrite (ledPin[currentLED], LOW);
currentLED += direction; // increment by the direction value
if (currentLED == 6){currentLED = 0;
}
}
Comments