Keypad + Stepper Motor+WW LED strip = Secret Lighted Closet Cabinet

Good afternoon everyone :slight_smile:

I want to thank you for taking the time to read this, and also help me if you are. I know I have read countless forum topics regarding some extremely similar situations as mine, however, I cannot figure out how to understand the creation of this code. I read the tutorial sticky on how to go about preparing an arduino program by Robin2, and I'm still having some difficulty proceeding.

I don't have the arduino or stepper yet (they come in the mail tomorrow), however I know I need some direction on how to go about coding this. So I know the actions I need the arduino code to contain, and they are as follows I believe:
checking the keypad for inputs, recording the sequence of inputs from the keypad, turning the stepper motor Clockwise (opening), turning the LED strip on, turning the stepper motor Counter-clockwise (closing), and turning the LED strip off.

This is where my questions start to go crazy. Do I have all the right "actions" in my list of actions that I need, did I miss a few, or did I name any incorrectly? How would I word these into functions for the code?

I'm a little shy when it comes to arduino and coding since I am just starting off, but this is information I have been trying to learn, and I would really appreciate someone helping me have that "aha!" moment :slight_smile:

Many many thanks for your input!

Anselmo Crespin

look for examples for keypad input (based on the type you bought)

look for examples on entering a password and comparing it.

look for examples on how to make a stepper motor turn, including controlling the speed.

break the problem into small parts then solve each one. Once you can make the basic parts work then you can start to add them together to make the final program.

people on here may seem rude and a little short fussed but they really are a great bunch of guys. If you expect them to write the program then go to the pay for programming section. If you want to learn then you are in the right place. Please read how to post and use the forum. More people will help when they can quickly read your code rather than having to download it or copy and paste from the browser. Post code between the code tags and alt+t the program before copying it to the site will allow people on mobile phones to read it.

ACrespin124:
So I know the actions I need the arduino code to contain, and they are as follows I believe:
checking the keypad for inputs, recording the sequence of inputs from the keypad, turning the stepper motor Clockwise (opening), turning the LED strip on, turning the stepper motor Counter-clockwise (closing), and turning the LED strip off.

This is where my questions start to go crazy.

This suggests to me that you are thinking of your project as a single complex program. Don't.

Start by writing separate short programs to learn how to do each of the pieces you mention.
When you can do each of the separately it will be time to "join" them together in the way that all the functions are joined together in planning and implementing a program

And f you have problems while you are learning it will be much easier to get advice here with small programs.

...R

Thank you both for your extremely generous replies, I really appreciate the time you took to respond.
My arduino and motor come in today, so I'm hoping that by the end of this weekend (going on vacation first), I will have the separate parts working, or at least the easier-seeming ones (LED strip on/off, stepper motor control). I will take the time I have now to solidify my knowledge and have an idea of where to start with what you gave me :slight_smile:

Okay, so thank you for your patience and your help again in advance, as it helped me take my project a long way!

Now I have all the parts, and everything connected together correctly. Tested my LED strip with a fade on and fade off function, and it works great with the TIP120 transistor I used to control it from one of the PWM pins :slight_smile:

So, now I’m having a problem with my main projects code, I’ll give a quick overview of what’s on each pin, then go into the code:

Tx/Rx - nothing
2 thru 8 - Keypad 4x3
9 - TIP120 Transistor for 12V LED strip
10 thru 13 - Stepper motor control
GNDs - One is with the TIP120, another with a 12v Fan
Vin - 12v Fan (draws only .1amps, its tiny for the Stepper motor control)

My code problem is SO SMALL for the length of the code and the modifications I’ve been making to it. I used the base of this code from another project slightly similar from adafruit.com, though I slightly modified the language and pins to fit my needs. The bottom part was already included and seemed to work before, however, now it’s giving this error.

#include <Keypad.h>
#include <Stepper.h>
 
// Define the states for the lock state machine
#define LOCKED 2
#define UNLOCKED 0
#define STEPS 50
 
// State Variables:   Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;
 
// Define your password key sequence here
char* secretCode = "2301000";
 
// Keypad key matrix:
const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = 
{
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'},
   {'*','0','#'}
};
 
// Keypad pin definitions
byte rowPins[rows] = {5, 6, 7, 8}; 
byte colPins[cols] = {2, 3, 4};  
 
// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
 
// More pin definitions:
int transistorPin = 9;

int in1Pin = 10;
int in2Pin = 11;
int in3Pin = 12;
int in4Pin = 13;

Stepper MyStepper(STEPS, in1Pin, in2Pin, in3Pin, in4Pin); 
 
void setup()                    
{
   pinMode(transistorPin, OUTPUT);
   
   pinMode(in1Pin, OUTPUT);
   pinMode(in2Pin, OUTPUT);
   pinMode(in3Pin, OUTPUT);
   pinMode(in4Pin, OUTPUT);
     
   // Initialize state and communicatins
   setLockState(LOCKED); 
   Serial.begin(9600);
   MyStepper.setSpeed(30);
}
 
 
void loop()                    
{
   // Run the state machine:
   
   // Locked State - Monitor keypad for valid Password code entry
   if (LockState == LOCKED)
   {
      char key = keypad.getKey();
 
      if (key == '*' || key == '#')
      {
         position = 0;
         setLockState(LOCKED);
      }
      if (key != 0)
      {
         if (key == secretCode[position])  // Valid key in Password sequence
         {
            Serial.print("Matched ");   
            Serial.print(key);   
            Serial.print("-at-");   
            Serial.println(position);   
            position ++;
         }
         else  // Invalid key - start all over again
         {
            Serial.println("Invalid Code!");   
            position = 0;
         }
      }
 
     
 
      if (position == 7)  // Password successfully entered - advance state
      {
         setLockState(UNLOCKED);
         for (int brightness = 0; brightness < 255; brightness++) {
           analogWrite(transistorPin, brightness);
           delay(10);
    }
         position = 0;
      }
      delay(100);
   }
 
   // UNLOCKED state - hold the solenoid open for a limited time
   else if (LockState == UNLOCKED)
   { 
      for (int i = 0; i < 30; i++)
      {
      for (int brightness = 255; brightness >= 0; brightness--) {
        analogWrite(transistorPin, brightness);
          delay(10);
      }
      
      delay(2000);
      setLockState (LOCKED);  // Time-out - go back to the locked state.
   }
}
 
 
 
 
 
// Set the state and the time of the state change
void setLockState(int state)
{
   LockState = state;
   StartTime = millis ();
   if (state == LOCKED)
   {
      Serial.println("Locked!");
      MyStepper.step(-STEPS); 
   }    
   
   else if (state == UNLOCKED)
   {
      Serial.println("Unlocked!");
      MyStepper.step(STEPS);      
   }
}

The error in the above code is as follows:

Arduino: 1.6.1 (Windows 8.1), Board: ‚ÄúArduino Uno‚ÄĚ

Cabinet_Compiling.ino: In function ‚Äėvoid loop()‚Äô:

Cabinet_Compiling.ino:127:1: error: a function-definition is not allowed here before ‚Äė{‚Äô token

Cabinet_Compiling.ino:141:1: error: expected ‚Äė}‚Äô at end of input

Error compiling.

You are missing a } to close off the loop() function.

If you are careful about how you indent your code that sort of problem will be obvious.

When I start a function I always put in the closing braces before I put in the content

void loop() {

}

and only then add the content

Tx/Rx - nothing

Wrong:

   Serial.begin(9600);

I have a very quick question about my arduino connection to my keypad (4x3 matrix, 7 pins, specifically this one: Amazon.com 3x4 Matrix).

I connected arduino pins 0-5 to keypad pins 1-6, respectively, and arduino pin 7 to keypad pin 7.

In the serial, I am able to read 3, 6, 9, and # from the keypad when I push those buttons, but no other buttons work. I tested the resistance of the keypad's connections initially and there seemed to be different resistance values for each column of buttons. This would lead me to believe I need a resistor for each of those pins, but is this correct? Should I just ditch the idea of using the 7 pins and go with the 5v+Analog+Gnd with the 7-resistor array, or is what I am currently trying to do feasible? If so, please shed some light, as I would love to continue on with my project :))

That's because pins 0 and 1 are used by the serial connection to the computer. You can't use them for the keypad too. That's why the example code above used pins 2~8, not using 0/1.

In a normal keypad setup (ie, not doing the resistor ladder trick), you don't need any resistors other than the pull-ups.