Halloween rekvisita att vända sig till se du som du promenera genom (4 / 11 steg)
Steg 4: Koden
Öppna upp Arduino-miljö. Kopiera och klistra in den i en ny skiss.
Sedan ladda upp koden till Arduino styrelsen.
#include < Servo.h >
Servo myservo; skapa objekt för att styra en servo servo
int ResetTimer = 0; uppsättningar dröjsmål återställa ställning
int målplacera = 3; lagrar den mål position (1-5) där personen som står
int GoalPositionDegrees; lagrar målet position i grader (30-150)
int CurrentPositionDegrees = 90. lagrar den aktuella positionen i grader (30-150)
int AveragingSpeed = 100; Anger hur snabbt medelvärdena justera (ett lägre värde ändrar genomsnittliga värdet snabbt) / / snabbt. Ett värde på 100 ändrar det genomsnittliga värdet långsamt
hans anger effektivt hastigheten vid vilken sensorerna kalibrera sig till förändrade ljusförhållanden
int PinOneCurrent; lagrar nuvarande värde av stift 1-5
int PinTwoCurrent;
int PinThreeCurrent;
int PinFourCurrent;
int PinFiveCurrent;
float PinOneAverage; lagrar det genomsnittliga värdet på stift 1-5
float PinTwoAverage;
float PinThreeAverage;
float PinFourAverage;
float PinFiveAverage;
flyta RatioPinOne = 0.00; lagrar förhållandet mellan nuvarande pin värde till pin medelvärde
flyta RatioPinTwo = 0.00;
flyta RatioPinThree = 0.00;
flyta RatioPinFour = 0.00;
flyta RatioPinFive = 0.00;
flyta tröskel = 0,95; anger lägsta beloppsgräns för sensorer
void setup()
{
myservo.attach(13); fäster servo digital stift 13
PinOneAverage = analogRead(1); läsningar av sensorer för att sätta första genomsnittliga pin värde
PinTwoAverage = analogRead(2);
PinThreeAverage = analogRead(3);
PinFourAverage = analogRead(4);
PinFiveAverage = analogRead(5);
}
void loop()
{
Läs analog stift 1-5 och ange resultatet som aktuellt värde
PinOneCurrent = analogRead(1);
PinTwoCurrent = analogRead(2);
PinThreeCurrent = analogRead(3);
PinFourCurrent = analogRead(4);
PinFiveCurrent = analogRead(5);
justera genomsnittliga pin värden
PinOneAverage = PinOneAverage + (PinOneCurrent-PinOneAverage) / AveragingSpeed;
PinTwoAverage = PinTwoAverage + (PinTwoCurrent-PinTwoAverage) / AveragingSpeed;
PinThreeAverage = PinThreeAverage + (PinThreeCurrent-PinThreeAverage) / AveragingSpeed;
PinFourAverage = PinFourAverage + (PinFourCurrent-PinFourAverage) / AveragingSpeed;
PinFiveAverage = PinFiveAverage + (PinFiveCurrent-PinFiveAverage) / AveragingSpeed;
beräknar förhållandet mellan nuvarande pin värde till pin medelvärde
RatioPinOne = (float) PinOneCurrent/PinOneAverage;
RatioPinTwo = (float) PinTwoCurrent/PinTwoAverage;
RatioPinThree = (float) PinThreeCurrent/PinThreeAverage;
RatioPinFour = (float) PinFourCurrent/PinFourAverage;
RatioPinFive = (float) PinFiveCurrent/PinFiveAverage;
avgöra vilken förhållandet är den största och anger mål
Ange målet position
om (RatioPinThree < tröskel & & RatioPinThree < RatioPinOne & & RatioPinThree < RatioPinTwo & & RatioPinThree < RatioPinFour & & RatioPinThree < RatioPinFive)
{Målplacera = 3;
ResetTimer = 0; }
annars om (RatioPinOne < tröskel & & RatioPinOne < RatioPinTwo & & RatioPinOne < RatioPinThree & & RatioPinOne < RatioPinFour & & RatioPinOne < RatioPinFive)
{Målplacera = 1;
ResetTimer = 0; }
annars om (RatioPinTwo < tröskel & & RatioPinTwo < RatioPinOne & & RatioPinTwo < RatioPinThree & & RatioPinTwo < RatioPinFour & & RatioPinTwo < RatioPinFive)
{Målplacera = 2;
ResetTimer = 0; }
annars om (RatioPinFour < tröskel & & RatioPinFour < RatioPinOne & & RatioPinFour < RatioPinTwo & & RatioPinFour < RatioPinThree & & RatioPinFour < RatioPinFive)
{Målplacera = 4;
ResetTimer = 0; }
annars om (RatioPinFive < tröskel & & RatioPinFive < RatioPinOne & & RatioPinFive < RatioPinTwo & & RatioPinFive < RatioPinThree & & RatioPinFive < RatioPinFour)
{Målplacera = 5;
ResetTimer = 0; }
annars om (ResetTimer > 100) //after dröjsmål återställs i läge 3
{Målplacera = 3;
ResetTimer = 0; }
annat
{ResetTimer = ResetTimer + 1;}
GoalPositionDegrees = målplacera * 25 + 15. konverterar mål ställning till grader
myservo.write(GoalPositionDegrees); anger servo skalad värde
Delay(30); Anger hur quckly servo blir (lägre siffror tur snabbare)
}