Help LED turning

Hello :slight_smile:
i have a group of 8 led’s, they are organized in a ring, and lightning up one by one, currently they are going with the clock. what i want is them to change direction to couterclock when i push the button.

can anybody give me some tips on how i can fix this?
(ps: im new at arduino and programming, and to this forum, so i hope i posted on the right thread)

can anybody give me some tips on how i can fix this?

My crystal ball tells me that you need to look at line 37 ow WeirdStuff.java. That can't really be right, can it? Perhaps it would be better if I didn't have to rely on my crystal ball to see your code.

I don't need a crystal ball at all to see that you didn't bother reading the posts at the top of the forum - the one that says "Read this BEFORE you post here".

When is our homework due?

This is not homework, just something i want to learn :slight_smile:

well, this is my program so far:

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
void setup() {

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
}
void loop() {
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);

digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);

digitalWrite(led3, HIGH);
delay(100);
digitalWrite(led3, LOW);
delay(100);

digitalWrite(led4, HIGH);
delay(100);
digitalWrite(led4, LOW);
delay(100);

digitalWrite(led5, HIGH);
delay(100);
digitalWrite(led5, LOW);
delay(100);

digitalWrite(led6, HIGH);
delay(100);
digitalWrite(led6, LOW);
delay(100);

digitalWrite(led7, HIGH);
delay(100);
digitalWrite(led7, LOW);
delay(100);

digitalWrite(led8, HIGH);
delay(100);
digitalWrite(led8, LOW);
delay(100);
}

So my questieon is: is there any ways to make this simplier? and is ther an good way to make the leds go the other way?
im sorry if i posted wrong… :~

Simpler ? Yes. Whenever you see variables with the same name apart from a numeric suffix you should think ‚Äúcould I use an array variable to hold these value ?‚ÄĚ. The answer is yes.

Declare an array at the start of the program like this
const byte arrayOfPinNumbers[] = {3, 4, 5, 6, 7, 8, 9, 10}Now you have a list of pin numbers that you can refer to by using an index to the array.

Try this

const byte arrayOfPinNumbers[] = {3, 4, 5, 6, 7, 8, 9, 10};

void setup() 
{
  Serial.begin(115200);
  for (int pin = 0; pin < 8; pin++)
  {
    Serial.println(arrayOfPinNumbers[pin]);
  }
}

void loop() 
{
}

You should see a list of pin numbers printed out (you will need to set the baud rate to suit your setup)

If you put the for loop in the loop() function instead of setup() the list of pin numbers will repeat over and over again. Now, instead of printing the pin number, suppose that you used the pin number to turn an LED on and off ?

Post your code when you have got that working or if you have problems and we can then advise on changing the order in which the pin numbers are read so that the LEDs can be lit in the opposite order.

Other homework for you.
Add a pushbutton to the Arduino, read the value at its pin and print it.
Research pinMode(pinNumber, INPUT_PULLUP);
Look at and understand the StateChangeDetection example in the IDE
Read the stickies at the top of this forum for advice on how to get help.
When you post your code put it in code tags like mine above.

Thanks for answere :slight_smile:

I'm a little afraid that this is beyond my knowledge at this moment. but i will take a look at it and see what i can do :wink:

Start with my example program. It will give a list po pin numbers that have LEDs attached by reading them from the array.

Try
digitalWrite(arrayOfPinNumbers[0], HIGH); It will turn on the first LED
digitalWrite(arrayOfPinNumbers[1], HIGH); It will turn on the second LED
I hope that you can see where we are going with this. The for loop provides the numbers to put in the square brackets so that a series of LEDs can be turned on (and off, of course)

Ya here is a simpler way implement is both clock wise and anti clock wise in much simpler way.

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
int button = 1; //botton is present at pin 1 
// When unpressed input 5V When pressed 0V
void setup() 
{                
  pinMode(button, INPUT);
  pinMode(led1, OUTPUT);     
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
}
void loop() 
{
  int x;
  for(x=3; x<=10; x++)
  {
    digitalWrite(x, HIGH);
    delay(100);
    digitalWrite(x, LOW);
    delay(100);
  }
  if(button == LOW)
  {
    for(x=10; x>2; x--)
    {
      digitalWrite(x, HIGH);
      delay(100);
      digitalWrite(x, LOW);
      delay(100);
    }
  }
}

Moderator edit: Personal insults removed - DO NOT REPEAT THEM.

martinoyh:
I'm a little afraid that this is beyond my knowledge at this moment.

As it appears is reading the "how to use this forum" sticky post that tells you how to post your code.

Thanks for asweres, i have tried by my self, and keeping it simple enough for me to understand :wink:
I almost have the result i want, i just need to make the direction stay in that direction when i push the button.
here is what i got so far:

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
const int knapp = 11;
int buttonState = 0;

void setup() {                
  
  pinMode(led1, OUTPUT);     
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(knapp, INPUT);
};

void loop() {
  
  buttonState = digitalRead(knapp);
  if (buttonState == HIGH) {
  
    digitalWrite(led1, HIGH);  
  delay(100);               
  digitalWrite(led1, LOW);    
  delay(100);   

digitalWrite(led2, HIGH);  
  delay(100);               
  digitalWrite(led2, LOW);    
  delay(100);    

digitalWrite(led3, HIGH);  
  delay(100);               
  digitalWrite(led3, LOW);    
  delay(100);      
  
  digitalWrite(led4, HIGH);  
  delay(100);               
  digitalWrite(led4, LOW);    
  delay(100);    
  
  digitalWrite(led5, HIGH);  
  delay(100);               
  digitalWrite(led5, LOW);    
  delay(100);    

digitalWrite(led6, HIGH); 
  delay(100);               
  digitalWrite(led6, LOW);    
  delay(100);   
 
 digitalWrite(led7, HIGH);  
  delay(100);               
  digitalWrite(led7, LOW);    
  delay(100);     
  
  digitalWrite(led8, HIGH); 
  delay(100);               
  digitalWrite(led8, LOW);    
  delay(100);    
  }

else 
{
  digitalWrite(led8, HIGH); 
  delay(100);               
  digitalWrite(led8, LOW);    
  delay(100);   
  
  digitalWrite(led7, HIGH); 
  delay(100);               
  digitalWrite(led7, LOW);    
  delay(100);   
  
  digitalWrite(led6, HIGH); 
  delay(100);               
  digitalWrite(led6, LOW);    
  delay(100);   
  
  digitalWrite(led5, HIGH); 
  delay(100);               
  digitalWrite(led5, LOW);    
  delay(100);   
  
  digitalWrite(led4, HIGH); 
  delay(100);               
  digitalWrite(led4, LOW);    
  delay(100);   
  
  digitalWrite(led3, HIGH); 
  delay(100);               
  digitalWrite(led3, LOW);    
  delay(100);   
  
  digitalWrite(led2, HIGH); 
  delay(100);               
  digitalWrite(led2, LOW);    
  delay(100);   
  
  digitalWrite(led1, HIGH); 
  delay(100);               
  digitalWrite(led1, LOW);    
  delay(100);
}  
 
   
  }

hopfully i post my code the rigth way this time :wink:

hopfully i post my code the rigth way this time

You did. Thank you.

I almost have the result i want, i just need to make the direction stay in that direction when i push the button.

If I understand you correctly, you want to press and release the button. Each time that happens, you want the direction to change. Is that correct?

If it is, then you are interested in setting a flag when the switch BECOMES pressed. See the state change detection example for how to detect when the switch BECOMES pressed, rather than seeing when the switch IS pressed.

Then, if the flag has one value (a boolean set to true, for instance) rotate one way. Otherwise, rotate the other way.

Before you go there, though, you really should look at arrays and for loops. It is far simpler to make enhancements to code that has 20 lines than it is to make enhancements to code that has 200 lines.

I cant realy find out how to put all these examples u guys are giving me together. so my question then is, can someone post a complete (or almost complete) code? so i can look how it’s all buildt up ? :slight_smile:

martinoyh:
I cant realy find out how to put all these examples u guys are giving me together. so my question then is, can someone post a complete (or almost complete) code? so i can look how it's all buildt up ?

This is where we came in isn't it?