Help please... for controlling a button...

Hi! Iam very new to adruino and to C language. I want to do this: a button attached to pin 7, if it stays pressed for more than 2 sec, to receive a value to operate something. If the same button stays pressed for less than 2 sec to receive another value to operate something else. How is this possible? I was thinking with the pulseIn() but i can't understand the values and how those been created. I used this code to see the values on screen but...

unsigned long time;
int pin = 7;

void setup(){
digitalWrite (pin, LOW);
Serial.begin(9600);
Serial.println("Reads a pulse (either HIGH or LOW) on a pin");
pinMode(pin, INPUT);
}

void loop(){
time = pulseIn(pin, HIGH);
if (time != 0){
Serial.println(time, DEC);
}
}

The values i receive are other than the time i hold the button down !

You are not reading the pin ever.

First try to get the status of the button pressed. I guess you are trying to use the internal pull down resistors by using the first digitalWrite. It should actually come after the pinMode command.

In the loop you need to read the pin status and see how much time run from the last time the button was pressed. Look at the millis() function.

The programme i have post is olny to read what values I receive from pulseIn() command.
The programme works but the values I receive are incomprehensible.
Any way I looked for millis() function but this is not what I want to do.
I want a programme to check if the button is pressed more or less than 2 sec.
Maybe Iam wrong but in Adruino.cc Reference page, the description for pulseIn() is closer for what I want than the millis().

You are not reading the pin ever.

Yes, the pin IS being read, in the pulseIn function.

Maybe Iam wrong but in Adruino.cc Reference page, the description for pulseIn() is closer for what I want than the millis().

Perhaps you need to re-read that page.

If you call pulseIn() with a value of HIGH, it waits for the pin to go LOW, then times how long it takes for the pin to go HIGH again. It is not appropriate for what you are doing.

You need to read the pin state using digitalRead(), and record the time when the pin state transitions to HIGH and when the pin state transitions to LOW, and when the pin transitions to the switch not pressed state, you output the difference in times.

To detect a transition, you need to know the current state and the previous state of the pin.

Also how is it wired up, do you have a pull up resistor externally?

The pin is pulled down with 10k.
So if I understand well the time start as this:
(look attachments)

pulseIn().bmp (494 KB)

In theory only in practice you have contact bounce.
As Paul said, this is not the way to do what you want.