can I call a sketch from a sketch

I am working through my seed counter project, and now I think I need to add debounce to stabilize my input signal. Can I call debounce from my counting sketch? If so, do I need to modify the variables in debounce to match the counter sketch?

Thanks

You can call functions from other functions, yes.

Post code.

here is what I have so far:

seed count

/*
This program should count seeds as they drop and
track how many have been planted

*/
const int seedPin_1 = 2;        // the pin the sensor will attach to
const int ledPin = 13;           // the pin the led is attached to
unsigned long seedCount = 0;    // variable to track seed count
int seedState_1 = 0;              // variable to count inputs
int lastseedState_1 = 0;          // previous state of counter


void setup()
{                               // initialize counters as inputs
  pinMode(seedPin_1, INPUT);    // initialize seedState as an input
  pinMode(ledPin, OUTPUT);      // initialize led as an output
  Serial.begin(9600);            // initialize serial communication
}


void loop()
{
  seedState_1 = digitalRead(seedPin_1);  // read the seedState input pin
  if (seedState_1 != lastseedState_1)      // if the state has changed
  {                                    // if the current state is
                                       // then button wend from off
                                       // to on
    if (seedState_1 == HIGH)
    {
      seedCount++;                     // incriment the counter
      Serial.println("on");
      Serial.print("number of seeds planted   ");
      Serial.println(seedCount);
      Serial.print(lastseedState_1);
      Serial.print(seedState_1);
    }
    else
    {
      
     Serial.println("off");             // if the current state is
                                        // low, then the button wend
                                        // from on to off
    }
  }
  lastseedState_1 = seedState_1;
  if (seedState_1 % 100 == 0)
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}
debounce




                               // constants won't change. they are used
                               // here to set pin numbers
 const int buttonPin = 2;       // the number of the pushbutton pin
 const int ledPin = 13;         // the number of the led pin

                               // Variables do change
 int ledState = HIGH;          // the current state of the output pin

 int buttonState;              // the current reading from the input pin

 int lastButtonState = LOW;   //the previous reading from the input pin

                               // the following variables are long because
                               //the time, measured inmilliseconds, will
                               // quickly become a bigger number than can
                               // be stored in an int.
long lastDebounceTime = 0;    // th last time the output pin was toggled

long debounceDelay = 30;      // the debounce time, increase if output
                              // flickers

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  digitalWrite(ledPin, ledState);    // set initial led state
}

void loop()
  {
                            // read the state of the switch into a
                           // local variable                 
   int reading = digitalRead(buttonPin);   // check to see if you
                            // just pressed the button (ie.the input
                            // went from low to high), and you've
                            // waited long enough since the last press
                            // to ignore any noise:
                            // if the switche changed, due to noise 
                            // or pressing:
    if (reading != lastButtonState)
     {
      lastDebounceTime = millis(); // reset the debouncing timer
     }
      if ((millis() - lastDebounceTime) > debounceDelay)  // whatever
                            // the reading is at, it's been there for
                            // longer than the debounce  delay, so take
                            // it as the actual current state:
                            // if the button state has changed:
      {
         if (reading != buttonState) 
         {
           buttonState = reading;
                         // only toggle the led if the new buttom
                         // state is high
           if (buttonState == HIGH)
           {
             ledState = !ledState;
           }
         }
      }
      digitalWrite(ledPin, ledState);  // save tyhe reading. next time
                                       // through the loop, it'll be
                                       // the lastButtonState:
      lastButtonState = reading;
  }

How would I incorporate debounce into seed count?

Please go back and "modify" your post with the code in it, highlight all the code (or in two pieces) and mark it as code with the "code" [ # ] button above the text window.

If no-one else volunteers, I may have a go at the integration at some time within the next 24 hours, though it is (way beyond) bedtime now. :D

I am trying to record data from a sensor but am fairly sure I am getting some false readings.While running the seed count sketch (see below), can I run the debounce (also see below) sketch within it? If so, how?

seed count



/*
This program should count seeds as they drop and
track how many have been planted

*/
const int seedPin_1 = 2;        // the pin the sensor will attach to
const int ledPin = 13;           // the pin the led is attached to
unsigned long seedCount = 0;    // variable to track seed count
int seedState_1 = 0;              // variable to count inputs
int lastseedState_1 = 0;          // previous state of counter


void setup()
{                               // initialize counters as inputs
  pinMode(seedPin_1, INPUT);    // initialize seedState as an input
  pinMode(ledPin, OUTPUT);      // initialize led as an output
  Serial.begin(9600);            // initialize serial communication
}


void loop()
{
  seedState_1 = digitalRead(seedPin_1);  // read the seedState input pin
  if (seedState_1 != lastseedState_1)      // if the state has changed
  {                                    // if the current state is
                                       // then button wend from off
                                       // to on
    if (seedState_1 == HIGH)
    {
      seedCount++;                     // incriment the counter
      Serial.println("on");
      Serial.print("number of seeds planted   ");
      Serial.println(seedCount);
      Serial.print(lastseedState_1);
      Serial.print(seedState_1);
    }
    else
    {
      
     Serial.println("off");             // if the current state is
                                        // low, then the button wend
                                        // from on to off
    }
  }
  lastseedState_1 = seedState_1;
  if (seedState_1 % 100 == 0)
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}
debounce




                               // constants won't change. they are used
                               // here to set pin numbers
 const int buttonPin = 2;       // the number of the pushbutton pin
 const int ledPin = 13;         // the number of the led pin

                               // Variables do change
 int ledState = HIGH;          // the current state of the output pin

 int buttonState;              // the current reading from the input pin

 int lastButtonState = LOW;   //the previous reading from the input pin

                               // the following variables are long because
                               //the time, measured inmilliseconds, will
                               // quickly become a bigger number than can
                               // be stored in an int.
long lastDebounceTime = 0;    // th last time the output pin was toggled

long debounceDelay = 30;      // the debounce time, increase if output
                              // flickers

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  digitalWrite(ledPin, ledState);    // set initial led state
}

void loop()
  {
                            // read the state of the switch into a
                           // local variable                 
   int reading = digitalRead(buttonPin);   // check to see if you
                            // just pressed the button (ie.the input
                            // went from low to high), and you've
                            // waited long enough since the last press
                            // to ignore any noise:
                            // if the switche changed, due to noise 
                            // or pressing:
    if (reading != lastButtonState)
     {
      lastDebounceTime = millis(); // reset the debouncing timer
     }
      if ((millis() - lastDebounceTime) > debounceDelay)  // whatever
                            // the reading is at, it's been there for
                            // longer than the debounce  delay, so take
                            // it as the actual current state:
                            // if the button state has changed:
      {
         if (reading != buttonState) 
         {
           buttonState = reading;
                         // only toggle the led if the new buttom
                         // state is high
           if (buttonState == HIGH)
           {
             ledState = !ledState;
           }
         }
      }
      digitalWrite(ledPin, ledState);  // save tyhe reading. next time
                                       // through the loop, it'll be
                                       // the lastButtonState:
      lastButtonState = reading;
  }

You already have this running over here

/*
This program should count seeds as they drop and
track how many have been planted

*/
const int seedPin_1 = 2;        // the pin the sensor will attach to
const int ledPin = 13;           // the pin the led is attached to
unsigned long seedCount = 0;    // variable to track seed count

OK. So, seedCount is a reasonable name. seedPin is not. You don't have a seed attached to the pin, do you? You might have a seed sensor, though I don't think I've ever seen such a sensor.

int seedState_1 = 0;              // variable to count inputs
int lastseedState_1 = 0;          // previous state of counter

Would the states be unplanted, planted, sprouted, etc.? The state you are referring to isn't the state of the seed. It's the state of a pin. The name should reflect that. The comment is crap.

The previous state is not that of the seed, either. The comment is also crap.

What, exactly, is sensing the seeds?

I will give a little background. I am trying to update a planter we have been using for a number of years. When it was new (in 1980) the sensor system was state of the art. Today, in 2014, ... not so much. I want to keep a count of the seeds as they drop as well as the distance traveled, both for detecting when seeds stop dropping (from things getting plugged up, breaking, or simply running out), as well as calculating how any seeds I a planting per acre. (I am aware there are many ways do define plant population, from seeds per foot / mile / or any number of ways). We could buy a new planter with all the new bells and whistles, but I have bought houses for less! I have the original sensors that surround the drop tube that detect seeds dropping. I will be adding a rotary magnetic sensor to track the revolutions of the planter drive wheels, which will give me distance. As you can obviously tell, I am new to c++. I hope you can overlook my unorthodox naming methods. The seedcount seems to work, but I think I am getting false signals that need to filtered out, perhaps by debounce. I am not familair with how to incorporate debounce into seedcount any suggestions woukd be greatly appreciated

I have the original sensors that surround the drop tube that detect seeds dropping.

OK. What kind are they? How do they detect the seed?

The seedcount seems to work, but I think I am getting false signals that need to filtered out, perhaps by debounce. I am not familair with how to incorporate debounce into seedcount

I suspect that debouncing is not what you need. Rather, it is state change detection (see the example of that name). You want to know when a seed STARTS to pass by the sensor, and maybe when it STOPS passing by, rather than knowing when a seed IS passing by.

I hope you can overlook my unorthodox naming methods.

Or, you could fix them. In the long run, I know which will be better.

One simple way to debounce is to use a small delay - about the only good use for the function. Ten or twenty milliseconds is usually enough. However, you already have some inadvertent delay in your code in the form of all those serial prints after seedCount++ it's ~ 30mS. So I think you need to get a better handle on what your detection mechanism is doing.

I am very new to this, so my help may be only worth the time it took to type it...

I would start with the debounce sketch and then alter it to suit your needs instead of trying to incorporate debouncing into an existing sketch. The problem with software debouncing , however, is that it detects a state change and then sets a variable that will hold its value until the next event. So the first event will toggle the variable to high and it will stay that way until another event, which will then toggle it low. To use this to count events, you need to increment on both high and low toggles, which seems like it might be more trouble than what you want.

If your problem truly is caused by switch bounce, perhaps a hardware debouncing solution would work better. There are several simple ways to do this and a Google search will lay them all in your lap.

I would start with the debounce sketch and then alter it to suit your needs instead of trying to incorporate debouncing into an existing sketch.

What switch is bouncing? Start with explaining that, and the rest makes sense. If you can't, and I don't think you can because I don't think the seeds are pushing a switch, then the rest doesn't.

The solution depends on the problem, which has yet to be clearly defined. If the seeds are detected by breaking a beam, then the Arduino should increment the counter only when the beam BECOMES broken, not every time it sees that the beam IS broken. And, I think that it is far MORE likely that the sensor is a beam break detector rather than a mechanical switch.

Pauls, you are correct that the sensor is not a physical switch. You are also correct that I need to identify exactly what is happening. Do you think a pull-up resistor would better?

PaulS: What switch is bouncing? Start with explaining that, and the rest makes sense. If you can't, and I don't think you can because I don't think the seeds are pushing a switch, then the rest doesn't.

The solution depends on the problem, which has yet to be clearly defined. If the seeds are detected by breaking a beam, then the Arduino should increment the counter only when the beam BECOMES broken, not every time it sees that the beam IS broken. And, I think that it is far MORE likely that the sensor is a beam break detector rather than a mechanical switch.

Perhaps. Personally, I don't make it my mission to nit pick the unknowns. If the guy says he has a switch bounce problem, then I answer based on that. I agree we need more details.

moldmaker: Pauls, you are correct that the sensor is not a physical switch. You are also correct that I need to identify exactly what is happening. Do you think a pull-up resistor would better?

A pull-up resistor on what? You should post a diagram.

Do you think a pull-up resistor would better?

Than what? The pancakes at McDonalds? Well, anything is.

No. I’ve said three times now what you need to do. Look at the state change detection example. You need to detect when the sensor BECOMES high (or low), rather than detecting that the sensor IS high (or low).

The reason for this is that you are reading the sensor, and incrementing the value, more than once while a seed passes the beam.

Read the sensor as often as possible, but only increment the value when the current reading is not the same as the previous reading AND the current reading indicates that a seed is present.

You seem to have posted the code for two separate sketches which is consistent with you title.

You can only upload one sketch to an Arduino at any one time. What you need to do is incorporate the debounce business into your other sketch.

...R

OK, here's the code to try. It is based on my generic debounce algorithm, somewhat simplified, and presumes your sensor generates an active low signal with the pullup enabled.

// seed counter

/*  This program should count seeds as they drop
 and track how many have been planted */

const byte seedPin_1 = 2;            // sensor pin - active LOW
const byte ledPin = 13;              // LED indicator
unsigned long seedCount;             // actual seed count
byte seedState_1;                    // sensor status
byte lastseedState_1;                // previous sensor status
byte bounce_1;                       // debounce counter
long lasttime;                       // clock

void setup()
{                                    
  pinMode(seedPin_1, INPUT);         // seedState as an input
  digitalWrite(seedPin_1, HIGH);     // pullup enabled (reliable way!)
  pinMode(ledPin, OUTPUT);           // LED as an output
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);                // serial communication
  seedCount = 0;
  seedState_1 = HIGH;
  lastseedState_1 = HIGH;
  bounce_1=10;                       // Bounce time >= 10 ms
  Serial.println("on");
}


void loop() {
  if (millis() > lasttime) {         // next clock tick
    lasttime = millis();
    if (digitalRead(seedPin_1) == seedState_1)
    {                                // if the state is same
      bounce_1 = 10;                 // nothing to bounce
    } 
    else if (--bounce_1 == 0) {      // if we hit zero, we act:
      seedState_1 = digitalRead(seedPin_1); // update the saved
      bounce_1=10;                   // Bounce time reset

      if (seedState_1 == LOW) {      // If input now active
        seedCount++;                 // increment the counter
        Serial.print("seeds planted: ");
        Serial.println(seedCount);
      }
    }
    if (seedState_1 % 100 == 0)
    {
      digitalWrite(ledPin, HIGH);
    }
    else
    {
      digitalWrite(ledPin, LOW);
    }
  }
}