Hello,
My program does the following:
First it gets the number of spins, and then the rotation steps (200 steps=1 full rotation)
Then in a for loop, i turn on a stepper motor: ex. 5 times x 400 steps
It uses the 4x4 keypad to enter the data.
Now, I want to start the spinning of the motor only when i press a letter from the keypad("A"), before each of that 5 times stated earlier.
Do you think is that possible ? Could you help with an idea?
In my code, the operation of the motor is done in line 65, void loop() for statement.
Thank you!
#include <Key.h>
#include <Keypad.h>
int smDirectionPin = 4; //Direction pin
int smStepPin = 5; //Stepper pin
int smEnablePin = 6; //Motor enable pin
char keypressed;
int tempFeedback = 0;
int tempFeedback2 = 0;
int value0;
int value1;
int value2;
int value3;
long val;
long readVal;
bool activated;
const byte ROWS = 4; //--(four rows)--
const byte COLS = 4; //--(four columns)--
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'}, //--(define the symbols on the buttons of the keypad)--
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {31, 33, 35, 37};
byte colPins[COLS] = {30, 32, 34, 36};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/*
_ _______ _____ _ ___
| |/ / __\ \ / / _ \/_\ | \
| ' <| _| \ V /| _/ _ \| |) |
|_|\_\___| |_| |_|/_/ \_\___/
int atoi(const char* string); Convert string to int
long atol(const char* string); Convert string to long
double atof(const char* string); Convert string to double
long double atolf(const char* string); Convert string to long double
*/
//--(keypad)--
void setup(){
Serial.begin(9600);
keypad.addEventListener(keypadEvent); // Add an event listener.
}
void loop()
{
Serial.println("Introduceti numarul de bucati");
int text=GetNumber();
Serial.println(tempFeedback);
Serial.println("Introduceti lungimea de taiere");
int textu=LungimeTaiere();
Serial.println(tempFeedback2);
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
pinMode(smEnablePin, OUTPUT);
digitalWrite(smEnablePin, HIGH); //Disbales the motor, so it can rest untill it is called uppond
// In this For loop you turn the stepper using # of cuts and lenght (steps)
for (int q=0; q<tempFeedback; q++)
{
rotate(tempFeedback2, 1000); //The motor rotates 800 steps clockwise with a speed of 0.1 (slow)
delay(2000);
}
}
int GetNumber() //--(Function for 1 to 4 digit number with return value int)--
{
int k=10; //--(local variable)--
int i;
static char buffer1[4]; //--(Used to store char string variable for atoi conversion)--
tempFeedback = 0; //--(Reset any previous held value for variable to zero)--
activated = true; //--(flag variable for while loop below)--
// In this While loop, you calculate the number of cuts, moving to the next step by pressing *
while(activated)
{
keypressed = kpd.getKey();
if (keypressed != NO_KEY)
{
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' )
{
tempFeedback += keypressed;
lcd.setCursor(k,1); //--(Place digit at display location i)--
lcd.print(keypressed); //--(Print digit)--
buffer1[i] = keypressed; //--(Put keypressed string in buffer1 for processing conversion to float)--
if (k == 10)
{
value0 = atoi(buffer1); //--(Convert string to int and put result in value0)--
}
if (k == 11)
{
value1 = atoi(buffer1); //--(Convert string to int and put result in value1)--
}
if (k == 12)
{
value2 = atoi(buffer1); //--(Convert string to int and put result in value2)--
}
if (k == 13)
{
value3 = atoi(buffer1); //--(Convert string to int and put result in value3)--
}
k++;
}
}
if (k > 14 || keypressed == '#') //--(Reset keypad entry)--
{
tempFeedback = 0;
k=10;
}
if ( keypressed == '*') //
{
activated = false; //
delay(1000);
if (k == 14)
{
tempFeedback = value0 * 1000 + value1 * 100 + value2 * 10 + value3; //--(Construct complete 4 digit number)--
}
else if (k == 13)
{
tempFeedback = value0 * 100 + value1 * 10 + value2; //--(Construct complete 3 digit number)--
}
else if (k == 12)
{
tempFeedback = value0 * 10 + value1; //--(Construct complete 2 digit number)--
}
else if (k == 11)
{
tempFeedback = value0; //--(Construct complete 1 digit number)--
}
}
}
return tempFeedback; //
}
int LungimeTaiere() //--(Function for 1 to 4 digit number with return value int)--
{
int k=10; //--(local variable)--
int i;
static char buffer1[4]; //--(Used to store char string variable for atoi conversion)--
tempFeedback2 = 0; //--(Reset any previous held value for variable to zero)--
activated = true; //--(flag variable for while loop below)--
{
keypressed = kpd.getKey();
if (keypressed != NO_KEY)
{
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' )
{
tempFeedback2 += keypressed;
lcd.setCursor(k,1); //--(Place digit at display location i)--
lcd.print(keypressed); //--(Print digit)--
buffer1[i] = keypressed; //--(Put keypressed string in buffer1 for processing conversion to float)--
if (k == 10)
{
value0 = atoi(buffer1); //--(Convert string to int and put result in value0)--
}
if (k == 11)
{
value1 = atoi(buffer1); //--(Convert string to int and put result in value1)--
}
if (k == 12)
{
value2 = atoi(buffer1); //--(Convert string to int and put result in value2)--
}
if (k == 13)
{
value3 = atoi(buffer1); //--(Convert string to int and put result in value3)--
}
k++;
}
}
if (k > 14 || keypressed == '#') //--(Reset keypad entry)--
{
tempFeedback2 = 0;
k=10;
}
if ( keypressed == '*') //
{
activated = false; //--(flag variable to stop while loop and continue)--
delay(1000);
if (k == 14)
{
tempFeedback2 = value0 * 1000 + value1 * 100 + value2 * 10 + value3; //
}
else if (k == 13)
{
tempFeedback2 = value0 * 100 + value1 * 10 + value2;
}
else if (k == 12)
{
tempFeedback2 = value0 * 10 + value1; //
}
else if (k == 11)
{
tempFeedback2 = value0; //
}
}
}
return tempFeedback2; //
}
void rotate(int steps, float speed){
digitalWrite(smEnablePin, LOW);
int direction;
if (steps > 0){
direction = HIGH; // mers inainte HIGH
}else{
direction = LOW; // mers inapoi LOW
}
steps = abs(steps);
digitalWrite(smDirectionPin, direction);
/*Steppin'*/
for (int i = 0; i < steps; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(700); // Set the speed of the stepper (500 high - 2500 low)
digitalWrite(smStepPin, LOW);
delayMicroseconds(700);
}
digitalWrite(smEnablePin, HIGH);
}