# Sistem with 6 led two buttons and a potentiometer

Sorry, I’m a beginner.
so far I have created simple projects, but this I cannot understand how to do it.
I have to solve this problem for a course I’m following.

Realization of a system with 6 LEDs two buttons and,
a potentiometer that is able to:

• switch on one of the six LEDs in mutual exclusion
that once it reaches its final position
will flash 4 times at the 4 Hz frequency for
then stay on
• the potentiometer will set the LED to be lit.
• Button 1 will reverse the direction of the movement
• button 2 will turn on a second LED
adjacent to the one already on and continuing to work
as in the previous points, a further pressure of the
button will return the system to the initial conditions

the circuit is physically that of the photo.

Can you help me on how to proceed?

What have you tried so far? Please post your code using code tags and people can help you figure this out. As a starting point, I'd suggest reading Several Things at a Time and use that structure for your code since you will want to be checking our buttons while you are flashing your leds, etc.

I also suggest that you start small with something like getting the LEDs to work the way you want and then worry about adding in the additional requirements of what the buttons add (reverse direction, 2nd led)

First of all I wanna give your instructor a smack in the head for teaching you Fritzing instead of how to draw a and read a real schematic. Maybe you can go and do this on my behalf. The only place a Fritzing diagram has in a course is next to the schematic diagram in order to help a beginning student build up a breadboard, to get them started quickly. After a few lessons I start to omit the Fritzing, time to learn to do the schematic to breadboard translation yourself.

So the pot part, that’s calling for a series of if statements.

The buttons produce is a state change - a basic state machine.

Both buttons will need debouncing.

Good luck!

So far I’ve come to this point, but the button 1 doesn’t work for me

``````int ledArray[6] = {8,9,10,11,12,13};
int statoLedArray[6] = {0, 0, 0, 0, 0, 0};
int sequenza1[6] = {8,9,10,11,12,13};
int sequenza2[6] = {13,12,11,10,9,8};

int pin_potenziometro = A0;
int value_potenziometro=0;

int pinPulsante1 = 6;
int pinPulsante2 = 7;

int statoPulsante1 = LOW;
int attesaDebounce = 50;
unsigned long ultimoTempoDebounce = 0;
int ultimaLettura = LOW;

int key_pulsante2 = 0;
/*------------------------------------------------------*/

void setup() {
for(int i=0; i<sizeof(ledArray);i++){
pinMode(ledArray[i], OUTPUT);
}
pinMode(A0, INPUT ); //Il pin è in ingresso
pinMode(pinPulsante1, INPUT);
pinMode(pinPulsante2, INPUT);

}

void loop(){
value_potenziometro = analogRead(pin_potenziometro); //Memorizzo il valore della variabile con la lettura del Potenziometro

switch(value_potenziometro){

case 0 ... 145:{
for(int i = 0; i<sizeof(ledArray);i++){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
break;
}
case 146 ... 290:{

if(statoLedArray[0] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[0], HIGH);
delay(125);
digitalWrite(ledArray[0], LOW);
delay(125);
}
}
digitalWrite(ledArray[0], HIGH);
statoLedArray[0] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[0]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = 0;
}
}
break;
}
case 291 ... 435:{

if(statoLedArray[1] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[1], HIGH);
delay(125);
digitalWrite(ledArray[1], LOW);
delay(125);
}
}
digitalWrite(ledArray[1], HIGH);
statoLedArray[1] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[1]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 436 ... 580:{

if(statoLedArray[2] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[2], HIGH);
delay(125);
digitalWrite(ledArray[2], LOW);
delay(125);
}
}
digitalWrite(ledArray[2], HIGH);
statoLedArray[2] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[2]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 581 ... 725:{

if(statoLedArray[3] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[3], HIGH);
delay(125);
digitalWrite(ledArray[3], LOW);
delay(125);
}
}
digitalWrite(ledArray[3], HIGH);
statoLedArray[3] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[3]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 726 ... 870:{

if(statoLedArray[4] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[4], HIGH);
delay(125);
digitalWrite(ledArray[4], LOW);
delay(125);
}
}
digitalWrite(ledArray[4], HIGH);
statoLedArray[4] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[4]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 871 ... 1023:{

if(statoLedArray[5] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[5], HIGH);
delay(125);
digitalWrite(ledArray[5], LOW);
delay(125);
}
}
digitalWrite(ledArray[5], HIGH);
statoLedArray[5] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[5]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}

}

//{
int lettura = digitalRead(pinPulsante1);
if(lettura != ultimaLettura){
ultimoTempoDebounce = millis();
}

if((millis() - ultimoTempoDebounce) > attesaDebounce){
if(lettura != statoPulsante1 and lettura == HIGH){
for(int i=0; i< sizeof(ledArray);i++){
ledArray[i] = sequenza2[i];
}

} else {

if(lettura != statoPulsante1 and lettura == LOW){
for(int i=0; i< sizeof(ledArray);i++){
ledArray[i] = sequenza1[i];
}
statoPulsante1 = lettura;
}

ultimaLettura = lettura;
delay(10);
}

}

}
``````

You need to configure pinPulsante1 and pinPulsante2 for INPUT_PULLUP otherwise the input will float with nothing connected to it.

Calimero101:
So far I've come to this point, but the button 1 doesn't work for me

No wonder, you're seemingly not doing anything with this "button 1", whatever it may be: I can't find any reference to "button 1" or even "button" in that code.

everywhere you have this:

``````  for (int i = 0; i < sizeof(ledArray); i++) {
``````

it is wrong. Your ledArray are ints which are 2 bytes to you are looping over twice as many elements. You need this:

``````const int sizeLEDArray = 6;
int ledArray[sizeLEDArray] = {8,9,10,11,12,13};
...
for(int i=0; i< sizeLEDArray; i++) { ... }
``````

or the more flexible way

``````int ledArray[] = {8,9,10,11,12,13};
const int sizeLEDArray = sizeof(ledArray) / sizeof(ledArray[0]);
...
for(int i=0; i< sizeLEDArray; i++) { ... }
``````

if I make the modification you said, nothing works.
with my code the whole part of the power on and flashing of the led works and it is not only the change of sequence with the button 1 that unzines me.
that is, sequence 2 takes me but does not return me to sequence 1 after a further click

``````int ledArray[6] = {8,9,10,11,12,13};
int statoLedArray[6] = {0, 0, 0, 0, 0, 0};
int sequenza1[6] = {8,9,10,11,12,13};
int sequenza2[6] = {13,12,11,10,9,8};

int pin_potenziometro = A0; // il pin a cui è connesso il potenziometro 1 //
int value_potenziometro=0; // lettura del valore del potenziometro 1 //

int pinPulsante1 = 6;
int pinPulsante2 = 7;

int statoPulsante1 = LOW;
int ultimaLettura = LOW;

int statoPulsante2 = LOW;
/*-----------------------------------------------------------*/

void setup() {
for(int i=0; i<sizeof(ledArray);i++){
pinMode(ledArray[i], OUTPUT);
}
pinMode(A0, INPUT ); //Il pin è in ingresso
pinMode(pinPulsante1, INPUT_PULLUP);
pinMode(pinPulsante2, INPUT_PULLUP);

}

void loop(){

value_potenziometro = analogRead(pin_potenziometro); //Memorizzo il valore della variabile con la lettura del Potenziometro

switch(value_potenziometro){
case 0 ... 145:{
for(int i = 0; i<sizeof(ledArray);i++){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
break;
}
case 146 ... 290:{
if(statoLedArray[0] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[0], HIGH);
delay(125);
digitalWrite(ledArray[0], LOW);
delay(125);
}
}
digitalWrite(ledArray[0], HIGH);
statoLedArray[0] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[0]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = 0;
}
}
break;
}
case 291 ... 435:{
if(statoLedArray[1] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[1], HIGH);
delay(125);
digitalWrite(ledArray[1], LOW);
delay(125);
}
}
digitalWrite(ledArray[1], HIGH);
statoLedArray[1] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[1]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 436 ... 580:{
if(statoLedArray[2] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[2], HIGH);
delay(125);
digitalWrite(ledArray[2], LOW);
delay(125);
}
}
digitalWrite(ledArray[2], HIGH);
statoLedArray[2] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[2]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 581 ... 725:{
if(statoLedArray[3] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[3], HIGH);
delay(125);
digitalWrite(ledArray[3], LOW);
delay(125);
}
}
digitalWrite(ledArray[3], HIGH);
statoLedArray[3] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[3]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 726 ... 870:{
if(statoLedArray[4] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[4], HIGH);
delay(125);
digitalWrite(ledArray[4], LOW);
delay(125);
}
}
digitalWrite(ledArray[4], HIGH);
statoLedArray[4] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[4]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 871 ... 1023:{
if(statoLedArray[5] == LOW){
for(int i =0; i<4; i++){
digitalWrite(ledArray[5], HIGH);
delay(125);
digitalWrite(ledArray[5], LOW);
delay(125);
}
}
digitalWrite(ledArray[5], HIGH);
statoLedArray[5] = HIGH;
for(int i = 0; i<sizeof(ledArray);i++){
if(ledArray[i] != ledArray[5]){
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}

}

/*I set button 1 as a mode switch
to have sequence 2 with the switch on
while with the switch off, the initial sequence remains
i.e. the sequence 1*/

int lettura = digitalRead(pinPulsante1);
if(lettura != ultimaLettura){

if(lettura != statoPulsante1 and lettura == HIGH){
for(int i=0; i< sizeof(ledArray);i++){
ledArray[i] = sequenza2[i];
}
}else
for(int i=0; i< sizeof(ledArray);i++){
ledArray[i] = sequenza1[i];

}
statoPulsante1 = lettura;
}

ultimaLettura = lettura;
delay(10);
}
``````

No, you did NOT make the modifications I suggested. You are still using sizeof(ledArray) in your for() loops which is looping twice as much as needed and also causing you to write beyond the end of your ‘statoLedArray’ array.

``````const int ArraySize = 6;
int ledArray[ArraySize] = {8, 9, 10, 11, 12, 13};
int statoLedArray[ArraySize] = {0, 0, 0, 0, 0, 0};
int sequenza1[6] = {8, 9, 10, 11, 12, 13};
int sequenza2[6] = {13, 12, 11, 10, 9, 8};

int pin_potenziometro = A0; // il pin a cui è connesso il potenziometro 1 //
int value_potenziometro = 0; // lettura del valore del potenziometro 1 //

int pinPulsante1 = 6;
int pinPulsante2 = 7;

int statoPulsante1 = LOW;
int ultimaLettura = LOW;

int statoPulsante2 = LOW;
/*-----------------------------------------------------------*/

void setup() {
for (int i = 0; i < ArraySize; i++) {
pinMode(ledArray[i], OUTPUT);
}
pinMode(A0, INPUT ); //Il pin è in ingresso
pinMode(pinPulsante1, INPUT_PULLUP);
pinMode(pinPulsante2, INPUT_PULLUP);
}

void loop() {

value_potenziometro = analogRead(pin_potenziometro); //Memorizzo il valore della variabile con la lettura del Potenziometro

switch (value_potenziometro) {
case 0 ... 145: {
for (int i = 0; i < ArraySize; i++) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
break;
}
case 146 ... 290: {
if (statoLedArray[0] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[0], HIGH);
delay(125);
digitalWrite(ledArray[0], LOW);
delay(125);
}
}
digitalWrite(ledArray[0], HIGH);
statoLedArray[0] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[0]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = 0;
}
}
break;
}
case 291 ... 435: {
if (statoLedArray[1] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[1], HIGH);
delay(125);
digitalWrite(ledArray[1], LOW);
delay(125);
}
}
digitalWrite(ledArray[1], HIGH);
statoLedArray[1] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[1]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 436 ... 580: {
if (statoLedArray[2] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[2], HIGH);
delay(125);
digitalWrite(ledArray[2], LOW);
delay(125);
}
}
digitalWrite(ledArray[2], HIGH);
statoLedArray[2] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[2]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 581 ... 725: {
if (statoLedArray[3] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[3], HIGH);
delay(125);
digitalWrite(ledArray[3], LOW);
delay(125);
}
}
digitalWrite(ledArray[3], HIGH);
statoLedArray[3] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[3]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 726 ... 870: {
if (statoLedArray[4] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[4], HIGH);
delay(125);
digitalWrite(ledArray[4], LOW);
delay(125);
}
}
digitalWrite(ledArray[4], HIGH);
statoLedArray[4] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[4]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}
case 871 ... 1023: {
if (statoLedArray[5] == LOW) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledArray[5], HIGH);
delay(125);
digitalWrite(ledArray[5], LOW);
delay(125);
}
}
digitalWrite(ledArray[5], HIGH);
statoLedArray[5] = HIGH;
for (int i = 0; i < ArraySize; i++) {
if (ledArray[i] != ledArray[5]) {
digitalWrite(ledArray[i], LOW);
statoLedArray[i] = LOW;
}
}
break;
}

}

/*I set button 1 as a mode switch
to have sequence 2 with the switch on
while with the switch off, the initial sequence remains
i.e. the sequence 1*/

int lettura = digitalRead(pinPulsante1);
if (lettura != ultimaLettura) {

if (lettura != statoPulsante1 and lettura == HIGH) {
for (int i = 0; i < ArraySize; i++) {
ledArray[i] = sequenza2[i];
}
} else
for (int i = 0; i < ArraySize; i++) {
ledArray[i] = sequenza1[i];
}
statoPulsante1 = lettura;
}

ultimaLettura = lettura;
delay(10);
}
``````

Hi,
Does the pot control work, do the LEDs operate in the sequence?

Tom…

The first part of the code, i.e. the LEDs lit by a potentiometer, works correctly.
while the part of code relating to button 1 which must reverse the sequence, creates problems for me.
in fact, at the first press the sequence is reversed but I cannot press the button again to return to the initial sequence.

When you modify your code but still have problems, please post that version of the code so we can all see it. I have no idea what version of code you are using and/or if you have fixed all the issues pointed out to you.

blh64:
I have no idea what version of code you are using and/or if you have fixed all the issues pointed out to you.

Any or all.