Declaration where I don't know how to solve

Hello,
I was looking for a code where you can time how long a switch is pressed for a bigger project. I found a code but there is an error and I don't know how to solve pls help :confused:

/* SwitchTime sketch 
 *  Countdown timer that decrements every tenth of a second lights an LED when 0 
 *  Pressing button increments count, holding button down increases rate of increment
 */ 

const int ledPin = 13;
const int inPin = 2;
const int debounceTime = 20;
const int fastIncrement = 1000;
const int veryFastIncrement = 4000;
int count = 0;

void setup(){
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}
void loop(){
  int duration = switchTime();
  if (duration > veryFastIncrement)
  count = count + 10;
  else if (duration > fastIncrement)
  count = count + 4;
  else if (duration > debounceTime)
  count = count + 1;
  else{
    if (count == 0)
    digitalWrite(ledPin, HIGH);
    else{
      digitalWrite(ledPin, LOW);
      count = count -1;
    }
  }
  Serial.println(count);
  delay(100);
}{
  static unsigned long startTime = 0;
  static boolean state;
  if digitalRead(inPin) != state){
    state = !state;
    startTime = millis();
  }
  if (state == LOW)
  return millis() - startTime;
  else
  return 0;
}

the error: 'exit status 1
'switchTime' was not declared in this scope'
I know to, but don't know to solve

Thank you in advance

I know to, but don't know to solve

You haven't written the function you are trying to call. Write it.

}{ Suspicious.
Missing something?

Where did you find the code?
The switchTime() function is likely there too.

The code is from a book called: Arduino cookbook
I've tried every code before without any errors. But when I read e description it sounds like the switchTime() is a function that's build-in, but it ain't.
This is What in the book:

"5.4 Determining How Long a Switch Is Pressed Problem
Your application wants to detect the length of time a switch has been in its current state. Or you want to increment a value while a switch is pushed and you want the rate to increase the longer the switch is held (the way many electronic clocks are set). Or you want to know if a switch has been pressed long enough for the reading to be stable (see Recipe 5.3). Solution The following sketch demonstrates the setting of a countdown timer. The wiring is the same as in Figure 5-5 from Recipe 5.2. Pressing a switch sets the timer by incrementing the timer count; releasing the switch starts the countdown. The code debounces the switch and accelerates the rate at which the counter increases when the switch is held for longer periods. The timer count is incremented by one when the switch is initially pressed (after debouncing). Holding the switch for more than one second increases the increment rate by four; holding the switch for four seconds increases the rate by ten.
Releasing the switch starts the countdown, and when the count reaches zero, a pin is set HIGH (in this example, lighting an LED):

/* SwitchTime sketch Countdown timer that decrements every tenth of a second lights an LED when 0 Pressing button increments count, holding button down increases rate of increment
 */ 
const int ledPin = 13;                // the number of the output pin 
const int inPin = 2;                         // the number of the input pin
const int  debounceTime = 20;         // the time in milliseconds required for the switch to be stable const int  fastIncrement = 1000;       // increment faster after this many milliseconds
 const int  veryFastIncrement = 4000;   // and increment even faster after this many milliseconds 
int count = 0;                         // count decrements every tenth of a second until reaches 0

void setup() {  
pinMode(inPin, INPUT); 
 digitalWrite(inPin, HIGH); // turn on pull-up resistor 
 pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}
void loop() {  
int duration = switchTime(); 
 if( duration > veryFastIncrement)
    count = count + 10; 
 else if ( duration > fastIncrement) 
   count = count + 4; 
 else if ( duration > debounceTime)  
  count = count + 1;
  else  {   // switch not pressed so service the timer
    if( count == 0) 
     digitalWrite(ledPin, HIGH); // turn the LED on if the count is 0 
   else    {     
 digitalWrite(ledPin, LOW);   // turn the LED off if the count is not 0 
     count = count - 1;           // and decrement the count 
   }  
}
  Serial.println(count);
  delay(100); 
}
// return the time in milliseconds that the switch has been in pressed (LOW) long
 switchTime() { // these variables are static  - see Discussion for an explanation 
 static unsigned long startTime = 0; // the time the switch state change was first detected
  static boolean state;                // the current state of the switch
  if(digitalRead(inPin) != state) // check to see if the switch has changed state
  {   
 state = ! state;      // yes, invert the state  
  startTime = millis();   // store the time 
 }  
if( state == LOW) 
   return millis() - startTime;   // switch pushed, return time in milliseconds 
 else 
   return 0; // return 0 if the switch is not pushed (in the HIGH state);
 }

Discussion
The heart of this recipe is the switchTime function. This returns the number of milliseconds that the switch has been pressed. Because this recipe uses internal pull-up resistors (see Recipe 5.2), the digitalRead of the switch pin will return LOW when the switch is pressed. The loop checks the value returned from switchTime to see what should happen. If the time the switch has been held down is long enough for the fastest increment, the counter is incremented by that amount; if not, it checks the fast value to see if that should be used; if not, it checks if the switch has been held down long enough to stop bouncing and if so, it increments a small amount. At most, one of those will happen. If none of them are true, the switch is not being pressed, or it has not been pressed long enough to have stopped bouncing. The counter value is checked and an LED is turned on if it is zero; if it’s not zero, the counter is decremented and the LED is turned off.

I really don't understand what to write for this function..

switchTime() { Very close.

Still missing something

How do you write a function that starts counting when a button is pressed I don't know that :confused:

You're missing a return type for your function definition.

I don't have your book, so I don't know if it is missing in the book, or you failed to transcribe it correctly.

Try "unsigned long" and see if you get any further.

hmm i'll try.
And you can find the book here:
http://www.maerivoet.org/website/software/arduino/manuals/arduino/arduino-cookbook.pdf
page 144 is my problem

page 144 is my problem

Actually, your problem is that you have not bothered to do any reading on how functions are coded, because the fix is very simple.

You've been given extremely strong hints by AWOL. All you need to do is follow them up by studying one or more of the many, many fine on line tutorials on the C/C++ language.

Trust me, it works to expend a tiny bit of effort on your own.

brecht123:
hmm i'll try.
And you can find the book here:
http://www.maerivoet.org/website/software/arduino/manuals/arduino/arduino-cookbook.pdf
page 144 is my problem

You mis-spelled 146.

If read the whole book until this page so I did and I’m not a king in C/C++ language
Look I’m gonna try myself then. I ain’t a snob that doesn’t look first and just ask right away.
I’m working on a project whit a similar part so I don’t need this to work I just don’t understand how to finish this function that’s why I’m asking
And this side chapter starts at 144 :stuck_out_tongue:

Look carefully at reply #2, reply #5 and page 146.

Cut&Paste error.
You have:

// return the time in milliseconds that the switch has been in pressed (LOW) long
 switchTime() { // these variables are static  - see Discussion for an explanation

The book has (more correctly):

// return the time in milliseconds that the switch has been in pressed (LOW)
long switchTime() { // these variables are static  - see Discussion for an explanation

Note the changed position of "long"

Omg this last part just needed to be on the top, I taught it would work if it just was in void loop. And I don't copy/ past i write it myself then you learn to understand the code... i'm a noob, but trying not to be :smiley:

So i have a question now, if you make a function, should the code for the function always be placed in the top before void setup? because a function after a '{' in void loop gives a error?

It'll work find where it is, as long as you put the "long" before the declaration of switchTime() instead of in the comment.

If you can't find the code, here's a way to do it:

You can find length a switch has been on/off by calling millis() function and storing it in a int value when the switches boolean changes.

millis() gets the time since the arduino started running it's program.

so: int storedMillis = millis();

Then call it again when the switch changes and do: int switchTime = millis() - storedMillis;

so:

// This represents your switch/button being turned off
bool switch = 0;

//Get time switch turned off
int storedMillis = millis();

//Used to create time delay between switch on/off - you dont need this
delay(1000);

// This represents your switch/button being turned on
switch = 1;

//This is the time between switch off/on
int switchTime = millis() - storedMillis;

This is untested code but should hopefully work.

Hope the learning is going well!

Styryl:
int storedMillis = millis();

[..] should hopefully work.

It doesn't. Try this:

unsigned long storedMillis = millis();