BlinkWithoutDelay + map function + DIY function

I'm still finding my sea legs with Arduino so be patient with me.

I'm making a mask that will blink its eyelids and blush. Just to clarify why I identified my commands the way I did.

I figured out how to tweak the BlinkWithoutDelay code so that instead of blinking, my LED fades in and out depending on the input from a pressure sensor (i.e. using the map function). Please, correct me if I'm wrong.

What I can't seem to figure out is how to add on the second set of commands that will be running parallel to my LED fading in and out.

Here is my code:

/*

"One is never so dangerous when one has no shame, than when one has grown too old to blush." -Marquis de Sade (brainyquote.com)

*/

//stuff for blink

include "Servo.h"

Servo myservo; // create servo object to control a servo Servo mysecondservo; // a maximum of eight servo objects can be created Servo mythirdservo; // create servo object to control a servo Servo myfourthservo; // create servo object to control a servo

//stuff for blush int ledValue = 0; // LED brightness int ledPin = 6; // LED is connected to digital pin 6 int sensorPin = 0; // force sensor is connected to analog pin 0 int sensorValue = 0; // variable to store the value coming from the sensor

//stuff for continuous blush long previousMillis = 0; // will store last time LED was updated long interval = 1000; // interval at which to blink (milliseconds)

void setup() { //setup stuff for blink myservo.attach(3); // attaches the servo on pin 5 to the servo object (right eye / upper lid) mysecondservo.attach(5); // attaches the servo on pin 6 to the servo object (right eye / lower lid) mythirdservo.attach(11); // attaches the servo on pin 10 to the servo object (left eye / upper lid) myfourthservo.attach(10); // attaches the servo on pin 11 to the servo object (left eye / lower upper lid)

//setup stuff for blush pinMode(ledPin, OUTPUT); // sets ledPin to be an output Serial.begin(9600); //initialize the serial port }

void loop() // run over and over again { sensorValue = analogRead(sensorPin); ledValue = map(sensorValue,0,1000,0,255); // set the high and low outputs of the sensor and the LED

// commands for continuous blush unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; // save the last time you blinked the LED

if (sensorValue>0) // if the value coming into the microcontroller is greater than 0 analogWrite(ledPin, ledValue); // turn the LED on at the brightness set by the sensor else analogWrite(ledPin, 0); // turn the LED off }

// After this point my servos do not respond and my LEDs delay lighting up // I want the 'wink_eyes' function to run once // While the LEDs are 'blushing' // Then returning to a loop of 'blink_eyes' // With the LEDs turned off

if (sensorValue>0){ wink_eyes(); } else{ blink_eyes(); analogWrite(ledPin, 0); // turn the LED off } }

// Here are my 'blink_eyes' and 'wink_eyes' fucntions

//this is a function which gives the directions how to blink void blink_eyes(){

delay(5000);

myservo.write(50); //right eye/upper lid position when eye is closed mysecondservo.write(120); //rightleft eye/lower lid position when eye is closed mythirdservo.write(120); //left eye/upper lid position when eye is closed myfourthservo.write(50); //left eye/upper lid position when eye is closed

delay(500);

myservo.write(120); //right eye/upper lid position when eye is open mysecondservo.write(50); //right eye/lower lid position when eye is open mythirdservo.write(50); //left eye/lower lid position when eye is open myfourthservo.write(120); //left eye/lower lid position when eye is open

}

//this is a function which gives the directions how to wink while the LEDs are blushing

void wink_eyes(){

myservo.write(50); //right eye/upper lid position when eye is closed mysecondservo.write(120); //right eye/lower lid position when eye is closed mythirdservo.write(120); //left eye/upper lid position when eye is closed myfourthservo.write(50); //left eye/upper lid position when eye is closed

delay(5000);

myservo.write(120); //right eye/upper lid position when eye is open mysecondservo.write(50); //right eye/lower lid position when eye is open mythirdservo.write(50); //left eye/lower lid position when eye is open myfourthservo.write(120); //left eye/lower lid position when eye is open

delay(500);

myservo.write(50); //right eye/upper lid position when eye is closed mysecondservo.write(120); //right eye/lower lid position when eye is closed

delay(500);

myservo.write(120); //right eye/upper lid position when eye is open mysecondservo.write(50); //right eye/lower lid position when eye is open }

Am I on the right track? Or am I walking the plank?

Thanks mateys!

Two things from a quick glance at your code…

myservo.attach(3); // attaches the servo on pin 5 to the servo object (right eye / upper lid)
mysecondservo.attach(5); // attaches the servo on pin 6 to the servo object (right eye / lower lid)
mythirdservo.attach(11); // attaches the servo on pin 10 to the servo object (left eye / upper lid)
myfourthservo.attach(10); // attaches the servo on pin 11 to the servo object (left eye / lower upper lid)

Instead of the cryptic names “my…servo” use something meaningful like “RightEyeUpperLid”, “RightEyeLowerLid”, “LeftEyeUpperLid”, and “LeftEyeLowerLid”.

The datatype for millis is important…

//stuff for continuous blush
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 1000; // interval at which to blink (milliseconds)

It does not look like you paid a lot of attention to the reason for using the BlinkWithoutDelay example, as your code is still full of delays. Which, of course, is why the mask is unresponsive. It's doing nothing, for the most part, since that is what you told it to do.

Think about how you would control the servos and LEDs using just a pad of paper (corresponds to the Arduino's memory), a pencil (the Arduino can write stuff), and a watch (corresponds to millis()).

Any time you do something, like move a servo or change the on/off state of an LED, you write down the time. Periodically (like, every pass through loop), you see if it is time to do something else.

I thought that by using the BlinkWithoutDelay commands the code would run that section continuously despite what was going on in the rest of the loop, even if there were delays.

I see now that that is not the case.

When I tired changing my delay()s to millis()s my servos went all jittery and it came up as an error: “too many arguments to function 'long unsigned int millis()”. (That was obviously not the right choice.) I still need the servos to pause periodically to emulate blinking.

Am I supposed to define different time lapses via the millis() function prior to my loop? And if that is the case, then how do I write that into my ‘blink_eyes’ and ‘wink_eyes’ functions?

I read the BlinkWithoutDelay tutorial but am having trouble understanding how to expand the code beyond its initial framework. Probably because I don’t really understand the millis() function properly.

I’m super lost; help please!

I'm super lost; help please!

Forget for a moment about the mess you created and go back to this example: http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Make a new project out of it, load it and play with it around until you understand it completely. Then think about how the things learned apply to your project. Then rebuild the software of your project with your newfound knowledge from scratch, starting with one output. Make sure this one works exactly as you expect. If you don't understand something until you you do. Don't skip the mysterious parts, they won't get away when you add more stuff. Fix them before progressing. Then add the next part and again check it out completely. I guess, this will be the crucial point where you'll see how well you managed at the previous steps. Then go on and add all the rest, always step by step.

The whole thing may sound mean, but as long as you don't understand the basics - and that tutorial is one of the basics - you will always create a mess that behaves erratically.

Korman

Ha ha. Not mean, honest.

And I would love to play around with the BlinkWithoutDelay code until I fully understand it but I don't have time at the moment. This code needed to be resolved yesterday.

What I need is some straightforward direction. Is was I've done really a total mess or can it be salvaged?

And I would love to play around with the BlinkWithoutDelay code until I fully understand it but I don't have time at the moment. This code needed to be resolved yesterday.

You have the time to play around with it, because your problems won't go away until you understand it. It's like saying I don't have the time to learn driving, I need to be driving for the past hour. Time to revise your project plan and talk to the concerned people. Miracles-ain't-U most likely.

And no, it's not a total wreck, there are many salvageable parts in it. But you need to learn first how to assemble them correctly. Once you have that, the rest will go very fast and the work you've done before will save you a lot of time. But what you have now don't help you with the assemble correctly part. As I wrote before, the first output will be pretty close to the blink without delay, adding the second one will be the critical point and all of the rest will be easy where yo can recycle a lot. From experience I just advise you to start from scratch and take from your old program piece by piece what you need instead of trying to work directly in the old one.

Korman

Point taken.

Okay, I'm going back to the start. I will try and figure it out. Unfortunately, modifications aren't an option; it will take my mask entirely out of context.

If it doesn't work out, is there somewhere in cyberspace I can go and hire someone to do it for me? To go along with your driving analogy Korman, if I can't drive I can always hire a taxi to take me where I need to go. ;)

Here it goes...