Felersuche
jetzt habe ich deinen hinweis geschnalllt::
code: [select]
// model railway switch (untested) 'jurs'
// use button input , external pull-down resistor
// or input_pullup , internal pull-up resistor
#define buttonpin 2
#define inputmode input // input or input_pullup
#define ledpin 3
#define potileft 4
#define potiright 5
#define servopin 6
#define servomiddle 1500 // 1500µs impulse duration means servo middle position
// define symbolic constants
enum {left, right}; // directions
enum {off, red, green}; // led states
int currentorientation;
void switchled(int ledstate)
{
switch(ledstate)
{
case red:
pinmode(ledpin,output);
digitalwrite(ledpin,high);
break;
case green:
pinmode(ledpin,output);
digitalwrite(ledpin,low);
break;
default:
pinmode(ledpin,input);
digitalwrite(ledpin,low);
}
}
void switchservo(int orientation)
{
switchled(off);
unsigned long time=millis();
while (millis()-time<500) updateservo(orientation); // half second delay updating servo
if (orientation==left) switchled(red);
else switchled(green);
currentorientation=orientation;
}
void updateservo(int orientation)
{
int servoimpulse=servomiddle;
if (orientation==left) servoimpulse-= analogread(potileft)/3;
else servoimpulse+= analogread(potiright)/3;
digitalwrite(servopin,high);
delaymicroseconds(servoimpulse);
digitalwrite(servopin,low);
delaymicroseconds(20000-servoimpulse);
}
boolean buttonpressed()
{
boolean result=false;
static byte lastbuttonstate;
byte buttonstate = digitalread(buttonpin);
if (inputmode==input_pullup) buttonstate=!buttonstate;
if (buttonstate == high && lastbuttonstate == low) result=true;
lastbuttonstate=buttonstate;
return result;
}
void setup() {
pinmode(buttonpin, inputmode);
pinmode(ledpin, output);
pinmode(servopin, output);
switchservo(left);
}
void loop() {
if (buttonpressed())
{
if (currentorientation==left) switchservo(right);
else switchservo(left);
}
updateservo(currentorientation);
}
jetzt habe ich deinen hinweis geschnalllt::sicher nicht!!
Arduino Forum > International > Deutsch (Moderator: uwefed) > Felersuche
arduino
Comments
Post a Comment