// Encoder connect to digitalpin 2(green) and 3(white) on the Arduino.
//microswitch to D4
const int sw = 4; //microswitch D4
const int RE_g = 2; //rotatry encoder green D2
const int RE_w = 3; //rotatry encoder white D3
const int R_p = A0;
volatile int counter = 0; //This variable will increase or decrease depending on the rotation of encoder
float angle, temp = 0.0;
float a = 0.0;
float D = 0.0;
float dd = 0.0;
float R = 10000.0;
float R_v = 0;
float r = 0;
void setup() {
Serial.begin(4800);
pinMode(RE_g, INPUT); // set pin to input
pinMode(RE_w, INPUT); // set pin to input
digitalWrite(RE_g, HIGH); // turn on pullup resistors
digitalWrite(RE_w, HIGH); // turn on pullup resistors
pinMode(sw, INPUT_PULLUP);
}
void loop(){
int sw_val = digitalRead(sw);
// Send the value of counter
R_v = analogRead(R_p);
r = ( 1023.0 - R_v ) / ( R_v / R );
Serial.println(r);
}
// Encoder connect to digitalpin 2(green) and 3(white) on the Arduino.
//microswitch to D4
const int sw = 4; //microswitch D4
const int RE_g = 2; //rotatry encoder green D2
const int RE_w = 3; //rotatry encoder white D3
volatile int counter = 0; //This variable will increase or decrease depending on the rotation of encoder
float angle, temp = 0.0;
float a = 0.0;
float D = 0.0;
float dd = 0.0;
float rlc = A0;
void setup() {
Serial.begin(4800);
pinMode(RE_g, INPUT); // set pin to input
pinMode(RE_w, INPUT); // set pin to input
digitalWrite(RE_g, HIGH); // turn on pullup resistors
digitalWrite(RE_w, HIGH); // turn on pullup resistors
pinMode(sw, INPUT_PULLUP);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop(){
int sw_val = digitalRead(sw);
Serial.print("D(mm)= ");
Serial.print(D);
Serial.print(",");
Serial.println(analogRead(rlc));
// Send the value of counter
if(counter >= 1200 || counter <= -1200) {
counter = 0;
}
angle = counter ;
if(sw_val == 0){
D = 0;
dd = 0;
Serial.print("D(mm)= ");
Serial.println(D);
}else{
D = dd * 8 / 1200;
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(RE_w)==LOW) {
counter++;
dd++;
}else{
counter--;
dd--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(RE_g)==LOW) {
counter--;
dd--;
}else{
counter++;
dd++;
}
}
次阻尼震盪
臨界阻尼震盪
過阻尼震盪