In the enclosed code, curious as to what line 228 does.
Code is from a coil winding machine.
executedWraps=hallTicks/116;
#define button1Pin 4
#define button2Pin 5
#define button3Pin 6
#define button4Pin 7
#define hallAPin 2
#define PWMPin 9
#define potPin A0
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte button1State=0;
byte button2State=0;
byte button3State=0;
byte button4State=0;
int lastButton1State = HIGH;
int lastButton2State = HIGH;
int lastButton3State = HIGH;
int lastButton4State = HIGH;
int button1Read=0;
int button2Read=0;
int button3Read=0;
int button4Read=0;
unsigned short cursorDigit=4; //cursor position for setting nujmber of wraps
unsigned short wrapDigit[]={0, 0, 0, 0, 0};
unsigned short i;
unsigned long wrapSetpoint=0;
volatile unsigned long hallTicks;
static unsigned short playButton=0; //value for wind initiatlization button
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime4 = 0;
unsigned long debounceDelay = 10;
unsigned short state = 0; //determines if machine is in menu mode or wrapping mode
unsigned long executedWraps=0;
unsigned long oldWraps=0;
unsigned short PWMSet = 0;
unsigned short potSet=0;
int a=0;
int counts=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.begin();
pinMode(hallAPin,INPUT);
attachInterrupt(digitalPinToInterrupt(hallAPin), counting, RISING);
pinMode(button1Pin,INPUT_PULLUP);
pinMode(button2Pin,INPUT_PULLUP);
pinMode(button3Pin,INPUT_PULLUP);
pinMode(button4Pin,INPUT_PULLUP);
pinMode(PWMPin,OUTPUT);
pinMode(potPin,INPUT);
lcd.setCursor(0,0);
lcd.print("COIL WINDER");
delay(3000);
lcd.clear();
}
void loop() {
switch(state){
case 0:
lcd.setCursor(0,0);
lcd.print("SET TURNS: ");
lcd.setCursor(0,1);
//lcd.blink();
//Button1 - increment***********************************************************************************
button1Read=digitalRead(button1Pin);
if (button1Read != lastButton1State) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (lastButton1State != button1State) {
button1State = button1Read;
if (button1State == LOW) {
//Serial.println(1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SET TURNS: ");
lcd.setCursor(0,1);
if(wrapDigit[cursorDigit]<=9){
wrapDigit[(cursorDigit)]=wrapDigit[(cursorDigit)]+1; //increments array element at cursor digit location
}
}
else if(wrapDigit[cursorDigit]>9){
wrapDigit[(cursorDigit)]=0; //resets element at digit
}
for(i=1;i<6;i++){
lcd.print(wrapDigit[(i-1)]);
lcd.setCursor(i,1);
}
lcd.setCursor(cursorDigit,1);
//lcd.blink();
}
}
lastButton1State = button1Read;
//Button 1 - increment**********************************************************************************
//Button2 - move left***********************************************************************************
button2Read=digitalRead(button2Pin);
if (button2Read != lastButton2State) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (lastButton2State != button2State) {
button2State = button2Read;
if (button2State == LOW) {
// Serial.println(2);
if(cursorDigit>0){
cursorDigit--;
}
else{
cursorDigit=4;
}
lcd.setCursor(cursorDigit,0);
//lcd.blink();
}
}
}
lastButton2State = button2Read;
//Button 2 - move left**********************************************************************************
//Button3 - play/cancel***********************************************************************************
button3Read=digitalRead(button3Pin);
if (button3Read != lastButton3State) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
if (lastButton3State != button3State) {
button3State = button3Read;
if (button3State == LOW && playButton==0) {
//Serial.println(3);
playButton=1;
lcd.noBlink();
lcd.setCursor(0,1);
lcd.print("Confirm: N Y");
delay(2000);
}
else if (button3State == LOW && playButton==1){
playButton=0;
//Serial.println(3);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SET TURNS: ");
for(i=1;i<6;i++){
lcd.print(wrapDigit[(i-1)]);
lcd.setCursor(i,1);
}
lcd.setCursor(wrapDigit,0);
//lcd.blink();
}
}
}
lastButton3State = button3Read;
//Button 3 - play/cancel**********************************************************************************
//Button4***********************************************************************************
button4Read=digitalRead(button4Pin);
if (button4Read != lastButton4State) {
// reset the debouncing timer
lastDebounceTime4 = millis();
}
if ((millis() - lastDebounceTime4) > debounceDelay) {
if (lastButton4State != button4State) {
button4State = button4Read;
if (button4State == LOW && playButton==1) {
Serial.println(4);
lcd.clear();
for(i=0;i<5;++i){
wrapSetpoint *= 10;
wrapSetpoint += wrapDigit[i];
}
// Serial.print(wrapSetpoint);
delay(1000);
playButton=0;
state=1;
lcd.noBlink();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ready to START");
delay(2000);
lcd.clear();
break;
}
}
}
lastButton4State = button4Read;
//Button 4**********************************************************************************
case 1:
lcd.noBlink();
if(executedWraps<wrapSetpoint){
lcd.setCursor(0,1);
lcd.print("No.set: ");
lcd.setCursor(8,1);
lcd.print(wrapSetpoint);
// lcd.setCursor(0,0);
// lcd.print(executedWraps);
potSet=analogRead(potPin);
PWMSet=map(potSet,1023, 0, 0, 255);
// Serial.println(hallTicks);
analogWrite(PWMPin,PWMSet);
executedWraps=hallTicks/116;
if(executedWraps != oldWraps){
lcd.setCursor(0,0);
lcd.print(executedWraps);
}
else if(executedWraps==0){
lcd.setCursor(0,0);
lcd.print(executedWraps);
}
oldWraps=executedWraps;
}
else if(executedWraps>=wrapSetpoint &&hallTicks!=0){
unsigned long doneWinds = executedWraps;
lcd.clear();
PWMSet=0;
analogWrite(PWMPin,PWMSet);
executedWraps=0;
oldWraps=0;
playButton=0;
state=0;
wrapSetpoint=0;
cursorDigit=4;
for(i=0;i<5;i++){
wrapDigit[i]=0;
}
lcd.setCursor(0,0);
lcd.print("WindsDone= ");
lcd.print(doneWinds);
delay(10000);
doneWinds = 0;
hallTicks=0;
lcd.setCursor(0,0);
lcd.print("Finished");
lcd.setCursor(0,1);
lcd.print("winding");
delay(10000);
lcd.clear();
break;
}
}
}
int counting(){
hallTicks++;
}
