|
Explore TEAMS!
|
THS /
Demo program for PWM & Servo Control// Servo & PWM Demo Program using a couple of functions // Rhine Jan 23, 2010 // Definitions and Included Libraries... #include <Servo.h> void setup() //"Main Program" int counter; long timer; Servo prettyservo_9; //declare a new variable of type Servo prettyservo_9.attach(9); //I'm plugging into PWM I/O pin 9 Serial.begin(9600); //Start of main program // Note that the ultrabright red LED is so bright that you probably won't // be able to see the effects of pwm dimming until you get down to 25% or 10% // You'll see the effects on the the normal green LED at all levels if (safe) Serial.println("\n Demo #1 - controlling LED using PWM 'dimmer' \n\n"); counter = 250;
while ((counter >= 0) && safe)
{
analogWrite(RedLED,counter);
analogWrite(GreenLED,counter);
Serial.print(counter);
Serial.print(" PWM = ");
Serial.print((counter * 100 / 255));
Serial.print("% duty cycle \n");
counter = counter - 10;
timer = millis();
while (((millis() < (timer + 500)) && safe)); // safe timer
}
if (safe) Serial.println("\n Demo #2 - controlling servos and LEDs using functions \n"); if (safe)
{ Serial.println("1. Green LED on at D=75%, blink 1X \n");
blinker(GreenLED, 75, 1, true, prettyservo_9);
}
if (safe)
{ Serial.println("2. Servo to 90 deg; Turn red on, D=75%, 2 sec \n");
Servo_set_timed_LED(prettyservo_9, 90, RedLED, 10, 2);
}
if (safe)
{
Serial.println("3. Green LED on at D=50%, blink 3X \n");
blinker(GreenLED, 50, 3, true, prettyservo_9);
}
if (safe)
{ Serial.println("4. Servo to 0 deg; Turn red on, D=50%, 4 sec \n");
Servo_set_timed_LED(prettyservo_9, 0, RedLED, 50, 3);
}
if (safe)
{ Serial.println("5. Green LED on at D=25%, blink 5X \n");
blinker(GreenLED, 25, 5, true, prettyservo_9);
}
if (safe)
{ Serial.println("6. Servo to 180 deg, Turn red on D=25%, 6 sec \n");
Servo_set_timed_LED(prettyservo_9, 180, RedLED, 25, 6);
}
if (safe)
{ Serial.println("7. Green LED on at D=10%, blink 7X \n");
blinker(GreenLED, 10, 7, true, prettyservo_9);
}
if (safe)
{ Serial.println("8. Red LED on at 10% for 8 sec; Sweep servo back-and-forth (0 - 180 deg) 8X \n");
Serial.println(" Note: is half a second long enough to move servo from 0 to 180?");
analogWrite(RedLED, 25);
blinker(0, 180, 8, false, prettyservo_9);
}
if (safe)
{ Serial.println("9. Turn red on, D=100%, End of program. \n");
analogWrite(RedLED, 255);
}
// Capture and handle unsafe condition here! if (!safe)
{ analogWrite(RedLED,0);
analogWrite(GreenLED,0);
Serial.println("\n\n Warning! Unsafe condition detected! \n");
counter = 1;
timer = millis();
while (counter < 43)
{
if ((millis() > (timer + 250)))
{ analogWrite(RedLED,((counter%2)*220 + 25));
if (!(counter%10)) Serial.println("Call");
else if ((counter > 2) && !((counter-2)%10)) Serial.println("Security! \n");
counter++;
timer = millis();
}
}
Serial.println("Unsafe program end! \n");
}
} void loop() {} //never use..create your own in "main" boolean blinker(int pin, int PowerLevel, int n, boolean pwm, Servo servo_num) at a user-specified PowerLevel. Alternatively, moves a servo back and forth 10 times.
Input Parameters:
pin: integer value representing a digital IO pin with an LED or servo attached
PowerLevel:
if LED...integer value from 0 to 100 (i.e., %) (converted to 0 - 255 PWM)
if Servo...0 to 180 (degrees)
n: Number of times to blink LED or sweep servo
pwm: 1 = PWM, 0 = Servo
Return Parameter - booloean - 1 (true) if safe, 0 otherwise)
Note: user should #define any desired global safe conditions at
beginning of program (if none, #define safe 1)
*/ int counter = 1;
long timer;
if (!pwm && safe) servo_num.write(0);
timer = millis();
while ((counter <= (2 * n)) && safe)
{
if ((millis() > (timer + 500)) && safe)
{
if (pwm && safe) analogWrite(pin,(counter%2)*(255 * PowerLevel / 100)); //when count is even, PWM dims
else if (safe) servo_num.write((counter%2)*PowerLevel);
counter++;
timer = millis();
}
}
if (safe)
return(1);
else
return(0);
}
boolean Servo_set_timed_LED(Servo servo_num, int position, int pin, int PowerLevel, int time) turns on an LED for a specified period of time and PWM power level.
Input Parameters:
position: integer value representing postion of servo
pin: integer value representing a digital IO pin with an LED
PowerLevel:
if LED...integer value from 0 to 100 (i.e., %) (converted to 0 - 255 PWM)
if Servo...0 to 180 (degrees)
time: time for LED to remain on in seconds
Return Parameter - booloean - 1 (true) if safe, 0 otherwise)
Note: user should #define any desired global safe conditions at
beginning of program (if none, #define safe 1)
*/ long timer = millis();
if (safe)
{
servo_num.write(position);
analogWrite(pin, (255 * PowerLevel / 100));
}
while (((millis() < (timer + time*1000)) && safe)); // safe timer
analogWrite(pin,0);
if (safe)
return(1);
else
return(0);
} |