New To Coding

Hi

I’m a technical director for a high school in Texas and over the summer started teaching myself coding. I bought a kit and have been toying with the projects. We are performing Beauty and the Beast for our Musical and are wanting to make our enchanted rose button operated. I started to piece together a code and its almost there. What we want to achieve is to have 5 rose petals attached to 5 servos, overtime the button is pushed it powers a servo to release a petal. Then the 6 press would reset all 5 servos to the starting position so we can reattach them for the next performance. So far with my current code a button push will power all 5 servos to move at the same time but thats it. Again I’m very new to the coding world so its kinda hard for me to know what’s missing. I would appreciate any help anyone can give me. Thanks in advance.

#include <Servo.h>

int button = 2;
int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
int press5 = 0;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;

void setup()
{
  pinMode(button, INPUT);
  servo1.attach(3);
  digitalWrite(2, HIGH);
  
  pinMode(button, INPUT);
  servo2.attach(4);
  digitalWrite(2, HIGH);
  
  pinMode(button, INPUT);
  servo3.attach(5);
  digitalWrite(2, HIGH);
  
  pinMode(button, INPUT);
  servo4.attach(6);
  digitalWrite(2, HIGH);
  
  pinMode(button, INPUT);
  servo5.attach(7);
  digitalWrite(2, HIGH);
}

void loop()
{
    press1 = digitalRead(button);
    if (press1 == LOW);
    {
    servo1.write(90);
    }
     
    press2 = digitalRead(button);
    if (press2 == LOW);
    {
    servo2.write(90);
    }
    
    press3 = digitalRead(button);
    if (press3 == LOW);
    {
    servo3.write(90);
    }
    
    press4 = digitalRead(button);
    if (press4 == LOW);
    {
    servo4.write(90);
    }
    
    press5 = digitalRead(button);
    if (press5 == LOW);
    {
    servo5.write(90);
    }
}

[This really should be moved to the "Programming" section by a moderator]

I'm not familiar with the play, but I think I can guess what's going on.

  pinMode(button, INPUT);
  ....
  digitalWrite(2, HIGH);

You don't need to make the pin mode INPUT 5 times. It stays an input. You also use two different methods of referring to the same pin - once you give it a name like "button" you should always use that name. Then this code looks like very old code, where you wrote an input HIGH to enable the input pullup. The correct code for this is:  pinMode(button, INPUT_PULLUP);

Your code will operate all petals simultaneously because it has no memory for where it is up to. It doesn't 'know' that petal 1 has dropped and the next push of the button will drop petal 2. It just sees that the button is currently pushed and starts dropping them all.

The "engineers gloves" method of doing this is to have a flexible framework that can take any number of servos and complex button pushes. This is overkill. It's possible to do this in a very simple Arduino program...

//assume you've fixed setup() as I asked above, and you still have button and the servos declared properly
const int DebounceDelay = 100; //milliseconds = ignore little glitches in the button shorter than this time
void loop() {
  while(digitalRead(button) == HIGH) {
    //do nothing while we wait for the button to be pushed
  }
  servo1.write(90);
  
  //Now the LOW signal which got us to this point may be just the first 'bounce' of the button and it
  //will keep cycling between HIGH and LOW for a few milliseconds.
  //The simplest way to eliminate that is to ignore it. 
  //After waiting a few milliseconds, the input will be at a stable LOW
  delay(DebounceDelay);  
  while(digitalRead(button) == LOW) {
    //do nothing while we wait for the button to be released
  }

  delay(DebounceDelay);  //wait long enough for a stable HIGH
  while(digitalRead(button) == HIGH) {
    //do nothing while we wait for the button to be pushed
  }
  servo2.write(90);
  
  delay(DebounceDelay);  
  while(digitalRead(button) == LOW) {
    //do nothing while we wait for the button to be released
  }


  //repeat for the desired number of servos
}

For resetting for the next performance, I would have the setup() function drive all the servos to the correct position. That way it will be ready as soon as it is powered on. I expect in a stage show you don't want to see the thing move on a 6th button push. You can't trust the button-pushers, even if it is yourself.

Thank you so much for your help. I re-worked the setup() and added the part to reset the servos. It worked. I can't thank you enough.

You mentioned the code was an "old code", where can I read about new code or to get up to date on stuff like that?

Well, stay away from Instructables. They seem pretty bad with the old code.

All of the examples that came with your copy of Arduino are up to date with the latest coding standards. Most of the stuff in the playground is old but good.