The servo struggle is real

Our Programm includes arrays. As we take out the array from the Programm, the servo works perfectly. As soon as the array is included in the Programm, the servo does not work at all.

Can arrays block the servo from running?
Do we have to define the Servo new with arrays included in the Programm? If yes, how?

Thanks for your support

arrrrrduino:
Can arrays block the servo from running?

No, except if you try to declare a very large array.

arrrrrduino:
Do we have to define the Servo new with arrays included in the Programm? If yes, how?

I have no idea what you ask there. But this is a forum for program help. So we expect to see your code (no snippets) posted inside code tags.

Sorry for that. In this situation, the Servo does not work. However the Servo should work. Perhaps it is because of the definition of the array.

Getr_nkeautomat_Programm.ino (3.86 KB)

Just POST the damned code.

#include <Servo.h>

struct getraenk{
  int Ausgabe;
  int ZaehlmechanismusLichtschranke;
  int Ausgangslage;
};
struct getraenk getraenke[3];

//setup
int index = 13;  
int Motor = 12; //Motor
int Button1 = 24; //Button1
int Button2 = 26; //Button2
int Button3 = 28; //Button3
int Button4 = 30; //Button4
int Button5 = 32; //MysteryButton
int Lichtschranke = 31;//Lichtschranke



Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position 
  

void setup() {
  Serial.begin(9600);
  pinMode(Motor, OUTPUT); //Motor
  pinMode(Button1, INPUT); //Button1
  pinMode(Button2, INPUT); //Button2
  pinMode(Button3, INPUT); //Button3
  pinMode(Button4, INPUT); //Button4
  pinMode(Button5, INPUT); //MysteryButton
  digitalWrite(Button1, HIGH); //Button1 HIGH
  digitalWrite(Button2, HIGH); //Button2 HIGH
  digitalWrite(Button3, HIGH); //Button3 HIGH
  digitalWrite(Button4, HIGH); //Button4 HIGH
  digitalWrite(Button5, HIGH); //MyteryButton HIGH
  pinMode(Lichtschranke, INPUT); //Licht
  digitalWrite(Lichtschranke, HIGH); //Licht Hoch
  myservo.attach(22);  // attaches the servo on pin 9 to the servo object 
  
  
  getraenke[0].Ausgabe = 0;
  getraenke[0].ZaehlmechanismusLichtschranke = 0;
  getraenke[0].Ausgangslage = 33;
  
  getraenke[1].Ausgabe = 8;
  getraenke[1].ZaehlmechanismusLichtschranke = 0;
  getraenke[1].Ausgangslage = 33;
  
  getraenke[2].Ausgabe = 16;
  getraenke[2].ZaehlmechanismusLichtschranke = 0;
  getraenke[2].Ausgangslage = 33;
  
  getraenke[3].Ausgabe = 24;
  getraenke[3].ZaehlmechanismusLichtschranke = 0;
  getraenke[3].Ausgangslage = 33;
  
  
}

void loop() {
  int buttonState1;
  buttonState1 = digitalRead(Button1);
  
  int buttonState2;
  buttonState2 = digitalRead(Button2);
  
  int buttonState3;
  buttonState3 = digitalRead(Button3);
  
  int buttonState4;
  buttonState4 = digitalRead(Button4);
  
  int buttonState5;
  buttonState5 = digitalRead(Button5);
  
  int randNumber;
  
  randNumber = random(1,5);
 
  if (buttonState1 == LOW)
     {index = 1;}
 
  if (buttonState2 == LOW)
     {index = 2;}  
  
  if (buttonState3 == LOW)
     {index = 3;}  
     
  if (buttonState4 == LOW)
     {index = 4;}  
  
  if (buttonState5 == LOW)
     {delay(1000); 
     index = randNumber;}  
 
 

 
     
     
  
  
  
  int LichtschrankeZustand;
  LichtschrankeZustand = digitalRead(Lichtschranke);
  
  
  
  Serial.println(index, DEC);
  digitalWrite(13, HIGH);
  
  
  
if (index != 13)    
  {
  digitalWrite(Motor,HIGH);

if (LichtschrankeZustand == LOW) 
  {
    delay(1000);
    getraenke[index].ZaehlmechanismusLichtschranke = getraenke[index].ZaehlmechanismusLichtschranke + 1;
  } 
if (getraenke[index].ZaehlmechanismusLichtschranke == getraenke[index].Ausgabe)
  {
    digitalWrite(Motor,LOW);
    for(pos = 0; pos <= 120; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 120; pos>=0; pos -= 1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
   
   
    myservo.write(LOW);
  
  }

getraenke[index].ZaehlmechanismusLichtschranke = getraenke[index].ZaehlmechanismusLichtschranke + 1;

  
  }

digitalWrite(Motor,HIGH);

if (LichtschrankeZustand == LOW) 
  {
  delay(100);
  getraenke[index].ZaehlmechanismusLichtschranke = getraenke[index].ZaehlmechanismusLichtschranke + 1;
  }

if (getraenke[index].ZaehlmechanismusLichtschranke == getraenke[index].Ausgangslage)
  {
   digitalWrite(Motor,LOW);
  }

}
}
 getraenke[3].Ausgabe = 24;

A three element array has no index 3.

I asked you to place it in code tags :wink: While you are at it, press ctrl+T in the IDE and see how it looks better :wink:

And I still have no idea what to motor needs to do and what you did "when it worked". Because the movement of servo is inside a if-statement that checks something in that array. So the "working" code is useful as well.

[ahhhh] While writing the tips I found the error. Declaring the array isn't a problem. But assigning values to the 4th element in a array with only 3 elements will not work :wink:

Some tips

Pos doesn't need to be global

More comments. You never explain what you want to do

      for (pos = 120; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees

And where you have comments they don't make sens... Or make damn sure they say the same as the code or just don't use the values in the comment. So a comment for that would be "sweep counter clockwise"

Once you start numbering variables arrays are the answer :wink:

const byte ButtonPins[] = {24, 26, 28, 30, 32};

Also made them const (they never change on run time) and byte big enough to hold the value).

myservo.attach(22);  // attaches the servo on pin 9 to the servo object

Besides the comment that tells something different then the code, you also just use a number out of the blue. Doing

myservo.attach(ServoPin);

Would be less prone to mis-numbering, easier to remember and is so clear it doesn't really need any comments.

You could initialize all members of getraenke when you make the array.

struct getraenk getraenke[4] = 
  { {0, 0, 33},
    {8, 0, 33},
    {16, 0, 33},
    {24, 0, 33}  //Hey, this is a 4th element so you need a array of size 4!
  };
[code]

[code]
digitalWrite(Button1, HIGH); //Button1 HIGH

Although it still works fine, it's the old way of setting the pull ups. Easier and more clear is to do

pinMode(Button1, INPUT_PULLUP);

[/code]
[/code]