Creating a multiple pulse generator using arduino

Hi everyone. I want to create a multiple pulse generator using Arduino (currently using mega2560).
I want to create pulses using two push buttons so that each time I press either of the push buttons a pulse on that input pin is generated. I want to control each button independently. Also, I want to once increase the push button time and then take the readings and then keep them steady and count them. Can anyone help me with the code?

Here is my code-
const int input = 12;
const int input1 = 10;// This is where the input is fed.
int pulse = 0; // Variable for saving pulses count.
int pulse1 = 1;
int var = 0;
int var1 = 0;
void setup(){
pinMode(input, INPUT);

Serial.begin(9600);
Serial.println(F("No pulses yet...")); // Message to send initially (no pulses detected yet).
}

void loop(){
if(digitalRead(input) > var)
{
var = 1;
pulse++;

Serial.print(pulse);
Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detected."));
}

if(digitalRead(input) == 0) {var = 0;}

delay(1000); // Delay for stability.

if(digitalRead(input1) > var1)
var1 = 1;
pulse1++;

Serial.print(pulse);
Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detected."));

if(digitalRead(input1) == 0) {var1 = 0;}

delay(1000); // Delay for stability.
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

if you want to "generate" a pulse, shouldn't it be on a pin configured as an output?

or do you just want to count pulses causes by button presses?

a digital input has a value of either 1 or zero. it's not clear what you're attempted to do by comparing it to > var

here the code conditionally increments pulse

but later pulse is unconditionally incremented

what is the purpose of the else case? are you using "var" to recognize changes on the "input" pin?

how does delay add stability

consider

const byte PinBut = A1;
const byte PinLed = LED_BUILTIN;

byte butState;

enum { Off = HIGH, On = LOW };

void loop ()
{
    int but = digitalRead (PinBut);

    if (butState != but) {      // recognize input change
        butState = but;
        delay (10);             // button debounce

        if (LOW == but)  {      // generate pulse
            digitalWrite (PinLed, On);
            delay (1000);
            digitalWrite (PinLed, Off);
        }
    }
}

void setup (){
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);

    pinMode      (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);
}

This is unclear. Do you want to generate an output pulse on the same pin that you are using to read the pushbutton?

What does this mean? What readings? Keep what steady? Count the number of pushes?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.