Felersuche

Jetzt habe ich deinen Hinweis geschnalllt::

// Model railway switch (untested) by 'jurs'

// use button with INPUT and external pull-down resistor 
// or with INPUT_PULLUP and 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 some 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 the 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); 
}

Volli:
Jetzt habe ich deinen Hinweis geschnalllt::

Sicher nicht!!

Und warum machst Du dafür einen neuen Thread auf? Willst Du uns verarschen?

Gruß Tommy