Need help with a small code

Hello friends,

Iam doing a small project of people counter with Laser, LDR and 7 segment, the code is pretty big because i used 3 1-digit 7 segments, anyways, i have sensor A and sensor B when someone passes through the A sensor ( reading of sensor A decreases) and passes through B too after A the counter increases 1 and vice versa, i wanted to do some modification on the counter, what condition should i write if i dont want the counter to change when someone passes through A and doesnt pass through B ?

Thanks in advance :slight_smile:

Code is in attachments

COUNTER_WITH_7_SEGMENT14.ino (9.58 KB)

I thought you said it was a small code. :slight_smile:

aarg:
I thought you said it was a small code. :slight_smile:

i think its small because its a condition by if, i dont understand what reading should i use in the condition so the counter not to be changed or maybe using a timer like delay

Sorry i meant the code that i wanted to write inside it not the one i uploaded haha :smiley:

i actually meant that part

void loop()
{
int n=0;
sensA=analogRead(A0);
sensB=analogRead(A1);
thresh=500;

if(sensA>thresh&&sensB<thresh)
{
ctr++;
delay(n);
}

if(sensA<thresh&&sensB>thresh)
{
ctr–;
delay(n);
}

if(sensA>thresh||sensB>thresh)
{
sensA=0;
sensB=0;
delay(n+600);
n=millis();
}

units=ctr%10;
tens=(ctr%100)/10;
hundreds=ctr/100;

aarg:
I thought you said it was a small code. :slight_smile:

This is smaller code.

#define ORIGIONAL    // code=5414, data=63, 

//#define ORIGIONAL   // code=1768, data=21

Chuck

COUNTER_WITH_7_SEGMENT14.ino (11.3 KB)

chucktodd:
This is smaller code.

#define ORIGIONAL    // code=5414, data=63, 

//#define ORIGIONAL  // code=1768, data=21




Chuck

can you explain to me what does this one do ?

gunha392:
can you explain to me what does this one do ?

download the code I attached to my message #6.

I coded a replacement for your code, along side your code. the #define is a compiler directive that along with #ifdef or #ifndef and #else and #endif control whether or not the compiler ignores or include code between those markers.

#define HELLO  //this tells the compiler the word HELLO exists

#ifdef HELLO  // if the word HELLO exists compile the following code

uint8_t variableA = 7;

#else

uint8_t variableA = 10;

#endif

//****************************
// as the above code is written, the only code that is compiled is

uint8_t variableA = 7;

// everything else is ignored
//****************************

// If I put comment markers infront of the #define, like this:


//#define HELLO  //this tells the compiler the word HELLO exists

// now the only code that is compiled is:

uint8_t variableA = 10;

Take a look at my code, I rewrote your code to show you a different way to accomplish the same thing,
but with less code duplication.

I was able to reduce all of your one(), two(), oneX() … functions down into one function:

const uint8_t segments[3][7] PROGMEM={{10,11,12,13,0,9,8},{6,7,2,1,3,5,4},{16,17,18,19,0,0,0}};
const uint8_t digits[11] PROGMEM={0x3F,0x06,0x1B,0x4F,0x66,0x1D,0x7D,0x07,0x7F,0x6F,0x00};

void writeDigit(uint8_t digi,uint8_t value){
// digit is 0 ..2, value is 0..9
uint8_t pattern = pgm_read_byte_near(digits[value]); // get segment pattern
if (digi=2) pattern = value%10; // simple binary, except 10 represents blank(0), not B00001010

for (uint8_t i =0;i<7;i++){
 digitalWrite(pgm_read_byte_near(segments[digi][i]),((1<<i)&pattern));
  }
}

The two const declarations are used to define your hardware configurations. segments[][] defines the Arduino pins that are associated with each segment.
digits[] defines how to represent each digit 0…9. for example, the third byte is 0x1B which represents the segments to turn on to display a ‘2’

// pgm_read_byte_near(digits[2]) = 0x1B;

dp g f e d c b a
0 0 0 1 1 0 1 1 // oops! the actual pattern should have been B01011011 or 0x5B, forgot the ‘g’ segment

The for loop is a little complex:

for (uint8_t i =0;i<7;i++){
  digitalWrite(pgm_read_byte_near(segments[digi][i]),((1<<i)&pattern));
  }

// i cycles for 0 to 6, you did not use the decimal points off the 7segments, so I skipped them.
//
// pgm_read_byte_near(segments[digi][i]) looks up the Arduino pin number for each segment (i from 0..6)
// the way I encoded the digits[] array bit 0 of each byte represents segment 'a', bit 2 segment 'b', etc.
// 
// the (1<<i) create a value starting at 1, and shifts that 1 left 'i' binary places, so if 'i' = 3 then,
// (1 << 1) becomes binary B00000010 or 2, (1<<2) becomes binary B00000100 or 4, (1<<3) becomes
// B00001000 or 8.
// pattern holds the current segment pattern to display, so if pattern = 0x5B, and i=3;
// (1<<i)&pattern = B00001000 & B01011011 = B00001000, which since it is not Zero is also equivalent to 
// true or HIGH.
// so, if digi = 0, (units place), and i=3, and pattern = 0x5B, the digitalWrite() equivilent to
// digitalWrite(13,HIGH);

My example does not answer your question, it just gives you a different example of possible coding.
I was bored and saw a way that I could compress your code down.

Chuck.

@chucktodd Woah thank you sir that’s amazing :smiley: , i think using arrays is a good way to make codes smaller

but i still have a a slight problem with that code i want to add, would you advise me to use delay in my problem or use readings of sensors with if condition ?

gunha392:
@chucktodd Woah thank you sir that’s amazing :smiley: , i think using arrays is a good way to make codes smaller

but i still have a a slight problem with that code i want to add, would you advise me to use delay in my problem or use readings of sensors with if condition ?

You need to state your problem more succinctly. How do you know that a person has passed sensor ‘A’ but not passed Sensor ‘B’?

Your code to increment and decrement your counter does not have any hysteresis.

What are your rules for counting?

Do both Sensors have to change at the same time for an event to be recorded?
Or does only one sensor need to change?
Do both Sensors have to return to an ‘off’ state before you can consider an event to have happened?

Your code is equivilent to this, because the loop() function is continuously called by the hidden main().

while((sensorA>thresh)&&(sensorB<thresh)){
  ctr++;

  delay(n); // btw: n never was set to any value in your original program?

  sensorA=analogRead(A0);
  sensorB=analogRead(A1);
  
  }

This code continuously increments ctr as long as sensorA>thresh and sensorB<thresh
Is this what you want?

I would change your code to track the ‘State’ of your sensors, and only count up or count down after the sensors have returned to a neutral state, or changed from their last state. Or, is just the passage of time acceptable to be considered a ‘new’ event?

void loop(){

static int currentA,currentB,lastA,lastB; // the static modifier tells the compiler this variable 
  // retains its value even when it drops out of context. (when loop() is exited)

currentA = analogRead(A0); // garbage, because the CPU had to switch the input multiplexer to new pin
currentA = analogRead(A0); // good value
currentB = analogRead(A1);
currentB = analogRead(A1);

if((currentA>thresh)&&(lastA>thresh))||(currentA<thresh)&&(lastA<thresh)){ // A0 has not changed enough
  // do nothing, or can an event be triggered when sensorB changes?
  if((currentB>thresh)&&(lastB>thresh))||(currentB<thresh)&&(lastB<thresh)){ // A1 has not changed 
    // both sensor's haven't changed? does this mean something?
    }   
  else { //sensorA did not Change, but sensorB did?  what event should happend here?
    }
  }
else { // A0 has changed
  
  // now do something?, IS a change of sensorA enough, or does sensorB also need to change?

  // If sensorB must also change before an event is possible, then test B
  
  if((currentB>thresh)&&(lastB>thresh))||(currentB<thresh)&&(lastB<thresh)){ // A1 has not changed 
    // but A0 did change? what should be done here?
   // if you do something here the relies on the fact that sensorA changed, you must update the 'State'
   // variable for sensorA
   lastA = currentA;
   }
  else { // both A0 and A1 have changed what do you want to do?
    
    // now in this case, after you process your event, you must update the 'State' variables
    lastA = currentA;
    lastB = currentB;

    }
 }

So, you need to describe under what conditions of sensorA, sensorB that events (counts) are valid.

Chuck.

chucktodd:
You need to state your problem more succinctly. How do you know that a person has passed sensor ‘A’ but not passed Sensor ‘B’?

Your code to increment and decrement your counter does not have any hysteresis.

What are your rules for counting?

Do both Sensors have to change at the same time for an event to be recorded?
Or does only one sensor need to change?
Do both Sensors have to return to an ‘off’ state before you can consider an event to have happened?

Your code is equivilent to this, because the loop() function is continuously called by the hidden main().

while((sensorA>thresh)&&(sensorB<thresh)){

ctr++;

delay(n); // btw: n never was set to any value in your original program?

sensorA=analogRead(A0);
 sensorB=analogRead(A1);
 
 }




This code continuously increments **ctr** as long as **sensorA>thresh** and **sensorB<thresh**
Is this what you want?

I would change your code to track the 'State' of your sensors, and only count up or count down after the sensors have returned to a neutral state, or changed from their last state. Or, is just the passage of time acceptable to be considered a 'new' event?



void loop(){

static int currentA,currentB,lastA,lastB; // the static modifier tells the compiler this variable
 // retains its value even when it drops out of context. (when loop() is exited)

currentA = analogRead(A0); // garbage, because the CPU had to switch the input multiplexer to new pin
currentA = analogRead(A0); // good value
currentB = analogRead(A1);
currentB = analogRead(A1);

if((currentA>thresh)&&(lastA>thresh))||(currentA<thresh)&&(lastA<thresh)){ // A0 has not changed enough
 // do nothing, or can an event be triggered when sensorB changes?
 if((currentB>thresh)&&(lastB>thresh))||(currentB<thresh)&&(lastB<thresh)){ // A1 has not changed
   // both sensor’s haven’t changed? does this mean something?
   }  
 else { //sensorA did not Change, but sensorB did?  what event should happend here?
   }
 }
else { // A0 has changed
 
 // now do something?, IS a change of sensorA enough, or does sensorB also need to change?

// If sensorB must also change before an event is possible, then test B
 
 if((currentB>thresh)&&(lastB>thresh))||(currentB<thresh)&&(lastB<thresh)){ // A1 has not changed
   // but A0 did change? what should be done here?
  // if you do something here the relies on the fact that sensorA changed, you must update the ‘State’
  // variable for sensorA
  lastA = currentA;
  }
 else { // both A0 and A1 have changed what do you want to do?
   
   // now in this case, after you process your event, you must update the ‘State’ variables
   lastA = currentA;
   lastB = currentB;

}
}




So, you need to describe under what conditions of sensorA, sensorB that events (counts) are valid.

Chuck.

I’m sorry for confusion, but the thing i wanted is that if person passes through SensorA only and doesnt pass through sensorB the counter doesnt increase and vice versa, but the main problem that i have in the code that the counter only increase when A is affected only

gunha392:
I'm sorry for confusion, but the thing i wanted is that if person passes through SensorA only and doesnt pass through sensorB the counter doesnt increase and vice versa, but the main problem that i have in the code that the counter only increase when A is affected only

Then, change it so that the counter will only increase/decrease when both sensorA and sensorB are detected.

I think you must answer the question that has been posed in reply #9, which is:

What are your rules for counting?

Otherwise you will be running around like a chicken with its head cut off.

aarg:
Then, change it so that the counter will only increase/decrease when both sensorA and sensorB are detected.

I think you must answer the question that has been posed in reply #9, which is:

Otherwise you will be running around like a chicken with its head cut off.

I’m very Sorry for being late, I’ve been searching for something to help me and i’ve tried this code below but it seems that nothing changed, I used this library called elapsed millis from How to code Timers and Delays in Arduino

the rule is that i want is to make a timer, the timer starts once the person pass sensorA, Now the person must pass SensorB too in an interval of time of 3000 millisecond, if person doesnt pass sensorB in a time larger than the interval then the counter doesnt change, if he passes in less than that time, the counter increase, and vice versa in sensorB

void loop()
{

sensA=analogRead(A0);
sensB=analogRead(A1);
thresh=500;

if (sensA>thresh) //Person cuts SensorA
{

timer0= 0; //start the timer

}

if(sensB>thresh&&timer0<interval) //The Person cuts sesnorB in time less than the interval
{
ctr++; // increase the counter by 1
delay (700);
timer0 -= interval; //Reset the timer

}

if (sensA>thresh&&sensB<thresh&&timer0<interval) //The person cuts A only but doesnt cut B and the time passed is less than the interval

{ ctr=ctr; // counter doesnt change
delay (700);
timer0-= interval; //reset the timer

}

}

I still dont understand why the timer isnt working ? :S

Where is your 3000 ms time? I don’t see it anywhere in the code. In fact, I don’t see your setup() function at all.

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

Sorry for this, Iam still new to the forums, i couldn’t put the whole code in the post because of allowed length so i had to put it in attachments

COUNTER_WITH_7_SEGMENT17.ino (10.2 KB)

That’s fine. Now we have an complete, updated version of your code.

Is there some reason why you didn’t adopt the code in reply #7?

Why do you have this:

int arrayOne [] = {16,17,18,19};                //*
  for (int pin = 0; pin <4; pin ++)              //*
  { 
  pinMode (arrayOne [pin], OUTPUT);  
  }

yet you have this:

  pinMode(0,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);

aarg:
That’s fine. Now we have an complete, updated version of your code.

Is there some reason why you didn’t adopt the code in reply #7?

I wanted to use it but I found it confusing and i have to use this code to explain to my friends in project how things happened, they only studied C++ and dont understand arduino language :smiley:

gunha392:
I wanted to use it but I found it confusing and i have to use this code to explain to my friends in project how things happened, they only studied C++ and dont understand arduino language :smiley:

Arduino language is C++.

Can you explain how your program works?

aarg:
Why do you have this:

int arrayOne [] = {16,17,18,19};                //*

for (int pin = 0; pin <4; pin ++)              //*
 {
 pinMode (arrayOne [pin], OUTPUT);  
 }



yet you have this:


pinMode(0,OUTPUT);
 pinMode(1,OUTPUT);
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
 pinMode(7,OUTPUT);
 pinMode(8,OUTPUT);
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);
 pinMode(11,OUTPUT);
 pinMode(12,OUTPUT);
 pinMode(13,OUTPUT);

The first one in array is because i converted analog pins to digital pins, i ran out of pins for 7 segment and i used a 74ls47 decoder, the rest pins are digital pins i used for two 7 segments

well they just studied the very basics of C++ and binary numbers, the project given by our professor as an optional work

The program is a person counter that works as follow, 2 Lasers are directed to 2 LDRs, When a person pass through the first Laser (SensorA) , the counter increase and vice versa in the second laser,

The problem i face that when someone passes through the counter, but he only cuts Laser A and doesnt not cut B, so the counter increased and the person didnt pass, thats what i want to solve