trying to write first sketch, delay off LED.

hey guys im trying to build a turbo timer which should consist of one input and one output, operating so that when the input goes high the output follows, and when the input goes low the output stays high for predetermined period of time and then goes low until the input goes high again. sounds easy and should have taken all off twenty mouse clicks on PLC, but ive been on this for ten hours now and getting no where fast! :frowning:

const int ledPin = 13;
const int buttonPin = 2;


int ButtonPin;


//"ALWAYS use unsigned long for timers, not int"
  //(variable declaration outside setup and loop, of course)
unsigned long   
 long Timer = 1500; 
   void setup(){
     pinMode(buttonPin,INPUT);
     pinMode(ledPin,OUTPUT);}

void loop (){
if digitalRead buttonPin==HIGH;
   digitalWrite (ledPin, HIGH);
   (Timer = millis()); 
if  (millis()); Timer >= 1500UL;
   digitalWrite (ledPin, LOW);}

the code compiles but does not run as intended, all that happens is the led goes high and stays high indeffinatley.
i have performed multiple searches for a simple arduino timer functions on the net and have read over the the delay and millis section of the playground many times (http://arduino.cc/en/Tutorial/BlinkWithoutDelay) (Arduino Playground - AvoidDelay) but have been unable to correctly brace and bracket the pseudo code shown there. why is this so difficult?

unsigned long   
 long Timer = 1500;

"unsigned long long" is not the same as unsigned long.


Try doing an auto-format on your sketch:

int ButtonPin;

//"ALWAYS use unsigned long for timers, not int"
//(variable declaration outside setup and loop, of course)
unsigned long   
long Timer = 1500; 
void setup(){
  pinMode(buttonPin,INPUT);
  pinMode(ledPin,OUTPUT);
}

void loop (){
  if digitalRead buttonPin==HIGH;
  digitalWrite (ledPin, HIGH);
  (Timer = millis()); 
  if  (millis()); 
  Timer >= 1500UL;
  digitalWrite (ledPin, LOW);
}

Now you can see you are doing:

  if  (millis());

In other words, if millis() returns non-zero (which it almost certainly will) do nothing.


This does nothing useful:

  Timer >= 1500UL;

ButtonPin and buttonPin are different variables (case is important).


  if digitalRead buttonPin==HIGH;

"if" statements need brackets.


How about looking at some of the examples? Don't just thrash around typing whatever gets into your head. Take a working example, look at the syntax, and then modify to your needs.

There are lots of examples in the IDE (File menu -> Examples).

thanks Nick i have gone through the examples on the arduino software and have been looking at blinkWithoutDelay, debounce,and ifStatementConditional. and have been trying to gather what to do with these to achieve what i want.
im starting to think it would be faster to make this thing out of some CMOS pulse gen's and counters!
if what ive done to create a counter is incorrect, then how do i do it?
all i can think of is

read input
if input high write output high
else if input low start timer (however you do this?????????)
if timer has run and input low
write output low

but where im having the most trouble is finding super simple building block type explanations/tutorials of arduino functions, maybe Don Lancaster should write a book on it :slight_smile:

ive searched the web for hours with such inputs as(arduino delay off timer, arduino timers, arduino millis as timers, arduino CMOS functions, arduino PLC functions) and i think il be doing even more.

It can be a bit overwhelming at first, but if you tackle one thing at a time it all becomes simple.

As for the idea of timers, I did a bit of a write-up on them here:

You don't "start a timer" - there is one running all the time. You can get its value with millis, eg:

unsigned long now = millis ();  // time now

So if the input goes low you can make a note of when that happened, and then see if it is (say) 5 seconds later. Preferably by subtracting because the timer wraps around from time to time (after 50 days).

eg.

if (millis () - when_switch_pressed > 5000) 
  {
    // 5 seconds have elapsed
  }