Ok i try this thanks
Okay I did this with this code.
I hope to be what you said.
Attach a snapshot
[code]
/* Keypadtest.pde
*
* Demonstrate the simplest use of the keypad library.
*
* The first step is to connect your keypad to the
* Arduino using the pin numbers listed below in
* rowPins[] and colPins[]. If you want to use different
* pins then you can change the numbers below to
* match your setup.
*
*/
#include <Keypad.h>
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
// Variables to hold Distance and CurrentPosition
int keyfullnumber=0; // used to store the final calculated distance val
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 22, 24, 26, 28 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 31, 33, 35, 37 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//#define ledpin 13
void setup()
{
//pinMode(ledpin,OUTPUT);
//digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}
void loop(){
char keypressed = keypad.getKey(); // Get value of keypad button if pressed
if (keypressed != NO_KEY){ // If keypad button pressed check which key it was
switch (keypressed) {
case '1':
checknumber(1);
break;
case '2':
checknumber(2);
break;
case '3':
checknumber(3);
break;
case '4':
checknumber(4);
break;
case '5':
checknumber(5);
break;
case '6':
checknumber(6);
break;
case '7':
checknumber(7);
break;
case '8':
checknumber(8);
break;
case '9':
checknumber(9);
break;
case '0':
checknumber(0);
break;
case '*':
deletenumber();
break;
case '#':
calculatedistance();
break;
}
}
}
void checknumber(int x){
if (firstnumber == 99) { // Check if this is the first number entered
firstnumber=x;
String displayvalue = String(firstnumber); // Transform int to a string for display
Serial.println(displayvalue); // Redraw Nokia lcd
} else {
if (secondnumber == 99) { // Check if it's the second number entered
secondnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber));
Serial.println(displayvalue);
} else {
if (thirdnumber == 99) { // Check if it's the third number entered
thirdnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
Serial.println(displayvalue);
} else { // It must be the 4rd number entered
fourthnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber)+ String(fourthnumber));
Serial.println(displayvalue);
}
}
}
}
void deletenumber() { // Used to backspace entered numbers
if (fourthnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
Serial.println(displayvalue);
fourthnumber=99;
}
else
if (thirdnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber));
Serial.println(displayvalue);
thirdnumber=99;
}
else {
if (secondnumber !=99) {
String displayvalue = String(firstnumber);
Serial.println(displayvalue);
secondnumber=99;
}
else {
if (firstnumber !=99) {
String displayvalue = "";
Serial.println(displayvalue);
firstnumber=99;
}
}
}
}
void calculatedistance() { // Used to create a full number from entered numbers
if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99 ) {
keyfullnumber=firstnumber;
Serial.println(keyfullnumber);
}
if (secondnumber != 99 && thirdnumber == 99 && fourthnumber== 99) {
keyfullnumber=(firstnumber*10)+secondnumber;
Serial.println(keyfullnumber);
}
if (thirdnumber != 99 && fourthnumber==99) {
keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
Serial.println(keyfullnumber);
}
if (fourthnumber != 99) {
keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
Serial.println(keyfullnumber);
}
//resetnumbers(); // Reset numbers to get ready for new entry
{
Serial.println(keyfullnumber);
}
}
[/code]
Robin2:
That seems like it just detects a single key and I think you want to build up a larger number with 3 or 4 key presses (such as 1600).Try extending that example code to do that.
Alternatively take a copy of the keypad code out of your other program and make it into a short test program.
...R
and another one
You have posted Replies 21, 22 and 23 but you have not said whether the code does what you want or whether you understand it.
Here are the three images. See this Image Guide
Please don't post that sort of thing as pictures. Just copy and paste the text. I can't read either of the first two, and in the absence of your explanation I don't know what the 3rd shows.
...R
Robin2:
You have posted Replies 21, 22 and 23 but you have not said whether the code does what you want or whether you understand it.Please don't post that sort of thing as pictures. Just copy and paste the text. I can't read either of the first two, and in the absence of your explanation I don't know what the 3rd shows.
...R
1}The KEYBOARD code do what I want.
my problem is when I run the code of the first post .
Im trying to add a 4 digit to the code, it works but somthing strange happen if i go over 1640.
The stepper moves as it needs to all the way up to 1640 mm, if i add exemple 1650mm the stepper moves the outher way
Do i need to add somthing to use 4digit to the code to make it work like it does from 0 to 1640?
2)On 3rd picture you can see :
void checknumber(int x)
the first entered nbr
the second entered nbr
the third entered nbr
the fourth entered nbr
then:
void calculatedistance()
i push # (is enter) to send stepper at 2354mm
then:
void resetnumbers()
the code reset all entered nbrs for new entry
then i try the:
void deletenumber()
i entered four nbrs and try to delete this nbrs one by one
delete * and its work
then:
void calculatedistance()
i enter two nbr and #(enter) to send stepper at 89mm
then i enter four nbr and #(enter) to send stepper at 1650mm
i test only the keypand code as you tell me.
but if add all code together i have the first problem
i hope to understand these I write
the code i use to control the stepper is in my first post.
thank you for help me
KOSTAS
When you enter 4 values and get 1650, you then call the function that contains:
long calculatedmove=(z*1600)/80; // Calculate number of steps needed in mm
1650 * 1600 is 2640000 which does not fit in an int. If z were a long, or 1600 was 1600UL, then the calculation would be done using longs, and the result stored in a long register, where it would fit. As is, though, the calculation is done using ints, and the result stored in an int register, where it does not fit.
PROFILCA:
1}The KEYBOARD code do what I want.
my problem is when I run the code of the first post .
If your keypad program allows you to enter any value from 0 to 9999 then you have a solid basis for providing data for the stepper part of the program.
I assume your keyboard program can store the value in a variable. Are you using the same keyboard code in the program with the stepper motor? If not, you should be.
...R
Yes its the same keyboard code in the program with the stepper motor
PROFILCA:
Yes its the same keyboard code in the program with the stepper motor
Can you please now post two programs.
{A} the program that just reads the keyboard and shows the result on the Serial Monitor
{B} the stepper program that includes the code in {A}
Hopefully then we will be able to sort out the remaining problems quickly.
...R
[code]
/* Keypadtest.pde
*
* Demonstrate the simplest use of the keypad library.
*
* The first step is to connect your keypad to the
* Arduino using the pin numbers listed below in
* rowPins[] and colPins[]. If you want to use different
* pins then you can change the numbers below to
* match your setup.
*
*/
#include <Keypad.h>
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
// Variables to hold Distance and CurrentPosition
int keyfullnumber=0; // used to store the final calculated distance val
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 22, 24, 26, 28 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 31, 33, 35, 37 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//#define ledpin 13
void setup()
{
//pinMode(ledpin,OUTPUT);
//digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}
void loop(){
char keypressed = keypad.getKey(); // Get value of keypad button if pressed
if (keypressed != NO_KEY){ // If keypad button pressed check which key it was
switch (keypressed) {
case '1':
checknumber(1);
break;
case '2':
checknumber(2);
break;
case '3':
checknumber(3);
break;
case '4':
checknumber(4);
break;
case '5':
checknumber(5);
break;
case '6':
checknumber(6);
break;
case '7':
checknumber(7);
break;
case '8':
checknumber(8);
break;
case '9':
checknumber(9);
break;
case '0':
checknumber(0);
break;
case '*':
deletenumber();
break;
case '#':
calculatedistance();
break;
}
}
}
void checknumber(int x){
if (firstnumber == 99) { // Check if this is the first number entered
firstnumber=x;
String displayvalue = String(firstnumber)+String("-first number"); // Transform int to a string for display
Serial.println(displayvalue); // Redraw screen
} else {
if (secondnumber == 99) { // Check if it's the second number entered
secondnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber)+String("-secondnumber"));
Serial.println(displayvalue);
} else {
if (thirdnumber == 99) { // Check if it's the third number entered
thirdnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber)+String("-thirdnumber"));
Serial.println(displayvalue);
} else { // It must be the 4rd number entered
fourthnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber)+ String(fourthnumber)+String("-fourthnumber"));
Serial.println(displayvalue);
}
}
}
}
void deletenumber() { // Used to backspace entered numbers
if (fourthnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber)+String(" - i have delete one number"));
Serial.println(displayvalue);
fourthnumber=99;
}
else
if (thirdnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber)+String(" - i have delete two numbers"));
Serial.println(displayvalue);
thirdnumber=99;
}
else {
if (secondnumber !=99) {
String displayvalue = (String(firstnumber)+String(" - i have delete three numbers"));
Serial.println(displayvalue);
secondnumber=99;
}
else {
if (firstnumber !=99) {
String displayvalue = (String(" - i have delete all numbers"));
Serial.println(displayvalue);
firstnumber=99;
}
}
}
}
void calculatedistance() { // Used to create a full number from entered numbers
{
String displayvalue = "";
Serial.println("I PUSH ENTER");
}
{ String displayvalue = (String(" - go to distance"));
Serial.println(displayvalue);
}
if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99 ) {
keyfullnumber=firstnumber;
Serial.println(keyfullnumber);
}
if (secondnumber != 99 && thirdnumber == 99 && fourthnumber== 99) {
keyfullnumber=(firstnumber*10)+secondnumber;
Serial.println(keyfullnumber);
}
if (thirdnumber != 99 && fourthnumber==99) {
keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
Serial.println(keyfullnumber);
}
if (fourthnumber != 99) {
keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
Serial.println(keyfullnumber);
}
{
//Serial.println(keyfullnumber);
//Serial.println("DONE")
}
{
resetnumbers(); // Reset numbers to get ready for new entry
}
}
void resetnumbers() { // Reset numbers for next entry
firstnumber=99;
secondnumber=99;
thirdnumber=99;
fourthnumber=99;
{
String displayvalue = "";
Serial.println("ALL NUMBERS RESET- READY FOR NEW ENTRY");
}
}
[/code]
This is keypand test
[code]
/* Arduino Control Stepper with Keypad and LCD
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include <AccelStepper.h> // AccelStepper Library
#include <Keypad.h> // Keypad Library
#include "U8glib.h" // U8glib for Nokia LCD
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
// Variables to hold Distance and CurrentPosition
int keyfullnumber=0; // used to store the final calculated distance value
String currentposition = ""; // Used for display on Nokia LCD
// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Keypad Library definition
// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7); // Arduino pins connected to Nokia pins:
// CLK=3, DIN=4, CE=6, DC=5, RST=7
// AccelStepper Setup
AccelStepper stepper(1, A0, A1); // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver
#define Limit01 2
const int ledPin1 = 9; // <======= the number of the LED REDpin
const int ledPin2 = 8; // <======= the number of the LED GREEN pin
int step_speed = 2; // Speed of Stepper motor (higher = slower)on Homing mode
void setup(void) {
{
pinMode(Limit01, INPUT);
pinMode(A1, OUTPUT);
pinMode(A0, OUTPUT);
//pinMode(pot_pin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
while (!digitalRead(Limit01))
{
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);//LOW=clocwise/high=anti-clocwise
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(step_speed);
digitalWrite(A0, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
delay(step_speed);
}
}
// Light up the LCD backlight LEDS
analogWrite(backlight_pin, 80); // Set the Backlight intensity (0=Bright, 255=Dim)
// AccelStepper speed and acceleration setup
stepper.setMaxSpeed(1200); // Not to fast or you will have missed steps
stepper.setAcceleration(200); // Same here
// Draw starting screen on Nokia LCD
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0,10, "ENTER DISTANCE");
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.drawStr(57, 47, "Home"); //when hit Home switch
}
while( u8g.nextPage() );
}
void loop(){
char keypressed = keypad.getKey(); // Get value of keypad button if pressed
if (keypressed != NO_KEY){ // If keypad button pressed check which key it was
switch (keypressed) {
case '1':
checknumber(1);
break;
case '2':
checknumber(2);
break;
case '3':
checknumber(3);
break;
case '4':
checknumber(4);
break;
case '5':
checknumber(5);
break;
case '6':
checknumber(6);
break;
case '7':
checknumber(7);
break;
case '8':
checknumber(8);
break;
case '9':
checknumber(9);
break;
case '0':
checknumber(0);
break;
case '*':
deletenumber();
break;
case '#':
calculatedistance();
break;
}
}
}
void checknumber(int x){
if (firstnumber == 99) { // Check if this is the first number entered
firstnumber=x;
String displayvalue = String(firstnumber); // Transform int to a string for display
drawnokiascreen(displayvalue); // Redraw Nokia lcd
} else {
if (secondnumber == 99) { // Check if it's the second number entered
secondnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber));
drawnokiascreen(displayvalue);
} else {
if (thirdnumber == 99) { // Check if it's the third number entered
thirdnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
drawnokiascreen(displayvalue);
} else { // It must be the 4rd number entered
fourthnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber)+ String(fourthnumber));
drawnokiascreen(displayvalue);
}
}
}
Serial.println(keyfullnumber);
}
void deletenumber() { // Used to backspace entered numbers
if (fourthnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
drawnokiascreen(displayvalue);
fourthnumber=99;
}
else
if (thirdnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber));
drawnokiascreen(displayvalue);
thirdnumber=99;
}
else {
if (secondnumber !=99) {
String displayvalue = String(firstnumber);
drawnokiascreen(displayvalue);
secondnumber=99;
}
else {
if (firstnumber !=99) {
String displayvalue = "";
drawnokiascreen(displayvalue);
firstnumber=99;
}
}
}
}
void calculatedistance() { // Used to create a full number from entered numbers
if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99 ) {
keyfullnumber=firstnumber;
movestepper(keyfullnumber);
}
if (secondnumber != 99 && thirdnumber == 99 && fourthnumber== 99) {
keyfullnumber=(firstnumber*10)+secondnumber;
movestepper(keyfullnumber);
}
if (thirdnumber != 99 && fourthnumber==99) {
keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
movestepper(keyfullnumber);
}
if (fourthnumber != 99) {
keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
movestepper(keyfullnumber);
}
resetnumbers(); // Reset numbers to get ready for new entry
}
void movestepper(int z) { // Move the stepper
int calculatedmove=((z*1600)/80); // Calculate number of steps needed in mm
stepper.runToNewPosition(calculatedmove);
currentposition = String(z);
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0, 10, "ENTER DISTANCE");
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.setPrintPos(57,47);
u8g.print(currentposition);
}
while( u8g.nextPage() );
}
void resetnumbers() { // Reset numbers for next entry
firstnumber=99;
secondnumber=99;
thirdnumber=99;
fourthnumber=99;
}
void drawnokiascreen(String y) {
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0, 10, "ENTER DISTANCE");
u8g.setPrintPos(0,29);
u8g.print(y); // Put entered number on Nokia lcd
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.setPrintPos(57,47);
u8g.print(currentposition); // Display current position of stepper
}
while( u8g.nextPage() );
}
[/code]
and this is all code
PaulS:
When you enter 4 values and get 1650, you then call the function that contains:long calculatedmove=(z*1600)/80; // Calculate number of steps needed in mm
1650 * 1600 is 2640000 which does not fit in an int. If z were a long, or 1600 was 1600UL, then the calculation would be done using longs, and the result stored in a long register, where it would fit. As is, though, the calculation is done using ints, and the result stored in an int register, where it does not fit.
You mean this;
long calculatedmove=(z*1600UL)/80;
I try this it's the same as before
PROFILCA:
This is keypand test
I can't immediately see what is the name of the variable that holds the value entered on the keypad. Can you tell me what it is called please.
...R
Robin2:
I can't immediately see what is the name of the variable that holds the value entered on the keypad. Can you tell me what it is called please....R
probably is this.
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
If it helps and if you have time you can take a look to this video on youtube https://youtu.be/b_bA5Ha_mec i have take the code from here and then i have modify it
PROFILCA:
probably is this.
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int fourthnumber=99;
That is what I suspected. But you need to take that a step further and convert those four values into a single variable. The code to do that might be something like
int combinedVal;
combinedVal = firstnumber * 1000 + secondnumber * 100 + thirdnumber * 10 + fourthnumber;
...R
Robin2:
int combinedVal;
combinedVal = firstnumber * 1000 + secondnumber * 100 + thirdnumber * 10 + fourthnumber;
...R
void calculatedistance() { // Used to create a full number from entered numbers
if (fourthnumber == 99 && thirdnumber == 99 && secondnumber == 99 && firstnumber != 99 ) {
keyfullnumber=firstnumber;
movestepper(keyfullnumber);
}
if (secondnumber != 99 && thirdnumber == 99 && fourthnumber== 99) {
keyfullnumber=(firstnumber*10)+secondnumber;
movestepper(keyfullnumber);
}
if (thirdnumber != 99 && fourthnumber==99) {
keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
movestepper(keyfullnumber);
}
if (fourthnumber != 99) {
keyfullnumber=(firstnumber*1000)+(secondnumber*100)+(thirdnumber*10)+fourthnumber;
movestepper(keyfullnumber);
that is not correct?
that is not correct?
Have you tried printing keyfullnumber? Does it contain the value you expect?
Have you fixed the issue with movestepper()'s overflow?
Your camel still needs a drink of water.
PaulS:
Have you tried printing keyfullnumber? Does it contain the value you expect?
Yes and all it's ok
PaulS:
Have you fixed the issue with movestepper()'s overflow?
void movestepper(int z) { // Move the stepper
long calculatedmove=((z*1600)/80); // Calculate number of steps needed in mm
stepper.runToNewPosition(calculatedmove);
currentposition = String(z);
you mean this?
you mean this?
Yes. For what values of z can you multiply by 1600 and not get an overflow? 32768/1600 = 20. So, it you pass a value greater than 20 to the function, you will get an overflow, and the code will NOT do what you expect.