Simulate level with a numbers of LED`s

Hi all ,

Im new here and i need your help. I have a project where i want to simulate the position of a blind by LEDs. I have a blind on my window and i want to know the position. I want to have 10 leds and an two inputs. If i close the input one the leds must turn on from 0 to 10 one by one in 10 seconds (one per second). If i release the input the LEDs must stay on that position. If i close the contact two the LEDs must do the reverse. I have a Arduino Uno Rev3 board. Please remind that i`m a noob but i want to learn and play with this kind of projects.

Thanks !

Welcome to the group!

Always show us what you have done so far.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Hello all,
I use this code and the LEDs are going on one by one when i press a button and going off if i press the second button. But is not like i want. I want if i press a button for 5 sec only 5 leds to turn it on and to stay on after the i release the button one. If i press the second button for two seconds i want to turn off only two LED`s and the rest to stay on. Can you help me with that ?

Thanks !

``const int buttonPin1 = 10; // the number of the pushbutton pin
const int buttonPin2 = 11;
const int ledPin1 = 9; // the number of the LED pin
const int ledPin2 = 8;
const int ledPin3 = 7;
const int ledPin4 = 6;
const int ledPin5 = 5;
const int ledPin6 = 4;
const int ledPin7 = 3;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin3, HIGH);
delay(500);
digitalWrite(ledPin4, HIGH);
delay(500);
digitalWrite(ledPin5, HIGH);
delay(500);
digitalWrite(ledPin6, HIGH);
delay(500);
digitalWrite(ledPin7, HIGH);
}

buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH) {
// turn LED off:
digitalWrite(ledPin7, LOW);
delay(500);
digitalWrite(ledPin6, LOW);
delay(500);
digitalWrite(ledPin5, LOW);
delay(500);
digitalWrite(ledPin4, LOW);
delay(500);
digitalWrite(ledPin3, LOW);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
}
}``

You said 10 LEDs but your code has 7, why?

You have delay(500); between each LED changing, why?

How are you wiring the LEDs and two switches?

Where do you control the blinds?

What happens if you press the switch for 4.5 seconds?

Rather than pressing for 5 seconds, have you considered 5 pushes (on the up switch) gives you 5 LEDs ON etc. ?

Step #1
Study this tutorial, make sure you understand what it teaches.

1.You said 10 LEDs but your code has 7, why?

I made tests. In the end will be 10 Led`s.

2.You have delay(500); between each LED changing, why?

In the end i need to cover 20 seconds (the blind working time) with 10 LED`s from first to the last.

3.How are you wiring the LEDs and two switches?

Everything is working right now but not like i want. I`m not so good to create a diagram, but if is very important i will do a diagram.

4.Where do you control the blinds?

I dont control a blind. I will take an information about position. Lets say i will simulate a blind with these 10 LED`s.

What happens if you press the switch for 4.5 seconds?

If i change the state of the button 1 all the LED`s sre going on from one to one (500ms delay). Is not important how long i press the button 1 will do the same. If i change the state of the button 2 will do the reverse from last LED to first.

but if is very important i will do a diagram.

Yes it is very important, without knowing what the hardware is you can not hope to get the software to do what you want.

Please read the how to use this forum sticky post it will tell you how to post code correctly here.

made tests. In the end will be 10 Led`s.

You know we love being miss-lead by incorrect information here, it makes us very happy.

I don`t control a blind. I will take an information about position.

How?

Let us work with this schematic:

Let's start with this partial sketch:

// Version 1

// M A C R O S
#define LEDon      HIGH
#define LEDoff     !LEDon

#define isPUSHED   LOW
#define isRELEASED !isPushed

// I N P U T S
//INPUT_PULLUP---pin---[N.O.switch]---GND
const byte upSwitch     = 12;
const byte downSwitch   = 13;

// O U T P U T S
//       RES        LED 
//pin---[220R]———[>|]------GND
const byte LEDs[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};

//14 = A0, a LED that toggles every 1/2 second
const byte heartBeatLED = 14;

// S R A M
//timing variables
unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long upMillis;
unsigned long downMillis;

//time between LED changes, 1/2 second
const unsigned long interval = 500ul;

//the last state the upSwitch was in
byte lastUpState;
//the last state the downSwitch was in
byte lastDownState;

//current LED we are working with
char ledCounter;

//if upFlag is true, upSwitch is pressed
boolean upFlag   = false;
//if downFlag is true, downSwitch is pressed
boolean downFlag = false;



//                           s e t u p ( )
//**********************************************************************
void setup()
{
  //***************************
  for (byte x = 0; x < sizeof(LEDs); x++)
  {
    pinMode(LEDs[x], OUTPUT);
  }

  //a LED to check if the code is blocking, toggles every 1/2 second
  pinMode(heartBeatLED, OUTPUT);

  pinMode(upSwitch, INPUT_PULLUP);
  lastUpState = digitalRead(upSwitch);

  pinMode(downSwitch, INPUT_PULLUP);
  lastDownState = digitalRead(downSwitch);

} //END of  setup()

//                           l o o p ( )
//**********************************************************************
void loop()
{
  //****************************
  //if our code is non-blocking, the heartBeat LED should toggle every 500ms
  if (millis() - heartBeatMillis >= 500)
  {
    //restart the timer
    heartBeatMillis = millis();

    //toggle the LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //****************************
  checkSwitches();

  //****************************
  handleLEDs();


} //END of  loop()

//**********************************************************************




//                    c h e c k S w i t c h e s ( )
//**********************************************************************
void checkSwitches()
{

  //more to come

} //END of   checkSwitches()


//                       h a n d l e L E D s ( )
//**********************************************************************
void handleLEDs()
{

  //more to come

} //END of  handleLEDs()

//**********************************************************************

Step #1

Study this tutorial, make sure you understand what it teaches.

Is there anything in the tutorial that you do not understand?

Step #2
In the above attached partial sketch, do you understand everything that is happening?

Edited with Schematic and the start of the sketch.