I added relays to xl97's eyeblink code, attaching them to pins 7 and 8, and writing them high and low. The code compiles, but I haven't built the circuit yet. Any pointers with the code would be greatly appreciated.
// IronMan Helmet: eye blink sequence_v1.0
// created by:
// Movie Props, Costumes and Scale Models | the RPF thread:
//
http://www.therpf.com/f24/iron-man-m...ml#post2647126
//import servo lib
#include <Servo.h>
//servo object names
Servo myservo; // create servo object to control a servo
Servo myservo1;
const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int pos = 0; // variable to store the servo positions
int pos1 = 180;
// led control pins (need to be PWM enabled pins for fading)
const int leftEye = 6; // the number of the left eye/pcb LEDs
const int rightEye = 3; // the number of the right eye/pcb LEDs
// relay control pins
int myservoLock = 7;
int myservo1Lock = 8;
unsigned long delaytime = 1;
unsigned long blinkspeed = 100;
unsigned long currentPWM = 0;
boolean isOpen = true;
#define S_IDLE 1
#define S_LEDON 2
#define S_WAITON 3
#define S_LEDOFF 4
#define S_WAITOFF 5
#define S_INITON 6
#define S_INITWAIT 7
#define S_BLINKON 8
#define S_SERVOUP 9
#define S_SERVODOWN 0
//FSM init vars
static int state = S_IDLE; // initial state is 1, the "idle" state.
static unsigned long lastTime; // To store the "current" time in for delays.
void setup() {
// Set up serial port
Serial.begin(9600);
//start it off
//state = S_BLINKON;
Serial.print("INTIT STATE: ");
Serial.println(state);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(10); // attaches the servo on pin 10 to the servo object
pinMode(buttonPin, INPUT); // initialize the button pin as a input
digitalWrite(buttonPin, HIGH); //use interal pull up resistors
pinMode(myservoLock, OUTPUT); // Set up relays as outputs
pinMode(myservo1Lock, OUTPUT);
}
void loop() {
switch (state)
{
case S_IDLE:
// We don't need to do anything here, waiting for a forced state change...like button press.
//check mian button state
buttonState = digitalRead(buttonPin);
// compare buttonState to previous state
if (buttonState != lastButtonState) {
//if button pressed/down
if (buttonState == LOW) {
//ie: pressed
if (isOpen == true) {
Serial.print("CLOSING FACE PLATE: ");
Serial.println(isOpen, DEC);
state = S_SERVODOWN;
}
else {
Serial.print("OPENING FACE PLATE: ");
Serial.println(isOpen, DEC);
//state = S_SERVOUP;
state = S_LEDOFF;
}
isOpen = !isOpen;
}
else {
//went from ON/HIGH to LOW/OFF..ie: released
//Serial.print("RELEASE: ");
//Serial.println(isOpen, DEC);
}
}
// save the current state for next loop
lastButtonState = buttonState;
break;
case S_BLINKON:
Serial.println("init blink.........");
//do blink routine here
analogWrite(leftEye, 155);
analogWrite(rightEye, 155);
delay(blinkspeed);
analogWrite(leftEye, 0);
analogWrite(rightEye, 0);
delay(10);
/*
analogWrite(leftEye, 155);
analogWrite(rightEye, 155);
delay(blinkspeed);
analogWrite(leftEye, 0);
analogWrite(rightEye, 0);
delay(10);
*/
currentPWM = 0;
state = S_LEDON;
break;
case S_LEDON:
Serial.println("increase........");
lastTime = millis(); // Remember the current time
analogWrite(leftEye, currentPWM);
analogWrite(rightEye, currentPWM);
state = S_WAITON; // Move to the next state
break;
case S_WAITON:
// If one second has passed, then move on to the next state.
if (millis() > lastTime + delaytime)
{
if (currentPWM < 255) {
currentPWM += 5;
state = S_LEDON;
}
else {
Serial.println("@ 255 done........");
state = S_IDLE;
//state = S_LEDOFF; //no auto turn off.. set to idle state
}
}
break;
case S_LEDOFF:
Serial.println("........decrease");
lastTime = millis(); // Remember the current time
analogWrite(leftEye, currentPWM);
analogWrite(rightEye, currentPWM);
state = S_WAITOFF;
break;
case S_WAITOFF:
// If one second has passed, then move on to the next state.
if (millis() > lastTime + delaytime)
{
if (currentPWM > 0) {
currentPWM -= 5;
state = S_LEDOFF;
}
else {
Serial.println("@ 0 done........");
state = S_SERVOUP; //leds off..raise faceplate
}
}
break;
case S_SERVOUP:
digitalWrite(myservoLock, HIGH); // Close relays, deactivate the lock
digitalWrite(myservo1Lock, HIGH);
myservo.write(100);
myservo1.write(100);
digitalWrite(myservoLock, LOW); // Open relays, activate lock
digitalWrite(myservoLock, LOW);
//delay(20); //wait to trigger the led flicker.. will remove delay() use in next revision
state = S_IDLE;
Serial.println("servo up.........");
break;
case S_SERVODOWN:
digitalWrite(myservoLock, HIGH); // Close relays, deactivate the lock
digitalWrite(myservo1Lock, HIGH);
myservo.write(0);
myservo1.write(0);
delay(20); //wait to trigger the led flicker.. will remove delay() use in next revision
state = S_BLINKON;
digitalWrite(myservoLock, LOW); // Open relays, activate lock
digitalWrite(myservoLock, LOW);
Serial.println("servo down.........");
break;
default:
state = S_IDLE;
break;
}
}