enum PinAssignments {
encoderPinA = 2, // ขา S1
encoderPinB = 3, // ขา S2
clearButton = 21 // ขา Key
// จ่ายไฟที่ขา +5v และ gnd
// Example For Arduino Uno By www.ArduinoAll.com
};
volatile unsigned int encoderPos = 0; // สำหรับนับจำนวน
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = true;
boolean B_set = true;
float f=10;
int n=200;
int nn=10;
int N=50;
int D=(f*n)/5;
int bendingtime=10000000;
int relaxtime=1000000;
int delay_entre_pas=2500;
volatile double waterFlow;
float temps;
unsigned long start;
unsigned long ended;
unsigned long duration;
int delta=0;
int lastReport=0;
int x=0;
int bending=1;
int i=0;
float speed=1000*f/duration;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(encoderPinA, INPUT_PULLUP); // กำหนดโหมดเป็นแบบ Input pullup
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(clearButton, INPUT_PULLUP);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
attachInterrupt(0, doEncoderA, CHANGE); //ทำงานแบบ interrupt เบอร์ 0 ในนี้คือขา pin 2
attachInterrupt(1, doEncoderB, CHANGE); //ทำงานแบบ interrupt เบอร์ 1 ในนี้คือขา pin 3
attachInterrupt(2, bend, LOW);
waterFlow=0;
attachInterrupt(5,pulse,RISING);
digitalWrite(7, HIGH);
for(int e=0;e<2;e++){
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13, HIGH);
}
//คำสั่งกด
void bend(){
for(i=0;i<nn;i++){
Serial.print("*Round ");
Serial.print(i+1);
delayMicroseconds(10000000);
Serial.print(" Start bending*");
Serial.print("\n");
start=millis();
digitalWrite(7,HIGH);
digitalWrite(13, LOW);
for(x=0;x<=D;x++){
digitalWrite(6,HIGH);
digitalWrite(6,LOW);
delayMicroseconds(delay_entre_pas);
}
ended=millis();
duration=ended-start;
Serial.print(" End bending");
Serial.print("\n");
waterFlow=0;
attachInterrupt(5,pulse,RISING);
//delay(bendingtime);
Serial.print(" -waterFlow:");
Serial.print(1000000*waterFlow/bendingtime,3);
Serial.println(" mL/s");
Serial.print(" Start.relese");//début=start
Serial.print("\n");
digitalWrite(7,LOW);
digitalWrite(13, HIGH);
for (x=0;x<=D;x++){
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delayMicroseconds(delay_entre_pas);
}
//delayMicroseconds(relaxtime);
Serial.print(" End.relese");
Serial.print("\n");
waterFlow=0;
attachInterrupt(5,pulse,RISING);
Serial.print(" -waterFlow:");
Serial.print(1000000*waterFlow/bendingtime,3);
Serial.println(" mL/s");
}
Serial.print("--Bending time/relaxation= ");
Serial.print(duration);
Serial.print(" ms--");
Serial.print("\n");
Serial.print(" System speed="); //system speed; vitesse=speed
speed=1000*f/duration;
Serial.print(speed, 2);
Serial.print(" mm/s");
Serial.print("\n");
Serial.print(" Bending time=");//duration of the bending
Serial.print(bendingtime);
Serial.print(" ms");
Serial.print("\n");
Serial.print(" Relaxation time=");//duration of the realese
Serial.print(relaxtime);
Serial.print(" ms");
Serial.print("\n");
Serial.print(" arrow=");
Serial.print(f);
Serial.print(" mm.");
Serial.print("\n");
Serial.print("--------------------------------------------------");
Serial.print("\n");
waterFlow=0;
duration=0;
start=0;
ended=0;
speed=0;
}
//waterFlow
void pulse(){
waterFlow += 1.00 /(5880.00/2);
}
// คำสั่งทำงานแบบ interrupt เมื่อมีการหมุน
void doEncoderA() {
// debounce
if ( rotating ) delay(1); // หน่วงเวลาป้องกันสัญญาณรบกวน debounce
// เช็คว่ามีบิดสวิตช์
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
rotating = false; // no more debouncing until loop() hits again
digitalWrite(7, LOW);
for(int x=0;x<N;x++){
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delayMicroseconds(delay_entre_pas);
}
}
}
// Interrupt on B changing state, same as A above
void doEncoderB() {
if ( rotating ) delay(1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if ( B_set && !A_set )
rotating = false;
digitalWrite(7, HIGH);
for(int x=0;x<N;x++){
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delayMicroseconds(delay_entre_pas);
}
}
}