This is the rest of the code
:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder_Polling_V2.h>
const int pin_A = 10 ; // Encoder input pins
const int pin_B = 13;
int encoder_angle_counter = 0;
// _________ Variables ___________ //
// ------ Motor ------------
#define pwm_pin 11
#define motorStop() analogWrite(pwm_pin,0)
#define direction_pin 13
#define break_pin 8
int motorDutyCicle = 150; // (0-100)%
int motorSpeed = 1;
boolean startStopMotorState = false; // false - Stop, true - Start
boolean directionState = false;
boolean firstRun = true; // false - Backwards
// ------ LCD ---------------
#define delay_5s 5000
#define delay_2s 2000
#define delay_1s 1000
#define lcd_first_row 0
#define lcd_second_row 1
// LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//set the LCD address to 0x27 for a 16 chars and 2 line display
byte degree[8] = {
B01000,
B10100,
B01000,
B00000,
B00000,
B00000,
B00000,
};
// ------ Angle ------------
#define resistor_pin A3
#define angle_init_pin 6
#define lcdRowClear() lcd.print(" ");
int angleMin=165;
int angleMax=80;
int angleInit=0;
int angleWorkinkg=0;
int angleMinHexa=10;
int angleMaxHexa=90;
//------- Menu -------------
#define button_plus 5
#define button_minus 4
#define button_menu 2
#define button_start_stop 1
boolean menuButtonState = false;
// _________ Functions ___________ //
void setup()
{
//Serial.begin(9600);
//encoder_begin(); // Start the library
//attach_encoder(0, pin_A, pin_B); // Attach an encoder to pins A and B
//Serial.print("Hello");
// ----- Motor -----
pinMode(pwm_pin, OUTPUT);
pinMode(direction_pin, OUTPUT);
//pinMode(break_pin, OUTPUT);
digitalWrite(break_pin, LOW);
digitalWrite(direction_pin, HIGH);
motorStop(); // prevent from moving with the default value
// ----- Angle -----
pinMode(resistor_pin, INPUT);
pinMode(angle_init_pin, INPUT);
// ----- LCD -------
lcd.begin(16,2);
lcd.createChar(0, degree);
lcd.print("Welcome"); // For testing it may be disabled
lcd.noCursor();
//lcd.autoscroll();
delay(delay_5s);
lcd.clear();
lcd.home();
pinMode(button_menu, INPUT);
pinMode(button_plus, INPUT);
pinMode(button_minus, INPUT);
pinMode(button_start_stop, INPUT);
// ------ Init ------
getInitAngle();
deviceInit();
}
void loop()
{
.
.
.
}
void motorMove()
{
//if(getButtonStatus(button_start_stop) == LOW ) analogWrite(pwm_pin, motorDutyCicle);
//else motorStop();
motorDutyCicle = motorSpeed *20+100;
analogWrite(pwm_pin, motorDutyCicle);
}
void motorCalc(int dc)
{
dc = constrain(dc, 10, 90);
motorDutyCicle = map(dc, 10, 90, 0, 255);
}
/*
Function: initialize the angle and correct error mesuring
*/
int getInitAngle()
{
// Need to define the reverse direction of the motor
//int angleInit;
lcd.clear();
lcd.setCursor(0,lcd_first_row);
lcd.print("Initializing...");
digitalWrite(direction_pin, LOW);
analogWrite(pwm_pin, motorDutyCicle);
while(digitalRead(angle_init_pin) != HIGH);
motorStop();
//digitalWrite(break_pin, HIGH);
lcd.clear();
lcd.setCursor(0,lcd_first_row);
lcd.print("ok");
angleInit = analogRead(resistor_pin); // perform callculation to determine the angle(not voltage)
lcd.setCursor(0,lcd_second_row);
lcd.print(angleInit);
delay(delay_2s);
encoder_angle_counter = 76;
Serial.print("Init..... DONE");
return angleInit;
}
/*
function: initialize the preference of the device
min/max angle, speed
*/
void deviceInit()
{
String initMesage[] = {"Start angle:","End angle:","Speed:"};
int initValues[3]; // {Start,End,Speed}
boolean buttonPressed = false;
initValues[0] = angleMin;
initValues[1] = angleMax;
initValues[2] = motorSpeed;
for(int i=0; i<3; i++)
{
lcd.clear();
lcd.setCursor(0,lcd_first_row);
lcd.print(initMesage[i]);
lcd.setCursor(0,lcd_second_row);
lcd.print(initValues[i]);
while(true)
{
if(digitalRead(button_plus) == HIGH)
{
initValues[i] = i<2 ? initValues[i]+5 : initValues[i]+1;
buttonPressed = true;
}
else if(digitalRead(button_minus) == HIGH)
{
initValues[i] = i<2 ? initValues[i]-5 : initValues[i]-1 ;
buttonPressed = true;
}
if(buttonPressed == true)
{
buttonPressed = false;
if(i==0) initValues[0] = constrain(initValues[0],135,165); // boundaries Start angle
else if(i==1) initValues[1] = constrain(initValues[1],80,initValues[0]-15); // boundaries End angle
else if(i==2) initValues[2] = constrain(initValues[2],1,5); // boundaries of speed
lcd.clear();
lcd.setCursor(0,lcd_first_row);
lcd.print(initMesage[i]);
lcd.setCursor(0,lcd_second_row);
lcd.print(initValues[i]);
delay(400);
}
if(digitalRead(button_menu) == HIGH){
delay(30);
while(digitalRead(button_menu) == HIGH);
delay(30);
break;
}
}
}
// Initial angle in Degree
angleMin = initValues[0];
angleMax = initValues[1];
motorSpeed = initValues[2];
// Initial angle in Hexa
angleMinHexa = (((angleMin/5)*4)+angleInit);
angleMaxHexa = (((angleMax/5)*4)+angleInit);
lcd.clear();
lcd.setCursor(0,lcd_first_row);
lcd.print("Setup complete.");
delay(delay_1s);
//lcd.clear();
lcd.setCursor(0,lcd_second_row);
lcd.print(" press START ");
}
boolean getButtonStatus(int buttonPin)
{
int state;
state = digitalRead(buttonPin);
if(state == HIGH){
delay(30);
while(digitalRead(buttonPin) == HIGH);
delay(30);// prevent from missreading
}
return state;