Go Down

Topic: Change a stepping value with 4x4 keypad (Read 132 times) previous topic - next topic

Brinarpi

Dear Everyone

I have a project for power press feeder with stepper motor. Thanks to Robin I have the first part done. There is 2 Arduino's connected. The Slave is only for stepping the motor, and the master is for keypad, and LCD 16/2 in order to change the stepps value(if the correction is needing).

The Sender has next code:
Code: [Select]
void setup() {
 Serial.begin(9600);

}

void loop() {
int stepsToGo = 8000;
Serial.print( '<');  // the start marker
Serial.print(stepsToGo);
Serial.println('>');  // end marker

 delay(1000) ;

}


What I need to do is to change the int stepsToGo = ?; value with a keypad 4x4 or with the push buttons (up/down) for me its the same, and to have an LCD 16/2 for monitoring the steps value.

I try to search similar sketch, but I dont find it. (becouse I am newbe in Arduino some part of the codes I dont understand clearly)

Did someone help to me? Thanks in advance.

PaulS

Where is the code to define the keypad object, and connect it to the appropriate pins?

Where are you reading the keypad?

While handling the numeric keys is straight-forward, you need to have some way of indicating that the number is complete, and should be sent to the other device. You also need to have some way of dealing with the wrong key being pressed - some kind of backspace key or a start-over-thats-not-what-I-meant key.

Your requirements are not complete, so it is premature to begin writing code.
The art of getting good answers lies in asking good questions.

Brinarpi

I found the one code (basic) and its working with my keypad, but as you say its fairly not complete. I realize that some enter, and clear button is required to store the value in stepsToGo int. Maybe we can split the job in two parts for easy understand. First to clear the keypad to work, and after to go on LCD.

Sorry Paul, but please help to me how to fixed the keypad and what changes are needing on the basic code. Maybe that code is not a good base for my stepper......

Here is the keypad test code:

Code: [Select]

#include <Keypad.h>

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
Serial.begin(9600);
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}


Thanks for help in advance.

A

PaulS

You need to have a variable to hold the number you are building.

Each time a key is pressed, see what the value is. If it is between '0' and '9', inclusive, multiply the number by 10 and add the key - '0'.
Code: [Select]

int number = 0;

void loop()
{
  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY)
  {
     if(keypressed >= '0' && keypressed <= '9') // A number
     {
        number *= 10;
        number += keypressed - '0';
     }
     else if(keypressed == '*') // The oops key
     {
        number = 0;
     }
     else if (keypressed == '#') // The use-this-value key
     {
        stepsToGo = number;
        Send(stepsToGo);
        number = 0;
     }
  }
}


All you need to do is write the Send() function to send the value in stepsToGo (and, of course, declare a variable named stepsToGo of type int).
The art of getting good answers lies in asking good questions.

Brinarpi

Ok. In this point I lost you....Sorry.
I was try to find on a Send() some examples, but there is nothing about it.
What was I do is to declare ints StepsToGo and number. Is that correct way how it is in code?

please help to me in send. If its work, maybe I can understand it.
I also learn lot from Robin too, in the stepping and RX TX.

Here is what I modificated:

Code: [Select]
#include <Keypad.h>

int stepsToGo = 0;
int number = 0;

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
Serial.begin(9600);
}

void loop()
{
  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY)
  {
     if(keypressed >= '0' && keypressed <= '9') // A number
     {
        number *= 10;
        number += keypressed - '0';
     }
     else if(keypressed == '*') // The oops key
     {
        number = 0;
     }
     else if (keypressed == '#') // The use-this-value key
     {
        stepsToGo = number;
        Send(stepsToGo);
        number = 0;
       
     }
  }
}


When I try to upload he tell :exit status 1 Send' was not declared in this scope

PaulS

Quote
When I try to upload he tell :exit status 1 Send' was not declared in this scope
So, you need to define the function.

Code: [Select]
void Send(int whatToSend)
{
   Serial.print( '<');  // the start marker
   Serial.print(whatToSend);
   Serial.println('>');  // end marker
}
The art of getting good answers lies in asking good questions.

Brinarpi

Thanks Paul for helping. Its working now.

Brinarpi

There is only one segment left from my project. I need somehow to check wether I type inside the correct number in stepsToGo int?

I dont know exacly does the serial begin and lcd begin work together on the arduino?
Or is pőssible to switch them off and on? Becouse the keypad use from 2-9 digital pins, where to wire LCD?
Is that possible to wire on analog pins too?

Here is the code what is working now, but without LCD.

Code: [Select]

#include <Keypad.h>

int stepsToGo = 0;
int number = 0;

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
Serial.begin(9600);
}

void loop()
{
  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY)
  {
     if(keypressed >= '0' && keypressed <= '9') // A number
     {
        number *= 10;
        number += keypressed - '0';
     }
     else if(keypressed == '*') // The oops key
     {
        number = 0;
     }
     else if (keypressed == '#') // The use-this-value key
     {
        stepsToGo = number;
       
        number = 0;

        Send(stepsToGo);
        }
  }
 }
  void Send(int stepsToGo)
{
   Serial.print( '<');  // the start marker
   Serial.print(stepsToGo);
   Serial.println('>');  // end marker
}


if I have some basic anwers, I start to put inside the LCD with his library.

A

Brinarpi

Thnaks god I solve the LCD to show the same numbers except 1 think.
If I type 4 characters the lcd show it. But after 4 if I pit only 3 or less characters, the serial give the correct value but LCD is show 4 characters. I need to reset the board to start again to solve it.

Where I am wrong in code?

Code: [Select]

#include <Keypad.h>

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int stepsToGo = 0;
int number = 0;

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
    Serial.begin(9600);
    lcd.begin(16, 2);
    lcd.setCursor(1,0);
    lcd.print("Hossz=(szam+#)");
}

void loop()
{
 
        lcd.setCursor(6, 1);
        lcd.print(stepsToGo);


  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY)
  {
     if(keypressed >= '0' && keypressed <= '9') // A number
     {
        number *= 10;
        number += keypressed - '0';
     }
     else if(keypressed == '*') // The oops key
     {
        number = 0;
     }
     else if (keypressed == '#') // The use-this-value key
     {
        stepsToGo = number;
       
        number = 0;

        Send(stepsToGo);
     }
  }
 }
  void Send(int stepsToGo)
{
   Serial.print( '<');  // the start marker
   Serial.print(stepsToGo);
   Serial.println('>');  // end marker
   delay(1000) ;
}

DangerToMyself

Add a space before or after your 3 digits.. (When going from 4 to 3, the last digit doesn't get overwritten. And it won't get removed automatically).

Brinarpi

Sorry..where to add a space?
During the typing or in program?

A

DangerToMyself

Code: [Select]

        lcd.setCursor(6, 1);
        lcd.print(stepsToGo);
        lcd.print(" ");


Like that. Unsure if stepsToGo is the number having the issue. But that's one way of doing it.

Go Up