Steampunk Hat ! Need help

Hi !

Newbie here!

I've designed a Steampunk Tophat that I intend to wear to ComicCon in Stockholm in November.
I need a little help with the code.

Will post pictures when project is completed ! =)

I use EEPROM to store whether or not an object is active, simple 1 for active and 0 for inactive.
In SETUP I write all objects as inactive.
Firstly, the Power button on the remote must be pressed to do anything else.
When the Power button is pressed, the "Clock" is also activated.
Then both Power and Clock are marked as active by writing "1" to the corresponding EEPROM adresses.

Pressing button 1 will activate the Clock.
If it's already active, nothing happens.
If something else is active, that will be deactivated and then the Clock will be activated.

Pressing button 2 will activate the Light
and the same rules apply.

I'm using nesting IF functions to search for anything active that needs to be deactivated before something else is activated, and of course the EEPROM.

Only one object can be active at any given time.

The function for Power works, but not the others.

What I Need Help With:

Figuring out why only the power button works...

/*-------------------- Steampunk Hat --------------------*/

/*--------------- Components ---------------*/

// Freenove Control Board (supposedly the same as Arduino Uno)
// Stepper 28BYJ-48 as positioner
// Stepper 28BYJ-48 as activator
// Stepper Driver x2
// Servo SG90 as Iris Servo
// Servo SG90 as Secondary Servo

/*-------------------- Libraries --------------------*/

// Infrared Remote
#include <IRremote.h>
#include <IRremoteInt.h>

// EEPROM
#include <EEPROM.h>

// Stepper
#include <AccelStepper.h>

// Servo
#include <Servo.h>

/*-------------------- Variables --------------------*/

// IR Remote

int RECV_PIN = 2;                         // Infrared Receiving Pin (doesn't work for pin 1)
IRrecv irrecv(RECV_PIN);                  // Create a class object used to receive class
decode_results results;                   // Create a decoding results class object

// Steppers

#define MOTORWIRES 4
#define PosPin1  6
#define PosPin2  7
#define PosPin3  8
#define PosPin4  9
#define ActPin1  10
#define ActPin2  11
#define ActPin3  12
#define ActPin4  13

AccelStepper StepperPos(MOTORWIRES, PosPin1, PosPin3, PosPin2, PosPin4);
AccelStepper StepperAct(MOTORWIRES, ActPin1, ActPin3, ActPin2, ActPin4);

#define STEPS             32
int StepsRev            = 2048;

// Servos

Servo ServoIris;                              // Create Servo object to control the Iris Servo
int ServoIrisPin = 3;                         // Servo Pins must be PWM

Servo ServoSec;                               // Create Servo object to control the Secondary Servo
int ServoSecPin = 5;                          // Servo Pins must be PWM

// Stepper Positioner

int ClockPos             = 0 ;                // Absolute Positions in whole revolutions
int LightPos             = StepsRev * 1 ;
int SpeakerPos           = StepsRev * 2 ;
int CameraPos            = StepsRev * 3 ;
int UmbrellaPos1         = StepsRev * 4 ;
int UmbrellaPos2         = StepsRev * 5 ;
int GogglesPos           = StepsRev * 6 ;
int MaskPos              = StepsRev * 7 ;

// Stepper Activator

int ClockAct             = StepsRev * 2 ;     // Full rotations to Extend/Retract
int LightAct             = StepsRev * 1 ;
int SpeakerAct           = StepsRev * 1 ;
int CameraAct            = StepsRev * 1 ;
int UmbrellaAct1         = StepsRev * 1 ;
int UmbrellaAct2         = StepsRev * 1 ;
int GogglesAct           = StepsRev * 1 ;
int MaskAct              = StepsRev * 1 ;

// Servos

int IrisClosed       = 90;
int IrisOpen         = 0;
int ServoSecHome     = 0;

int ClockSer         = 10;
int LightSer         = 10;
int SpeakerSer       = 10;
int CameraSer        = 20;
int UmbrellaSer1     = 20;
int UmbrellaSer2     = 20;
int GogglesSer       = 20;
int MaskSer          = 20;

// Speeds

int SpeedPosMax      = 500;
int SpeedActMax      = 500;
int AccelPos         = 200;
int AccelAct         = 200;

// HEX values for remote

int PowerHEX             = 0xFFA25D;     // Power Button
int ClockHEX             = 0xFF30CF;     // Button 1
int LightHEX             = 0xFF18E7;     // Button 2
int SpeakerPosHEX        = 0xFF7A85;     // Button 3
int CameraPosHEX         = 0xFF10EF;     // Button 4
int UmbrellaHEX          = 0xFF38C7;     // Button 5
int GogglesHEX           = 0xFF5AA5;     // Button 6
int MaskHEX              = 0xFF42BD;     // Button 7
int ServoSecHEXplus      = 0xFF02FD;     // Button +
int ServoSecHEXminus     = 0xFF9867;     // Button -

// EEPROM

int PowerAdr             = 0;           // Address to store status of Power
int ClockAdr             = 1;           //                            Clock
int LightAdr             = 2;           //                            Light
int SpeakerAdr           = 3;           //                            Speaker
int CameraAdr            = 4;           //                            Camera
int Umbrella1Adr         = 5;           //                            Umbrella Position 1
int Umbrella2Adr         = 6;           //                            Umbrella Position 2
int GogglesAdr           = 7;           //                            Goggles
int MaskAdr              = 8;           //                            Mask

/*-------------------- SETUP --------------------*/

void setup()

  { // Open Setup

      Serial.begin(4800);                    // Initialize the serial port and set the baud rate to 4800

  // IR Receiver

      irrecv.enableIRIn();                   // Start the receiver

  // Servos

      ServoIris.attach(ServoIrisPin);        // Attaches the servo on servoPin to the servo object
      ServoSec.attach(ServoSecPin);          // Attaches the servo on servoPin to the servo object

  // Steppers

      StepperPos.setCurrentPosition(0);
      StepperPos.setMaxSpeed(SpeedPosMax);

      StepperAct.setCurrentPosition(0);
      StepperAct.setMaxSpeed(SpeedActMax);

      EEPROM.write(PowerAdr,0);
      EEPROM.write(ClockAdr,0);
      EEPROM.write(LightAdr,0);
      EEPROM.write(SpeakerAdr,0);
      EEPROM.write(CameraAdr,0);

  } // Close Setup

void loop()

  { // Open Loop

    if (irrecv.decode(&results))                             // Have we received a signal?

      { // Open Switch

        switch (results.value)

          { // Open Cases

      /*---------- Powering ON ----------*/

    case 0xFFA25D:                                     

      if  ((EEPROM.read(PowerAdr)) == 0)                         // IF Power is OFF

        { // Turn Power ON
        
            ServoIris.write(IrisOpen);                           // Open Iris
            delay(1000);

            StepperAct.setSpeed(SpeedActMax);
            StepperAct.setAcceleration(AccelAct);
            StepperAct.move(ClockAct);                           // Extend Clock
            StepperAct.runToPosition();
            delay(1000);

            ServoIris.write(IrisClosed);                         // Close Iris

            EEPROM.write(ClockAdr, 1);                           // Mark Clock as ACTIVE
            EEPROM.write(PowerAdr, 1);                           // Mark Power as ON

        } // Close IF

      else ((EEPROM.read(PowerAdr)) == 1);                       // IF Power is ON

        { // Turn Power OFF

              // powering off routine
          
        } // Close ELSE

    break;

/*---------- Activating the Clock ----------*/

    case 0xFF30CF:                                               // Receive the number "1"

      if  ((EEPROM.read(PowerAdr)) == 1)                         // If the Power is ON

        { // Open IF for Power

          if  ((EEPROM.read(ClockAdr)) == 0)                     // AND the Clock is INACTIVE

            { // Open IF for Clock
            
              if  ((EEPROM.read(LightAdr)) == 1)                 // Then continue

                { // Open IF for Light

                    StepperPos.setSpeed(SpeedPosMax);            // Go to Light
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(LightPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Light
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-LightAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(LightAdr, 0);                   // Mark Light as INACTIVE

                } // Close IF for Light

              else if ((EEPROM.read(SpeakerAdr)) == 1)           // IF Speaker is ACTIVE
            
                { // Open IF for Speaker
        
                    StepperPos.setSpeed(SpeedPosMax);            // Go to Speaker
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(SpeakerPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Speaker
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-SpeakerAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(SpeakerAdr, 0);                 // Mark Speaker as INACTIVE

                } // Close IF for Speaker

              else if ((EEPROM.read(CameraAdr)) == 1);           // IF Camera is ACTIVE
            
                { // Open IF for Camera

                    StepperPos.setSpeed(SpeedPosMax);            // Go to Camera
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(CameraPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Camera
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-CameraAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(CameraAdr, 0);                  // Mark Camera as INACTIVE

                } // Close IF for Camera

                StepperPos.setSpeed(SpeedPosMax);                // Go to Clock Position
                StepperPos.setAcceleration(AccelPos);
                StepperPos.moveTo(ClockPos);
                StepperPos.runToPosition();
                delay(1000);

                ServoIris.write(IrisOpen);                       // Open Iris
                delay(1000);

                StepperAct.setSpeed(SpeedActMax);                // Extend Clock
                StepperAct.setAcceleration(AccelAct);
                StepperAct.move(ClockAct);
                StepperAct.runToPosition();
                delay(1000);

                ServoIris.write(IrisClosed);                     // Close Iris

                EEPROM.write(ClockAdr, 1);                       // Mark Clock as ACTIVE

            } // Clock Active

        } // End of Case

    break;
        
/*---------- Activating the Light ----------*/

    case 0xFF18E7:                                               // Receive the number "2"

      if  ((EEPROM.read(PowerAdr)) == 1)                         // If the Power is ON

        { // Open IF for Power

          if  ((EEPROM.read(LightAdr)) == 0)                     // AND the Light is INACTIVE

            { // Open IF for Light
            
              if  ((EEPROM.read(ClockAdr)) == 1)                 // Then continue

                { // Open IF for Clock

                    StepperPos.setSpeed(SpeedPosMax);            // Go to Clock
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(ClockPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Clock
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-ClockAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(ClockAdr, 0);                   // Mark Clock as INACTIVE

                } // Close IF for Light

              else if ((EEPROM.read(SpeakerAdr)) == 1)           // IF Speaker is ACTIVE
            
                { // Open IF for Speaker
        
                    StepperPos.setSpeed(SpeedPosMax);            // Go to Speaker
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(SpeakerPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Speaker
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-SpeakerAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(SpeakerAdr, 0);                 // Mark Speaker as INACTIVE

                } // Close IF for Speaker

              else if ((EEPROM.read(CameraAdr)) == 1);           // IF Camera is ACTIVE
            
                { // Open IF for Camera

                    StepperPos.setSpeed(SpeedPosMax);            // Go to Camera
                    StepperPos.setAcceleration(AccelPos);
                    StepperPos.moveTo(CameraPos);
                    StepperPos.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisOpen);                   // Open Iris
                    delay(1000);

                    StepperAct.setSpeed(SpeedActMax);            // Retract Camera
                    StepperAct.setAcceleration(AccelAct);
                    StepperAct.move(-CameraAct);
                    StepperAct.runToPosition();
                    delay(1000);

                    ServoIris.write(IrisClosed);                 // Close Iris

                    EEPROM.write(CameraAdr, 0);                  // Mark Camera as INACTIVE

                } // Close IF for Camera

                StepperPos.setSpeed(SpeedPosMax);                // Go to Light Position
                StepperPos.setAcceleration(AccelPos);
                StepperPos.moveTo(LightPos);
                StepperPos.runToPosition();
                delay(1000);

                ServoIris.write(IrisOpen);                       // Open Iris
                delay(1000);

                StepperAct.setSpeed(SpeedActMax);                // Extend Light
                StepperAct.setAcceleration(AccelAct);
                StepperAct.move(LightAct);
                StepperAct.runToPosition();
                delay(1000);

                ServoIris.write(IrisClosed);                     // Close Iris

                EEPROM.write(LightAdr, 1);                       // Mark Light as ACTIVE

            } // Light Active

        } // End of Case

    break;

    irrecv.resume();                                             // Receive the next value

          } // Close Cases
        
      } // Close Switch

  } // Close Loop


// End of Program ! (for now...)

What did you want this bit to do?

        StepperAct.move(ClockAct);
        StepperAct.runSpeed();

.move(offset); means set the target position this far from the current position. So you want a relative move and not an absolute move? An absolute move would be .moveTo(position);.

'.runSpeed()' only moves one step. "You must call this as frequently as possible". You probably want ".runToPosition()" which steps until it completes the move. If you wanted an absolute position (.moveTo(position)) you could combine the two into: .runToNewPosition(position);

Your sketch doesn't use EEPROM.get(address, variable);. What error do you get when you try?

Thank You !!! It works now!

forgot to mention that StepperAct, which move first, moves relative.
The StepperPos moves in absolutes.

About the EEPROM. Will update the code when I get that error again.

Thanks again !

I feel like this page would be specifically helpful for you:

Thanks for the reply

I have read through this a few times and I just don't seem to understand it enough... :frowning:

I've updated the code to include the EEPROM.get :slight_smile:
and the new motor functions, which all seem to work now, except the ones that need to get data from the EEPROM

And if you want help with that, post your new, updated code so we are all on the same page.

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