Problem where my code is doing the opposite of what I want? (servo motor & keypad)

Have u earlier check the both the components the motor and the keypad are they working well or not

Yes, I did individual test and they both seemed to work quite well alone, the servo moved fine and the keypad worked well

Check then while u are running the project no button is pressed of the keypad as it normally dosen't happened if you assign to do or run a specific task when a button pressed and it is doing opposite

Could You Please share your Circuit Diagram

NO!
here you have a mis-understanding of "power"
power means voltage multiplied with current
Power = voltage * current

imagine 10 supertiny digital-watch-batteries 3 mm diameter 1 mm thick.
each has 1,5V put ten of them behind each other which results in a voltage of 15V

try to power a servo with this 15V the servo will do a 1mm small "not-move" and that's all
because those super-tiny batteries deliver not enough current.

imagine a single-cell lipo-accu of 10000 mAh with only 3,7V
If you blockade the servo from turning. The servo will burn through on just 3,7V because the current this big 10000 mAh-lipo is able to deliver is 150 A.

Your servo will not pull 150A maybe 2A or 3A but this is a too high current which will make the motor and / or the servo-electronic overheat and destroy.

Be the change you want to see in the world
best regards Stefan

Hello all, this is me on another account cause Arduino limited the amount of replies I could have.
I had question:
I could replace all the equipment and gear to properly work when I get back to school, However, do you think that the code works (are there any glaring issues)? or is it impossible to tell without physical tests?
Here is the code:

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

const byte ROWS = 4;
const byte COLS = 4;

// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Servo Myservo;

void setup() {
  Serial.begin(9600);
  Myservo.attach(8);
  Serial.print("test");
}


void loop() {
  char customKey = customKeypad.getKey();

  if (customKey != NO_KEY) {
    Serial.println(customKey);
    Serial.println ( " button pressed");
    Myservo.writeMicroseconds(180);
  } else Myservo.writeMicroseconds(0);
}

creating a second account is against the forum-rules.
You are in danger to get banned for 14 days if you creat a sock-puppet account.
Then you have the opposite of yhat you wanted.

Delete this second account by yourself. otherwise a moderator will do so.

To be able to post more do this:
Get to trust level 1 by…

  • Entering at least 5 topics (show them on your screen)
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

You can use the WOKWI-simulator to do some more tries in getting your code to work

Be the change you want to see in the world
best regards Stefan

I shall delete the account now, this is just a bit of emergency so sorry, thanks for the heads up!
Why does my servo keep twitching, after using wokwi to simulate it the code, the servo is constantly twitching for no reason it seems, do you think there is something in the code causing this?

Sure there are multiple things:

you are using IO-pin 8 for the keypad and for the servo

you are using writeMicroseconds() with values that are suitable for write()

loop is looping very fast thousands of time per second
with each iteration of loop the keypad gets read new
The keypad-library creates only one time a char
After that in the next iteration it returns NO_KEY again
This is why your servo is twitching.

It is a bad idea to just throw together two codes with low knowledge.
You have to examine how a single library works .

Using delay() is a even worser idea. This would make your code unresponsive and you would have to press the button in exact that super-short timeslice where the button-press starts. If you are 0,01 seconds too late a code with a delay would be in the next delay()

I will introduce you to how non-blocking-timing and using functions to structure your code works.

A functionality of

  • press a key on the keyboard
  • then move servo to new position
  • wait some time at this position
  • then go back to zero-position

is shown in this code

Of course the keypad library offers more functions than just getKey()

If you want to have a functionality like
turn servo to 180 degree (not 180 microseconds) as long as the key is pressed down
and turn back servo if key is releaed
you have to use different functions which you can look up in the reference for the keypad-library
https://playground.arduino.cc/Code/Keypad/

Be the change you want to see in the world
best regards Stefan

Thank you so much for your help, this makes so much more sense now. is there a way for me to make it so only when one button is pressed, ex: only if the button 1 is pressed

Sure.
Check with an if-condition which key is pressed.
I will not write in more detail how this can be coded. This is pretty simple. You have to try it on your own.

If you post a own attempt where you have tried to code this and have a specific question why it does not yet work I will answer this question.

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