I have combined two sketches. Each worked in their own right. On combining, the second sketch was changed to a function with the name test_run(). In its original sketch, all the lines under test_run where actually under void loop. I had to add the while(1) to the start of the test_run function to get it to run. It wasn't in the stand alone sketch.
When I run the sketch, I can enter the numbers as required so that's fine - it will become a menu. When I press the "A" key on the keypad, I'm taken to the test_run function and it works fine also.
What I cant do is return back to the void loop ie. menu. Anything I add to the test_run function like return 0 etc, stops the sketch from running.
As I want to add more selections from the keypad, I would like to sort this out so I can add more functionality.
This is the code so far;
#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h> // Keypad Library
#include "Adafruit_LiquidCrystal.h"
#include "Wire.h"
#define LEFT_PIN 2
#define STOP_PIN 3
#define RIGHT_PIN 4
// 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;
// Define our analog pot input pin // new for updown function
#define SPEED_PIN A3
// Define our maximum and minimum speed in steps per second (scale pot to these) new for updown function
#define MAX_SPEED 1500
#define MIN_SPEED 0.1
// Variables to hold Distance and CurrentPosition
int keyfullnumber=0; // used to store the final calculated distance value
String displayvalue="";
String currentposition = ""; // new added
// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Three Columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13,12,11,10}; //{22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {9,8,7,6};//{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
Adafruit_LiquidCrystal lcd(0);
// AccelStepper Setup
AccelStepper stepper1(1, A0, A1);
void setup(void) {
// AccelStepper speed and acceleration setup
stepper1.setMaxSpeed(500); // Not too fast or you will have missed steps - down from 1500
stepper1.setAcceleration(100); // Same here - down from 400
// Set up the three button inputs, with pullups new for updown function
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
lcd.begin(20,4);
lcd.setCursor(0, 0);
lcd.print("Enter number");
}
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 'A':
test_run();
break;
}
}
}
void checknumber(int x){
if (firstnumber == 99) { // Check if this is the first number entered
firstnumber=x;
displayvalue = (firstnumber);
lcd.setCursor(13,0);
lcd.print(displayvalue);
} else {
if (secondnumber == 99) { // Check if it's the second number entered
secondnumber=x;
displayvalue = (secondnumber);
lcd.setCursor(14,0);
lcd.print(displayvalue);
// new code below for 4 numbers
} else {
if (thirdnumber == 99) { // Check if it's the third number
thirdnumber=x;
displayvalue = (thirdnumber);
lcd.setCursor(15,0);
lcd.print(displayvalue);
} else { // It must be the 4th number entered
fourthnumber=x;
displayvalue = (fourthnumber);
lcd.setCursor(16,0);
lcd.print(displayvalue);
//new code above to add fourth number
}
}
}
}
void deletenumber() { // Used to backspace entered numbers
// new code below for 4 numbers
if (fourthnumber !=99) {
String displayvalue = (" ");
lcd.setCursor (16,0);
lcd.print(displayvalue);
fourthnumber=99;
// new code above for 4 numbers
}
else {
if (thirdnumber !=99) {
String displayvalue = (" ");
lcd.setCursor(15,0);
lcd.print(displayvalue);
thirdnumber=99;
}
else {
if (secondnumber !=99) {
String displayvalue = (" ");
lcd.setCursor(14,0);
lcd.print(displayvalue);
secondnumber=99;
}
else {
if (firstnumber !=99) {
String displayvalue = (" ");
lcd.setCursor(13,0);
lcd.print(displayvalue);
firstnumber=99;
}
}
}
}
}
void test_run (){
while (1){
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
// This will run the stepper at a constant speed
stepper1.runSpeed();
}
}