I'm a newbie in arduino, I am want to make a function to add 1 to a variable once(like a pulse) and then add 1 every milisecond after 3 seconds

I'm a newbie in arduino, I am want to make a function to add 1 to a variable once(like a pulse) and then add 1 every milisecond after 3 seconds using millis(); function.

`if (digitalRead(12)== LOW)
{

buttonPressTime=millis();
if(buttonPressTime==5)
{
  number=number+1;
}
if(buttonPressTime>3000)
{
  number=number+1;
}`

I don't know what went wrong. please help
here is the entire code--

bool num_array[10][7]=
{
  //{a,b,c,d,e,f,g},//0
  {0,1,1,1,1,1,1},//0
  {0,0,0,0,1,1,0},//1
  {1,0,1,1,0,1,1},//2
  {1,0,0,1,1,1,1},//3
  {1,1,0,0,1,1,0},//4
  {1,1,0,1,1,0,1},//5
  {1,1,1,1,1,0,1},//6
  {0,0,0,0,1,1,1},//7
  {1,1,1,1,1,1,1},//8
  {1,1,0,1,1,1,1} //9
};
byte time = 5;
byte number;

unsigned long buttonPressTime;
void setup() {


 for (byte i=2;i<=12;i++)
 {
  pinMode(i,OUTPUT);
  }
  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  
}

void loop() {
 
  if (digitalRead(12)== LOW)
  {
    
    number=number+1;
    delay(200);
    
  }
  if (digitalRead(11)== LOW)
  {
    number=number-1;
    delay(200);
    
  }
  
    seven_segment(number);
    delay(1);
  
 }
void seven_segment(int number)
{
  
  byte ones;
  byte tens;
  if(number < 100 )
  {
  ones=number%10;
  tens=(number-ones)/10;   
  }
  

  choose_segments(tens);
  pick_digit(1);
  delay(time);

choose_segments(ones);
  pick_digit(2);
  delay(time);
}

void choose_segments(int num)
{
for(byte j = 0; j <= 6; j++)
{
  digitalWrite(j+2,num_array[num][j]);
 
  }
}

void pick_digit(int digit)
{
  
switch(digit)
{

case 1:
digitalWrite(9,HIGH);
delay(time);
digitalWrite(9,LOW);
break;

case 2:
digitalWrite(10,HIGH);
delay(time);
digitalWrite(10,LOW);
break;

}  
}





  

This is a great case to use for learning…

First thing… you are testing to see if the button IS pressed, you probably want to test WHEN it’s pressed.

Second, checking for a lengthy press (3000ms) is a little more complex. At the moment, you’ll get an event every passs of loop while the button is pressed - after the first 3000ms from reset

Look into millis based timing, it’s not hard, you just
have to get your head around it.

It’s also a good time to start naming your pins, because as your programs get bigger - it helps to self-document your code - like // comments.

1 Like

You should describe the wanted functionality in normal words. As you are a beginner and as we (the other users on the forum don't know how much you know about programming you might have some misconceptions about a programming-function.

Using normal words assures that there are no misconceptions involved
So describe the behaviour of your program like a bystanding person that is watching you
and just describe what this bystanding person can see.

millis() counts up the milliseconds from the moment you connect power to your microcontroller.
After 3 seconds millis() returns value 3000
After 4 seconds millis() returns value 4000
After 5 seconds millis() returns value 5000
....
This means any timing based on millis is done by calculating time-differencies.

best regards Stefan

1 Like

+1 on slow down a bit and start smaller.

But the above code is problematic at best.

millis() will equal 5 for one millisecond, very shortly after the Arduino resets or powers up.

While one millisecond is an eternity to some perspectives, it is unwise to rely on being around at the right time to see 5 go by.

a7

1 Like

thank you for the suggestion
but can you please help my with some code.
I am making this for science fair and I need to submit my project by 16th of feb.
please guide me further

working really scientific means you elaborate and reasearch on something that is new to the entire human race! = there is nobody around the globe that could teach you.
real researchers use certain methods they have learned on a new subject.

And you are asking on delivering code which is something that you could learn by googling?
So one thing every researcher has to learn: Never underestimate how long it can take to develop a certain project.

Ok it is a younger students science fair.
The least thing you can do is demonstrating some

own effort

which is the thing you get the honor for on a science fair.

Showing some own effort by posting your search activities trying to find out how programming timing based on millis() works. Some google links like that

You can mark the relevant port of the url

and post it in its own line so the forum-sogftware can translate it to a reral link

and then post a specific question.

beeing scientific means to be specific and precise

Yes I can here is "some" code

void setup() {
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();
}

This is "some" code. And it is even C++ (not python or java) which is also "some" code
Of course I can conclude you want code related to your initial question.

If you post a picture with a timing-diagram that shows how things should happen after each other or at the same time and specifiy IO-pins sensors and numbers you might be able to finish this project right in time.

From what you have posted so far somebody would have to write hundreds of code-variants to make sure that one of the variants fits to your project. This surely will nobody do

best regards Stefan

Ok I will definitely try hard but can you please help me a tiny bit please?
can you tell why this isn't working?

bool pulse = false;
void setup(){
pinMode(12, INPUT_PULLUP);
pinMode(2,OUTPUT);

}
void loop() {
if (digitalRead(12)== LOW)
{
generate_pulse();
}
if (digitalRead(12)==HIGH)
{
pulse=false;
}
}
void generate_pulse()
{
if(pulse==false)
{
digitalWrite(2,HIGH);
delay(50);
digitalWrite(2,LOW);
pulse=true;
}
}

I want this code to generate a pulse only.

You are still wayyyy to unprecise

If you want to
I exactly quote you

just remove the

and your code will I again quote you

I'm 101% sure you want something different than.

And you have to describe this different thing in

much more words

You will speed up finishing your project if you take 20 minutes time to describe

in detail

what you really want

in summary.

And the best way to describe this is drawing

a timing diagram

You are welcome on this forum! You are working on an informatic project and what is most needed in an informatic project is information imagine: do the other users here in the forum have a clear picture of what you are trying to do?

To speed up finishing your project you should invest some time into writing additional information I'm 100% sure that this WILL speed up finishing your project.

If you don't post all this information because you want a "quick answer" to your detail problem It is very likely to turn out that all that happens is having mutliple waiting-times with mutliple asking back for details until the other users do have a clear picture of what you want to do.

best regards Stefan

Okay, so, with the code I wrote above, I want it to generate a pulse to add 1 to the variable.
First let me tell you what my project is about.
So my project is to demonstrate a safety feature in which we get to know about the total no. of people who entered a premises and the no. of people left in the premises.
For this I want to generate a pulse so that I can add1 to the total no. of people who entered a place. And then subtract 1 to see that how many people are left inside.
To sense if someone entered, I am using LDR and a laser torch.
I want to use buttons to switch between two modes((1) how many entered,(2) how many remain)
I am using arduino nano (old boot loader).
To actually show the numbers, I am using 3 seven segment displays, 330ohm and 1k resistors and npn transistors.
I am going to use some bits of codes from everything given above, and I am unable to generate a single pulse as the switch is pressed (for testing).


testing circuit.


actual circuit

The pictures shows two LDRs

This means there is one light-barrier for entering
a second light-barrier for leaving ?

So does this mean each time the light-barrier-laser-beam is interrupted through a person that walks through the entering light-barrier a counter-variable should increment by 1?

And if the light-barrier for leaving is interrupted the variable shall be decremented by 1?

The actual problem is to generate a single-pulse by pressing the button
to change between
1 show number of persons that have entered (as a summary over all)
2 show number of persons that remain (by counting up/down entered / leaved persons)

Which means the button-press shall toggle between
1 display show number of persons that have entered
2 display show number of persons that remain
?

confirm or correct this description
best regards Stefan

I want to use the generated pulse to add and subtract too(via LDR).

"So does this mean each time the light-barrier-laser-beam is interrupted through a person that walks through the entering light-barrier a counter-variable should increment by 1?"

Yep.
I am trying to use the interrupt command.
below is the new circuit

this is the new code but it too is really problematic please check it once.

#define SW1 A0
#define SW2 A1
int tentered = 25;
int tleft = 15;
byte toshow = true;


bool num_array[10][7]
{
//{a,b,c,d,e,f,g},//0
  {1,1,1,1,1,1,0},//0
  {0,1,1,0,0,0,0},//1
  {1,1,0,1,1,0,1},//2
  {1,1,1,1,0,0,1},//3
  {0,1,1,0,0,1,1},//4
  {1,0,1,1,0,1,1},//5
  {1,0,1,1,1,1,1},//6
  {1,1,1,0,0,0,0},//7
  {1,1,1,1,1,1,1},//8
  {1,1,1,1,0,1,1} //9
};
byte time = 5;
int number;

void setup()
{
  Serial.begin(9600);
  for (byte i = 4; i < 14; i++)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), ISRIN, FALLING);
  attachInterrupt(digitalPinToInterrupt(3), ISROUT, FALLING);
}
void loop()
{
    if (digitalRead(A0) == LOW)
    {
      do
      {
      } while (digitalRead(A0) == LOW);
      toshow = false;
    }
    if (digitalRead(A1) == LOW)
    {
      do
      {
      } while (digitalRead(A1) == LOW);
      toshow = true;
    }
    if (toshow == true)
    {
      seven_segment(001);//(tentered);
    }
    else
    {
      seven_segment(tentered-tleft);
    }
}
void ISRIN()
{
  tentered++;
}
void ISROUT()
{
  tleft++;
}
void seven_segment(int number)
{
  //Serial.println(number);
 byte ones;
 byte tens;
 byte hundreds;
 if (number< 1000 && number>-1)
   {
    
    ones = (number % 100)%10;
    tens = ((number-ones)%100)/10;
    hundreds=number-((tens*10)+ones)/100;
   }
 else
   {
     Serial.println("PEVN");
   }
   choose_segments(hundreds);
  pick_digit(1);
  delay(2);

  choose_segments(tens);
  pick_digit(2);
  delay(2);

  choose_segments(ones);
  pick_digit(3);
  delay(2);
}

void choose_segments (int num)
{
  for (byte j = 0; j < 7 ; j++)
  {
    digitalWrite(j+4,num_array[num][j]);
  }
}

void pick_digit(int digit)
{
  switch(digit)
  {
    case 1:
     digitalWrite(11,HIGH);
     delay(2);
     digitalWrite(11,LOW);
     break;

     case 2:
     digitalWrite(12,HIGH);
     delay(2);
     digitalWrite(12,LOW);
     break;
     
     case 3:
     digitalWrite(13,HIGH);
     delay(2);
     digitalWrite(13,LOW);
     break;
  }
}

by the way I was thinking that we can use a zoom meeting to quickly solve the problem.
if you agree I can send you link to enter the meeting tomorrow.

Even though you need a fast solution, I would suggest rather than put all of your code together at once, you try smaller pieces of it at a time so you can see what is working and what is not.

I also would suggest you make use of serial.print() to send information back to the Arduino IDE monitor so that you can verify the results.

If you have not verified this already, a good thing to start with is making sure that you can get the sensors to generate an interrupt and you can read it in the code and detect the people passing. If you already did that, good. For others to help, it is also good to know what you have tested that already works. e.g. have you tested you can write to the display?

For me, if I am working on new things, I like to verify the functional pieces and then add them together incrementally, again testing to make sure things still work. I expect that this would also sit well with people judging the work.

Once you have this basically operating, you need to test it well, see if you can find some inaccurate counts when people pass by.

If you find you can make count inaccuracies, maybe these things are solvable in your project timeframe. If not, you can cite them as potential issues for improvements and make suggestions.

I was trying to something similar 2 years ago but I was using a modified PIR sensor (to avoid wiring across a doorway). I could not make that reliable, so I documented door opening and closing instead with magnetic door switch.

I hope this helps. Good luck with it.

I tested everything and everything was working fine but the numbers displayed on 7 segment display were really wrong.

Have you printed these numbers you cakulated to see if they are correct and make the digits of number?

a7

yep,
I have extended the date of submission to 20 feb for me by calling my teacher. so we still got a bit time.