Blackfin:
Walk through the loop to understand what it's doing:
First pass, suppose the duration is >1200 so the if() is true and you move your servo.
On the next pass you follow the same logic and the for() executes again. The first thing it will do is write '0' to myservo which will cause it to move back to zero.
A "break" isn't what you need here. Without devoting too much thought, maybe add a true/false flag that indicates the servo has already moved to 180. It's initialized to false and once the servo moves to 180 it is set to true. You check this flag in your if( duration > 1200 ) -- i.e. if( duration > 1200 && flagAlreadyAt180 == false )
Very crudely:
#include <Servo.h>
const uint8_t servoPin = 9;
const uint8_t signalPin = 7;
uint8_t servoPos = 0;
Servo myservo;
unsigned long duration;
uint8_t flagAlreadyAt180;
void setup()
{
Serial.begin(9600);
myservo.attach( servoPin );
pinMode( signalPin, INPUT );
flagAlreadyAt180 = false;
}//setup
void loop()
{
duration = pulseIn(signalPin, HIGH);
delay(1000);
Serial.println(duration);
delay(2000);
if (duration > 1200 && flagAlreadyAt180 == false )
{
for(servoPos=0;servoPos <= 90;servoPos+=1)
{
myservo.write(servoPos);
delay(50);
Serial.println(servoPos);
}//if
flagAlreadyAt180 = true;
}//if
}//loop
I thought I had cracked it and added to the code to go from 180 degrees back to zero and this is what happens
operate the switch on the RC transmitter - ON
the servo moves up to 180 degrees and stops
operate the switch on the RC transmitter - OFF
the servo moves back to 0 then flips to max and starts to count down from 255!
As far as I can see from the code, the flag should stop anything happening! Am I missing something obvious? I have a solution using another library but would like to understand why I can't get this method to work!
#include <Servo.h>
const uint8_t servoPin = 9;
const uint8_t signalPin = 7;
uint8_t servoPos = 0;
Servo myservo;
unsigned long duration;
uint8_t flagAlreadyAt180;
void setup()
{
Serial.begin(9600);
myservo.attach( servoPin );
pinMode( signalPin, INPUT );
servoPos = 0;
flagAlreadyAt180 = false;
}//setup
void loop()
{
duration = pulseIn(signalPin, HIGH);
delay(1000);
Serial.println(duration);
delay(2000);
Serial.println(flagAlreadyAt180);
if (duration > 1200 && flagAlreadyAt180 == false )
{
for (servoPos = 0; servoPos <= 180; servoPos += 1)
{
myservo.write(servoPos);
delay(50);
Serial.println(servoPos);
}//if
flagAlreadyAt180 = true;
}//if
if (duration < 1200 && flagAlreadyAt180 == true )
{
for(servoPos = 180;servoPos >= 0;servoPos-=1)
{
myservo.write(servoPos);
delay(50);
Serial.println(servoPos);
}//if
flagAlreadyAt180 = false;
}
}//loop