I’m sorry for delay in reply.
Thank you very much for your disponibility.
My idea is to control both the speed and verse of rotation of my stepper motor .
I don’t know if my code is capable to control the stepper motor , because I have combined 2 codes , but,unfortunately today, I have not still used the code.
What is you opinion about my code?
This is the code.
// ===================================================================
// Example for LCD Keypad Shield from http://www.nuelectronics.com/
// -------------------------------------------------------------------
// * Modified to use a patched LiquidCrystal library
// * Alternative routine to detect keys
// ===================================================================
#include <stdio.h>
#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// ===================================================================
// hardware settings: define some values used by the panel and buttons
#define rightKey 0
#define upKey 1
#define downKey 2
#define leftKey 3
#define selectKey 4
#define NUM_KEYS 5
// ===================================================================
// Program parameters
// definisci, per l'opzione 1,il minimo valore numerico che apparirà sul display
#define minOpt1 1
// definisci, per l'opzione 1,il massimo di valore numerico che apparirà sul display
#define maxOpt1 20
int valueOpt1 = 1;
// definisci, per l'opzione 2,il minimo di valore numerico che apparirà sul display
#define minOpt2 1
// definisci, per l'opzione 2,il massimo di valore numerico che apparirà sul display
#define maxOpt2 20
int valueOpt2 = 1;
// definisci i pin collegati ai pulsanti come da schema rightKey=0, upKey= 1,downKey = 2 leleftKey = 3
int motorPinx[] = {0, 1, 2, 3};
// definisci, per l'opzione 1 ovvero 2,il numero e la sequenza dei valori numerici che apparirà sul display
int sequenza[] = {1,2,3,4,5,6.7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int countX = 0;
int count2X = 0;
//definisci due variabili di tipo integer rispettivamente per memorizzare il valore letto sul pin 0 e sul pin 1;
int ora = 0;
int ant = 0;
int delayTime = 100;
int pinPot = 0;
// definisci i pin collegati ai pulsanti come da schema, pin 0 al senso orario e pin 1 al senso antiorario (verso orario = programMode1 digital ,verso antiorario = programMode2)
#define programMode1 0
#define programMode2 1
int programMode = programMode1;
boolean inMainProgram = false;
int programmSequence = 0;
#define sequenceModeSelect 0
#define sequenceModeOption 1
#define sequenceModeProgram 2
// first menu line - description of menu level
char sequenceHeader[3][17] = {"Mode select ", "Option select ", "Program start "};
// second menu line - mode select
char sequenceModes[3][17] = {" Mode 1 >", "< Mode 2 >",};
// second menu line - options settings
char sequenceOptions[3][17] = {"Option 1: ", "Option 2: "};
void setup()
{
// initial lcd display while initialize and pc detection
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print(" Hello word ");// print a simple message
pinMode(programMode1, INPUT);
pinMode(programMode2, INPUT);
for (countX=0; countX<4; countX++)
{
pinMode(motorPinx[countX], OUTPUT);
}
pinMode(pinPot, INPUT);
programmSequence = sequenceModeSelect;
inMainProgram = false;
delay(2000);
updateScreen();
}
// ===================================================================
// main loop with new key detection
int keyEvent = -1;
int lastEvent = -1;
int countEvent = 0;
//Key message
char msgs[5][3] = {
"> ",
"^ ",
"v ",
"< ",
"* " };
void updateScreen()
{
lcd.clear();
lcd.print(sequenceHeader[programmSequence]);
lcd.setCursor(0,0);
switch(programmSequence)
{
case sequenceModeSelect:
menuModeSelect( keyEvent );
break;
case sequenceModeOption:
menuOptionSelect( keyEvent );
break;
case sequenceModeProgram:
break;
}
}
void loop()
{
int keyEvent = detectKey();
if (keyEvent >= 0)
{
switch (keyEvent)
{
case upKey:
if (!inMainProgram)
{
if (programmSequence > sequenceModeSelect)
programmSequence--;
updateScreen();
}
break;
case downKey:
if (!inMainProgram)
{
if (programmSequence < sequenceModeProgram)
programmSequence++;
updateScreen();
}
break;
case rightKey:
case leftKey:
if (!inMainProgram)
{
switch (programmSequence)
{
case sequenceModeSelect:
menuModeSelect( keyEvent );
break;
case sequenceModeOption:
menuOptionSelect( keyEvent );
break;
case sequenceModeProgram:
break;
}
}
break;
case selectKey:
lcd.setCursor(0, 1);
if (lastEvent != keyEvent)
{
lastEvent = keyEvent;
countEvent=0;
}
else
countEvent++;
lcd.print(msgs[keyEvent]);
lcd.print(countEvent);
break;
}
}
}
// ===================================================================
// Menu tools
void menuModeSelect( int keyEvent )
{
switch (keyEvent)
{
case rightKey:
if (programMode < programMode2)
programMode++;
break;
case leftKey:
if (programMode > programMode1)
programMode--;
break;
}
lcd.setCursor(0,1);
lcd.print( sequenceModes[programMode] );
}
void menuOptionSelect( int keyEvent )
{
char cbuf[4] = " ";
lcd.setCursor(0,1);
lcd.print(sequenceOptions[programMode]);
switch (keyEvent)
{
case rightKey:
switch (programMode)
{
case programMode1:
if (valueOpt1 < maxOpt1)
valueOpt1++;
break;
case programMode2:
if (valueOpt2 < maxOpt2)
valueOpt2++;
break;
}
break;
case leftKey:
switch (programMode)
{
case programMode1:
if (valueOpt1 > minOpt1)
valueOpt1--;
break;
case programMode2:
if (valueOpt2 > minOpt2)
valueOpt2--;
break;
}
break;
}
switch(programMode)
{
case programMode1:
if (valueOpt1 > minOpt1)
lcd.print("<");
else
lcd.print(" ");
sprintf(cbuf,"%3d",valueOpt1);
lcd.print(cbuf);
if (valueOpt1 < maxOpt1)
lcd.print(">");
else
lcd.print(" ");
break;
case programMode2:
if (valueOpt2 > minOpt2)
lcd.print("<");
else
lcd.print(" ");
sprintf(cbuf,"%2d",valueOpt2);
lcd.print(cbuf);
if (valueOpt2 < maxOpt2)
lcd.print(">");
else
lcd.print(" ");
break;
}
}
// ===================================================================
// Lcd tools
void clearLine(int line)
{
lcd.setCursor(0,line);
lcd.print(" ");
lcd.setCursor(0,line);
}
// ===================================================================
// Define a custom char in lcd
int defineCharacter(int ascii, int *data) {
int baseAddress = (ascii * 8) + 64;
// baseAddress = 64 | (ascii << 3);
lcd.command(baseAddress);
for (int i = 0; i < 8; i++)
lcd.write(data[i]);
lcd.command(128);
return ascii;
}
// ===================================================================
// Convert ADC value to key number
int adc_key_val[NUM_KEYS] ={ 30, 150, 360, 535, 760 };
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
return k;
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
// ===================================================================
// new key detection routine, without delays!
int lastKeyEvent = 0;
int curKeyEvent = 0;
int keyToReturn = 0;
boolean keyToProcess = false;
int adc_key_in = 0;
int detectKey()
{
keyToReturn = -1;
adc_key_in = analogRead(0); // read the value from the sensor
curKeyEvent = get_key(adc_key_in); // convert into key press
if (curKeyEvent != lastKeyEvent)
{
if (!keyToProcess)
{
lastKeyEvent = curKeyEvent;
keyToProcess = true;
}
else
{
keyToReturn = lastKeyEvent;
lastKeyEvent = -1;
keyToProcess = false;
}
}
return keyToReturn;
ora = digitalRead(programMode1);
ant = digitalRead(programMode2);
int readPot = analogRead( pinPot );
int delayTime = map(readPot,0,1024,10,1000);
if (ora == HIGH) {
orario();
}
if (ant == HIGH) {
antiorario();
}
delay(delayTime);
}
void orario()
{
int val = LOW;
for (countX=0; countX<4; countX++)
{
if (countX == sequenza[count2X]) val = HIGH;
else val = LOW;
digitalWrite(motorPinx[countX], val);
}
count2X++;
if (count2X == 4) count2X = 0;
}
void antiorario()
{
int val = LOW;
for (countX=3; countX>=0; countX--)
{
if (countX == sequenza[count2X]) val = HIGH;
else val = LOW;
digitalWrite(motorPinx[countX], val);
}
count2X--;
if (count2X < 0) count2X = 3;
}
// ===================================================================