Created
December 21, 2016 17:23
-
-
Save varlal/05210a4ecdba0fc4c81ae225859a6818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo myservo; | |
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to | |
const int servoPin = 9; // Analog output pin that the LED is attached to | |
int sensorValue = 0; | |
int sensorBuff[10] = {0,0,0,0,0,0,0,0,0,0}; | |
int aveValue = 0; | |
int count = 0; | |
int pos = 0; | |
int startPos = 80; | |
int endPos = 0; | |
int open_flag=0; | |
int high_flag=0; | |
int high_flag_buff=0; | |
int high_time_history[8] = {1,1,1,1,1,1,1,1}; | |
int history_count=0; | |
int correct_time_history[7] = {103,186,186,192,192,102,189};//逆になっている | |
int time_error=70; | |
void setup() { | |
Serial.begin(115200); | |
myservo.attach(servoPin); | |
myservo.write(startPos); | |
} | |
void loop() { | |
sensorValue = analogRead(analogInPin); | |
sensorValue = abs(sensorValue -512); | |
sensorBuff[count%10]=sensorValue; | |
aveValue = 0; | |
for(int i = 0; i < 10; i++){ | |
aveValue+=sensorBuff[i]; | |
} | |
aveValue/=10; | |
//Serial.println(aveValue); | |
if(aveValue > 100){ | |
high_flag=1; | |
}else{ | |
high_flag=0; | |
} | |
//立ち上がり | |
if(high_flag==1 && high_flag_buff==0){ | |
high_time_history[history_count%8]=count; | |
for(int n = 0; n < 7; n++){ | |
int diff =high_time_history[(history_count-n)%8]- high_time_history[(history_count-n-1)%8]; | |
Serial.println(diff); | |
if(correct_time_history[n]-time_error <= diff && diff <= correct_time_history[n]+time_error){ | |
//あってるよ! | |
//Serial.print(diff); | |
Serial.print(" "); | |
}else{ | |
break; | |
} | |
//最後までbreakしなかったら鍵を開ける | |
if(n == 6){ | |
open_flag=1; | |
} | |
} | |
Serial.println("----"); | |
history_count=(history_count+1)%32768; | |
} | |
//鍵を開ける | |
if(open_flag==1){ | |
for (pos = startPos; pos >= endPos; pos -= 1) { | |
myservo.write(pos); | |
delay(15); | |
} | |
delay(5000); | |
for (pos = endPos; pos <= startPos; pos += 1) { | |
myservo.write(pos); | |
delay(15); | |
} | |
for(int i = 0; i < 10; i++){ | |
sensorBuff[i]=0; | |
} | |
} | |
open_flag=0; | |
high_flag_buff=high_flag; | |
count=(count+1)%32768; | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment