Hello all! I am trying to figure out a new project. I have a tubing bender that I am making that has a remote operated solenoid valve and am trying to use an Arduino to input the desired bend angle and check it against a LPD3806-360BM rotary encoder that will be connected to the shaft. When it reaches the angle the relay will stop once the desired angle is reached. However, I'm not sure how to declare the variable from the set_Degrees function into the loop. It is not in the setup function so I believe it is a global variable. Any help is appreciated!
[code]
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define enc_a 2
#define enc_b 3
int CYLINDER_EXTEND = 13;
int CYLINDER_RETRACT = 12;
volatile int encoderValue = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x20 for a 16 chars and 2 line display
byte rowPINS[ROWS] = {4, 5, 6, 7};
byte colPINS[COLS] = {8, 9, 10, 11};
Keypad kpd = Keypad(makeKeymap(keys), rowPINS, colPINS, ROWS, COLS);
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}
float set_Degrees() {
int i = 0;
int counter = 0;
float Degrees = 0;
float Bend_Angle = 0;
float num = 0.00;
float decimal = 0.00;
float decnum = 0.00;
float val = 0.00;
lcd.clear();
char key = kpd.getKey();
lcd.setCursor(0, 1); lcd.print("Enter BendAngle:"); lcd.setCursor(0, 3); lcd.print("OK= # "); lcd.print((char)60); lcd.print((char)45); lcd.print(" D");
lcd.setCursor(17, 2);
bool decOffset = false;
while (key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '.':
if (!decOffset)
{
decOffset = true;
}
lcd.print(key);
break;
case 'D':
num = 0.00;
lcd.setCursor(16, 2); lcd.print(" ");
lcd.setCursor(16, 2);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (!decOffset)
{
num = num * 10 + (key - '0');
lcd.print(key);
}
else if ((decOffset) && (counter <= 1))
{
num = num * 10 + (key - '0');
lcd.print(key);
counter++;
}
break;
} //end case
if (num >= 181)
{
lcd.clear();
lcd.setCursor(0, 0); lcd.print("EXCEED MAX PARAMETER"); //The bender will bend tubing 180 degrees in one operation but not more than 180. Software limit
delay(5000);
software_Reset();
}
decnum = num / pow(10, counter);
key = kpd.getKey();
} //end while not #
return decnum; //I need the value from the keypad entry to be saved to the variable last_Degrees and passed into the if-statements to compare to the encoder value
}
void setup() {
lcd.init();
lcd.backlight();
lcd.backlight(); lcd.print("JIMS MACHINE SERVICE");
lcd.setCursor(4, 2); lcd.print("TUBING BENDER");
lcd.setCursor(3, 3); lcd.print("COPYRIGHT 2020");
delay(2000);
lcd.clear();
set_Degrees();
pinMode(CYLINDER_EXTEND, OUTPUT);
pinMode(CYLINDER_RETRACT, OUTPUT);
pinMode(enc_a, INPUT_PULLUP);
pinMode(enc_b, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
}
void loop() {
lcd.setCursor(0, 1); lcd.print(" "); // These two lines are to clear the screen without having a lcd.clear inside the loop?
lcd.setCursor(4, 3); lcd.print(" ");
char key = kpd.getKey();
float decnum;
float Degrees = encoderValue * .5; //The encoder value is multiplied by .5 because the quadrature encoder doubles the value that I actually want.
float set_Degrees ; //STATIC
while (Degrees != set_Degrees)
{
//set_Degrees = Degrees;
lcd.setCursor(1, 0); lcd.print("Industrial Bender");
lcd.setCursor(0, 2); lcd.print("Ent.Angle:"); lcd.setCursor(11, 2); lcd.print("Bend Ang:");
lcd.setCursor(0, 3); lcd.print(decnum); //changed to write instead of print?
lcd.setCursor(7, 3); lcd.print("*");
lcd.setCursor(12, 3); lcd.print(encoderValue * .5);
}
if (Degrees >= set_Degrees) {
digitalWrite(CYLINDER_EXTEND, HIGH); // Here the last_degrees variable, the entered value, checks with the encoder.
digitalWrite(CYLINDER_RETRACT, LOW); //
encoderValue == set_Degrees;
}
if (Degrees == set_Degrees) {
digitalWrite(CYLINDER_RETRACT, HIGH); //send bender home after bending operation.
digitalWrite(CYLINDER_EXTEND, LOW);
set_Degrees = 0;
}
}
void encoder() {
if (digitalRead(enc_a) == digitalRead(enc_b)) {
encoderValue++;
}
else {
encoderValue--;
}
}
[/code]