Useless Box code just loops on Arduino Nano

Basically it runs to either Action 4 or 5 and then loops back to the beginning. I dont see any obvious reasons for it. I am running an older Nano using the old bootloader. Could it be the Arduino is bad? I appreciate any help. I am documenting the entire thing and including printable files to throw a kit together for charity (with the orginal coder's appoval). The code is my last obstacle. Thank you very much for your help!

Image of the schematic
Video of the action

#include <Servo.h>
Servo lidservo;
Servo armservo;
Servo flag;

int switchpin = 5;
int ledpin = 13;

int action = 1;
int pos = 0;

// Servo positions

const int lidOpen = 20;
const int lidHalf = 50;
const int lidClosed = 90;
const int armDown = 15;
const int armSwitch = 155;
const int armNoSwitch = 15;
const int flagDown = 0;
const int flagUp = 125;
const int flagLow = 75;

void setup() {
  pinMode(switchpin, INPUT);
  lidservo.attach(2);
  armservo.attach(3);
  flag.attach(4);

  // Servo Default Starting Position

  armservo.write(armDown);
  flag.write(flagDown);
  lidservo.write(lidClosed);

  // debug led
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, LOW);
}

void loop() {

  // Action Selection starting with 1

  if (digitalRead(switchpin) == HIGH)
  {
    digitalWrite(ledpin, HIGH); // debug tell us we are doing an action
    if (action > 8) {
      action = 1;
    }
    switch (action) {
      case 1:
        action1();
        break;
      case 2:
        action2();
        break;
      case 3:
        action3();
        break;
      case 4:
        action4();
        break;
      case 5:
        action5();
        break;
      case 6:
        action6();
        break;
      case 7:
        action7();
        break;
    }
    digitalWrite(ledpin, LOW); // debug tell us action has finished
    action += 1;
  }
}

//The Following are actions that are chosen chronologically

void action1() { // classic open lid and turn off switch
  lidservo.write(lidOpen); // open lid
  delay(1000);
  armservo.write(armSwitch); // turn off switch
  delay(500);
  armservo.write(armDown); // retract arm
  delay(1000);
  lidservo.write(lidClosed); // close lid
  delay(1000);
}

void action2() { // partly open lid and quickly turn off switch
  lidservo.write(lidHalf); // lid 1/2 way open
  delay(100);
  armservo.write(armSwitch); // turn off switch
  delay(250);
  armservo.write(armDown); // return arm
  delay(100);
  lidservo.write(lidClosed); // close lid
}

void action3() { // slowly open lid turn off switch
  for (pos = lidClosed; pos > lidOpen; pos -= 1)
  {
    lidservo.write(pos); // slowly open lid
    delay(50);
  }
  armservo.write(armSwitch); // turn off switch
  delay(250);
  armservo.write(armDown); // return arm
  delay(100);
  lidservo.write(lidClosed); // close lid
}

void action4() { // wait, then open lid, turn off switch and slowly return arm
  delay(250);  // wait
  lidservo.write(lidHalf);  // open lid
  delay(100);
  armservo.write(armSwitch); // quickly turn off switch
  delay(2500); // wait wait wait
  for (pos = armSwitch; pos > lidClosed; pos -= 1) // slowly return arm
  {
    armservo.write(pos);
    delay(50);
  }
  armservo.write(armDown);
  delay(100);
  lidservo.write(lidClosed);
 
}

void action5() { // open and close lid three times and then turn off switch
  for (int x = 0; x < 3; x++) {
    lidservo.write(lidHalf);
    delay(500);
    lidservo.write(lidClosed);
    delay(500);
  }
  delay(2500);
  lidservo.write(lidHalf);
  delay(100);
  armservo.write(armSwitch);
  delay(250);
  armservo.write(armDown);
  delay(100);
  lidservo.write(lidClosed);
}

void action6() { // twice almost turn off switch, do it third time
  for (int x = 0; x < 3; x++) {
    lidservo.write(lidHalf);
    delay(100);
    armservo.write(armNoSwitch);
    delay(250);
    armservo.write(armDown);
    delay(100);
    lidservo.write(lidClosed);
    delay(250);
  }
  lidservo.write(lidHalf);
  delay(100);
  armservo.write(armSwitch);
  delay(250);
  armservo.write(armDown);
  delay(100);
  lidservo.write(lidClosed);
}

void action7() {
  for (pos = lidClosed; pos > lidOpen; pos -= 2)
  {
    lidservo.write(pos);
    delay(50);
  }
  for (pos = armDown; pos < 145; pos += 2)
  {
    armservo.write(pos);
    delay(100);
  }
  armservo.write(armSwitch);
  for (pos = armSwitch; pos > armDown; pos -= 2)
  {
    armservo.write(pos);
    delay(100);
  }
  for (pos = lidOpen; pos < lidClosed; pos += 2)
  {
    lidservo.write(pos);
    delay(50);
  }
  lidservo.write(lidClosed);
}

void action8() {
  if (digitalRead(switchpin) == HIGH)
  {
    delay(1000);
    for (pos = lidClosed; pos > lidOpen; pos -= 2)
    {
      lidservo.write(pos);
      delay(50);
    }
    for (pos = flagDown; pos < flagUp; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    //--------------------------------
    delay(250);
    for (int x = 0; x < 4; x++) {
      for (pos = flagUp; pos > flagLow; pos -= 10)
      {
        flag.write(pos);
        delay(50);
      }
      for (pos = flagLow; pos < flagUp; pos += 10)
      {
        flag.write(pos);
        delay(50);
      }
    }

    delay(250);
    for (pos = 175; pos > armDown; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = lidOpen; pos < lidClosed; pos += 2)
    {
      lidservo.write(pos);
      delay(50);
    }
    delay(2000);
    lidservo.write(lidHalf);
    delay(100);
    armservo.write(armSwitch);
    delay(250);
    armservo.write(armDown);
    delay(100);
    lidservo.write(lidClosed);
  }

}

Welcome to the forum

    pinMode(switchpin, INPUT);
    if (digitalRead(switchpin) == HIGH)

What, if anything, keeps the input LOW when the switch is not closed ?

If this is your circuit then Bob is correct, you have a problem.

Switches are best wired between GND and input, with INPUT_PULLUP as the mode set in setup. Then, in your logic, look for a LOW as switch activated.

By the way, your Arduino won't last long powering three servos off the 5V output. It's not intended for any such use. Buy a 5V power supply, 2 or 3 Amperes, and use it for your projects going forward.

Well that was someones attempt to clean up the OP code. The original doesnt have the input low. I'm guessing I will have to write it from scratch to figure it out. Maybe I can hire someone on fiver to do it.

Original code:

#include <Servo.h>
Servo lidservo;
Servo armservo; 
Servo flag;

int switchpin = 3;

int action = 1;

int pos = 0;

void setup() {
pinMode(switchpin,INPUT);
lidservo.attach(1);
armservo.attach(2);
flag.attach(4);

// Servo Default Starting Position

armservo.write(50);
flag.write(50);
lidservo.write(90);
}

void loop() {

// Action Selection starting with 1

  if (digitalRead(switchpin) == HIGH)
  {
  if (action > 8) {
    action = 1;
  }  
    if (action == 1) {
    action1();
    }
    else if (action == 2) {
    action2();
    }
    else if (action == 3) {
    action3();
    }
    else if (action == 4) {
    action4();
    }
    else if (action == 5) {
    action5();
    }
    else if (action == 6) {
    action6();
    }
    else if (action == 7) {
    action7();
    }
    else if (action == 8) {
    action8();
    }

    action += 1;
    
  }
}

//The Following are actions that are chosen chronologically

void action1() {
  lidservo.write(20);
  delay(1000);
  armservo.write(177);
  delay(500); 
  armservo.write(50);
  delay(1000);
  lidservo.write(90);
  delay(1000);
}

void action2() {
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}

void action3() {
    for(pos = 90; pos > 20; pos -=1)
  {
    lidservo.write(pos);
    delay(50);
  }
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}

void action4() {
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(2500);
for(pos = 177; pos > 90; pos -=1)
  {
    armservo.write(pos);
    delay(50);
  }
  lidservo.write(90);
  armservo.write(50);
}

void action5() {
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(500);
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(500);
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(2500);
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}

void action6() {
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);  
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}

void action7() {
    for(pos = 90; pos > 20; pos -=2)
  {
    lidservo.write(pos);
    delay(50);
  }
for(pos = 50; pos < 145; pos +=2)
  {
    armservo.write(pos);
    delay(100);
  }
  armservo.write(177);
for(pos = 177; pos > 50; pos -=2)
  {
    armservo.write(pos);
    delay(100);
  }
for(pos = 20; pos < 90; pos +=2)
  {
    lidservo.write(pos);
    delay(50);
  }
  lidservo.write(90);
}

void action8() {
  if (digitalRead(switchpin) == HIGH)
  {
    delay(1000);
      for(pos = 90; pos > 20; pos -=2)
  {
    lidservo.write(pos);
    delay(50);
  }
      for(pos = 50; pos < 175; pos +=10)
  {
    flag.write(pos);
    delay(50);
  }
  delay(250);
      for(pos = 175; pos > 125; pos -=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 125; pos < 175; pos +=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 175; pos > 125; pos -=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 125; pos < 175; pos +=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 175; pos > 125; pos -=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 125; pos < 175; pos +=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 175; pos > 125; pos -=10)
  {
    flag.write(pos);
    delay(50);
  }
        for(pos = 125; pos < 175; pos +=10)
  {
    flag.write(pos);
    delay(50);
  }
  delay(250);
        for(pos = 175; pos > 50; pos -=10)
  {
    flag.write(pos);
    delay(50);
  }
   for(pos = 20; pos < 90; pos +=2)
  {
    lidservo.write(pos);
    delay(50);
  }
  delay(2000);
      lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  }

}

The project wont work with an external source. Trying to keep it a neat and clean box, not concerned with how many uses you get from a 9v. Besides soldering the switches the whole thing can be made with whatever hardware is already included and a 3d printer for under $25. Thank you for the switches information, I will read more on how to fix it that way.

The box

So you're saying you want to know so little you want us to just fix it for you, not bother you with the details like how to fix it and why? Yeah, go to fiver, I guess.

I wasn't saying that but don't worry. I won't bother you guys anymore. Thanks.

this code could probably be made much simpler by using tables to specify a servo, position(s), possibly rates of change and delays

@ka-choo the switch for the input at D5 is fine as it selects either 5 volts or GND.These will be read as HIGH and LOW, and as long as the logic makes sense of that, you are fine.

The other switch is just a lower switch.

A big problem is powering three servos off the 5 volts coming out of an Arduino powered by a nine volt battery, or for that matter wherever the board gets its Vin.

Since the entire circuit is switched totally off by design at the end of a useless period, you could just use 4x AA batteries. If this is to be a standalone thing you don't wanna use a plug-in power supply for.

a7

Too soon to go away! These are world wide fora, and many ppl I see here alla time are probably asleep, or at the beach, where in neither place can they take your code and run it.

At a glance through the tiny window in transit it looks plausible. The most common issue with servos is the havoc they can wreak if they are not powered carefully.

Imma bet your code is fine and that the undesirable behaviour will be found to have been a hardware issue.

But give us a few hours - if this isn't solved by the time I get to my lab, I will read the code and run it and then we'll see.

a7

Not for me it doesn't.

I used a pushbutton instead of your SPDT switch, and I did not arrange it mechanically so that any servo move would change the switch. Or cut power. I presume that the Useless Box goes through some dance and shuts itself off.

If you keep your finger on the button, the code spews servo commands step by step and repeats.

If you take your finger off the button, the current action is completed and progress through the steps halts.

If you then press again the button, the sequencing of steps continues from where it left off. If you hold the button down, the sequence continues and loops.

As a reading of the sketch would suggest.

If you are seeing what you claim, you need to look at the hardware not the software.

HTH

a7

Seems to be working... what should it do?

// https://forum.arduino.cc/t/useless-box-code-just-loops-on-arduino-nano/1202506

#include <Servo.h>
Servo lidservo;
Servo armservo;
Servo flag;
// int switchpin = 3;// does not need PWM
int switchpin = 2;
int action = 1;
int pos = 0;
void setup() {
  pinMode(switchpin, INPUT);
  // lidservo.attach(1); // do not use pin 1
  // armservo.attach(2); // Servo needs PWM
  // flag.attach(4); // Servo needs PWM
  lidservo.attach(3);
  armservo.attach(5);
  flag.attach(6);
  // Servo Default Starting Position
  armservo.write(50);
  flag.write(50);
  lidservo.write(90);
}
void loop() {
  // Action Selection starting with 1
  if (digitalRead(switchpin) == HIGH)
  {
    if (action > 8) {
      action = 1;
    }
    if (action == 1) {
      action1();
    }
    else if (action == 2) {
      action2();
    }
    else if (action == 3) {
      action3();
    }
    else if (action == 4) {
      action4();
    }
    else if (action == 5) {
      action5();
    }
    else if (action == 6) {
      action6();
    }
    else if (action == 7) {
      action7();
    }
    else if (action == 8) {
      action8();
    }
    action += 1;
  }
}
//The Following are actions that are chosen chronologically
void action1() {
  lidservo.write(20);
  delay(1000);
  armservo.write(177);
  delay(500);
  armservo.write(50);
  delay(1000);
  lidservo.write(90);
  delay(1000);
}
void action2() {
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}
void action3() {
  for (pos = 90; pos > 20; pos -= 1)
  {
    lidservo.write(pos);
    delay(50);
  }
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}
void action4() {
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(2500);
  for (pos = 177; pos > 90; pos -= 1)
  {
    armservo.write(pos);
    delay(50);
  }
  lidservo.write(90);
  armservo.write(50);
}
void action5() {
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(500);
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(500);
  lidservo.write(50);
  delay(500);
  lidservo.write(90);
  delay(2500);
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}
void action6() {
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(155);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
  delay(250);
  lidservo.write(50);
  delay(100);
  armservo.write(177);
  delay(250);
  armservo.write(50);
  delay(100);
  lidservo.write(90);
}
void action7() {
  for (pos = 90; pos > 20; pos -= 2)
  {
    lidservo.write(pos);
    delay(50);
  }
  for (pos = 50; pos < 145; pos += 2)
  {
    armservo.write(pos);
    delay(100);
  }
  armservo.write(177);
  for (pos = 177; pos > 50; pos -= 2)
  {
    armservo.write(pos);
    delay(100);
  }
  for (pos = 20; pos < 90; pos += 2)
  {
    lidservo.write(pos);
    delay(50);
  }
  lidservo.write(90);
}
void action8() {
  if (digitalRead(switchpin) == HIGH)
  {
    delay(1000);
    for (pos = 90; pos > 20; pos -= 2)
    {
      lidservo.write(pos);
      delay(50);
    }
    for (pos = 50; pos < 175; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    delay(250);
    for (pos = 175; pos > 125; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 125; pos < 175; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 175; pos > 125; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 125; pos < 175; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 175; pos > 125; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 125; pos < 175; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 175; pos > 125; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 125; pos < 175; pos += 10)
    {
      flag.write(pos);
      delay(50);
    }
    delay(250);
    for (pos = 175; pos > 50; pos -= 10)
    {
      flag.write(pos);
      delay(50);
    }
    for (pos = 20; pos < 90; pos += 2)
    {
      lidservo.write(pos);
      delay(50);
    }
    delay(2000);
    lidservo.write(50);
    delay(100);
    armservo.write(177);
    delay(250);
    armservo.write(50);
    delay(100);
    lidservo.write(90);
  }
}

diagram.json - for Wokwi.com

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 72, "left": -10.1, "attrs": {} },
    {
      "type": "wokwi-servo",
      "id": "servo1",
      "top": -78.8,
      "left": 182.4,
      "attrs": { "horn": "cross" }
    },
    {
      "type": "wokwi-servo",
      "id": "servo2",
      "top": -155.6,
      "left": 182.4,
      "attrs": { "horn": "cross" }
    },
    {
      "type": "wokwi-servo",
      "id": "servo3",
      "top": -2,
      "left": 182.4,
      "attrs": { "horn": "cross" }
    },
    { "type": "wokwi-slide-switch", "id": "sw1", "top": -149.2, "left": 89.5, "attrs": {} }
  ],
  "connections": [
    [ "servo3:PWM", "nano:3", "green", [ "h0" ] ],
    [ "servo1:PWM", "nano:5", "green", [ "h0" ] ],
    [ "servo2:PWM", "nano:6", "green", [ "h0" ] ],
    [ "nano:5V", "servo3:V+", "red", [ "v9.6", "h48", "v-86.4" ] ],
    [ "servo3:V+", "servo1:V+", "red", [ "h-19.2", "v-124.7" ] ],
    [ "servo1:V+", "servo2:V+", "red", [ "h-19.2", "v-124.7" ] ],
    [ "servo3:GND", "nano:GND.2", "black", [ "h0" ] ],
    [ "nano:GND.2", "servo1:GND", "black", [ "v0" ] ],
    [ "nano:GND.2", "servo2:GND", "black", [ "v0" ] ],
    [ "nano:GND.2", "sw1:3", "black", [ "v0" ] ],
    [ "sw1:2", "nano:2", "green", [ "v0" ] ],
    [ "servo2:V+", "sw1:1", "red", [ "h-86.4", "v0.1" ] ]
  ],
  "dependencies": {}
}

Thank you so much for trying everything. The 9v amps was definitely an issue and I agree it was causing the nano to reset. I swapped with 4AA and it plays thru the code except for the flag portion. I will test the servo again separately and make changes as needed but at least it doesnt quit mid movement anymore. Thanks again to everyone who helped

Thank you, I will read about this and make changes, any way to simplify the code is great. My plan is to sell the model and BOM and instructions for $5 on cults3d and donate all funds to the Detroit Rescue Mission. Then I'm going to pre-build a bunch of these as kits and donate to the local Children's Hospital where I hope they get interested and make their own programming on it too. Tables could make it easier then.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.