Problem controlling a continuous rotation motor with a 4x3 keypad

Hello,
I am trying to create an elevator for my final project at school and I cannot get the motor to respond to the keypad. The keypad is connected and whenever I press a button the number shows up in the serial monitor. The issue is that whenever I upload the code to the arduino, the motor slowly turns even though I haven’t given it the command to turn yet. All I have done is connected the motor to the arduino and somehow the motor is getting power and turning without me coding it. Any help is much appreciated.

#include <Keypad.h>
#include <Servo.h>
Servo myservo;

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {8, 7, 6, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 3, 2};

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '*':
        //digitalWrite(ledpin, LOW);
        break;
      case '#':
        //digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

What motor, motor power supply and motor driver are you using?
Post a diagram (not Fritzing) showing how everything is wired up.

PS: you should never power a motor or servo from the Arduino.

From what you've written, I assume that you're using a continuous-rotation servo. (Please correct me if I'm wrong.)

You attach it, but don't write a value telling it to remain stationary. I'm only surprised that it's turning slowly rather than quickly.
(As soon as you 'attach()' the servo, the Arduino begins sending it pulses.)
For your servo to remain stationary, you need to either:-

myServo.write(90);

or

myServo.writeMicroseconds(1500);

And even then, you might need to alter the value slightly to get it to stop altogether, unless there's a trim pot that you have access to for fine adjustment.

Edit: On power-up, it's a good idea to write the initial value before calling 'attach()' to begin pulsing the servo.

I have attached a picture of the continous rotation servo motor below and the power supply is given by the arduino and its connected to 5V. I will upload a picture of the entire arduino and its connections tomorrow as I left my project at school.

Your picture is invisible.

And as mentioned by jremington, you should not power a servo from the Arduino. A separate power supply is necessary.

UsmanJ:
I have attached a picture of the continous rotation servo motor below and the power supply is given by the arduino and its connected to 5V. I will upload a picture of the entire arduino and its connections tomorrow as I left my project at school.

Here is a link to the picture of the servo motor because it wouldnt let me attachthe file.

https://www.google.ca/search?q=continuous+rotation+servo&espv=2&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjDy7yJjJTKAhXLlR4KHbMfC-oQ_AUICCgC&biw=1366&bih=623#imgrc=n4B78B37rHhU8M%3A

I mistakenly called it just a motor but its a continuous rotation servo motor.

Here's the picture. It's a Futaba S148 standard continuous-rotation servo.
( @UsmanJ, there's an "Insert an image" button at the top of the "Reply" window. )

On the webpage that the pic comes from, it says "Position "0" (1.5ms pulse) is stop, "90" (2ms pulse) is full speed forward, "-90" (1ms pulse) is full speed backwards."

Using the standard "Servo" library, this is not true. The values range from 0 to 180, with 90 being 'stop', as I mentioned earlier.
They even link to an example sketch, "Sweep", which uses the 0 to 180 values.

In short, everything that has been said so far is correct. ie. Write 90 to the servo to stop it, and you cannot power it directly from the Arduino 5V rail.

Edit: And apparently, it does have a trimpot to adjust the speed to 'stopped' when 1500uS pulses are received.
If you write a short sketch that writes 90 to the servo using the "Servo" library, you can then adjust the trimpot until the servo stops turning.

Thanks alot :), but how can I connect the servo to power without using the arduinos 5V input?

UsmanJ:
Thanks alot :), but how can I connect the servo to power without using the arduinos 5V input?

You mean 5V output.

You need to use a separate 5V or 6V supply for the servo. A servo draws too much current for the Arduino's regulator or the computer's USB port, and also can place spikes on the Arduino 5V rail that can cause unpredictable behaviour like unexpected resets.
Whatever you use to power the project via the DC input jack or Vin terminal could also provide power to a small switching DC to DC converter, which could in turn provide power to the servo. Then you would connect the DC to DC converter's ground line to Arduino ground and connect the signal line from the Arduino to the servo as usual. (That's how I do it.)
You can get small 5V or adjustable switching regulators on eBay for a few dollars.
Something like this:- DC 4-38V to 1.25-36V Step-Down(Buck) Converter 5V/12/24V 5A Power Supply Module
You'll probably find even cheaper ones, that's just an example.

So I was able to make the servo stop moving by inputing the myservo.write (90) and now when I press 1 and 2 on the keypad the servo moves the way I want it too but when I press button 1 it does what I coded for it and what I coded for button 2 and i cant figure out why. When I press button 1 it should just turn 180 for 5 seconds and then stop but instead it turns 180 for 5 seconds and then turns 0 for 5 seconds and then stops.

#include <Keypad.h>
#include <Servo.h>
Servo myservo;

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns


// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {8, 7, 6, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 3, 2};

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  myservo.write(90);
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    Serial.println(key);
    switch (key)
    {
      case '1':
        {myservo.write(180);
        delay(5000);}
      
      case '2':
        {myservo.write(0);
        delay(5000);}
        
      case '*':
        //digitalWrite(ledpin, LOW);
        break;
      case '#':
        //digitalWrite(ledpin, HIGH);
        break;
      //default:
        
    }
  }
}

You can also use a 4xAA battery pack to power the servo. Connect the negative battery lead to the Arduino ground.

when I press button 1 it does what I coded for it and what I coded for button 2 and i cant figure out why.

You need to terminate each case within the switch case statement with

break;

Okay I will try that tomorrow, many thanks :slight_smile: