789Views20Replies

Author Options:

static int counter = 0; if ((++counter & 0x3f) == 0) Answered

I have not been ablle to find a laymans discription of what this is counting. Looks like it is counting 63 times from the 0x3f ASCII charactor.
I bleave it is counting for the helper light, but what for?



#include <Password.h>
#include <Keypad.h>
#include <Stepper.h>
Password password = Password( "7457" ); //password to unlock door, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3',},
{'4','5','6',},
{'7','8','9',},
{'*','0','#',},
};

byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6,};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Stepper myStepper(stepsPerRevolution, 9,10,11,12);
int enable_pin = 13;
int button_pin = A0;
int button_pinstate = 0; // variable to store current pir state
//int lastpirstate = 0; // variable to store last pir state
int lastbutton_pinstate = 0;
int pos = 35;
int pos2 = -35; 

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long offtime;
boolean helper_light_is_on = false; // helper light off

void setup(){

myStepper.setSpeed(60); // set the speed at 60 rpm:
pinMode( button_pin, INPUT);
digitalWrite(button_pin, HIGH); // pull-up
Serial.begin(9600);
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(15, OUTPUT); //Helper light
pinMode(16, OUTPUT); //green light
pinMode(17, OUTPUT); //red light
pinMode(enable_pin, OUTPUT);
digitalWrite(enable_pin,HIGH);

keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop()
{
button_pinstate = digitalRead(button_pin);
if(button_pinstate != lastbutton_pinstate) {
Serial.println(" Button Press");
if(button_pinstate == LOW)// {
delay(10);
digitalWrite(enable_pin,HIGH);
myStepper.step(pos);
Serial.print(" Open ");
delay(2000);
myStepper.step(pos2);
Serial.print(" Close ");
digitalWrite(enable_pin,LOW);
delay(10);
}
static int counter = 0;
if ((++counter & 0x3f) == 0)
Serial.println();
delay(100);
keypad.getKey();
myStepper.step(0);
process_helper_light();
}
void keypadEvent(KeypadEvent eKey)
{
switch (keypad.getState())
{
case PRESSED:
// a key is pressed so light the helper light
helper_light_is_on = true;
digitalWrite(15,HIGH);
offtime = millis() + 5000; // set the offtime for 30 seconds in the future

Serial.print(" enter: ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey)
{
case '*':
checkPassword();
delay(1);
break;
case '#':
password.reset();
delay(1);
break;
default:
password.append(eKey);
delay(1);
}
}
}

void checkPassword()
{
if (password.evaluate()) //if password is right unlock door
{
Serial.println(" Accepted");
Serial.write(254);
delay(10);
digitalWrite(enable_pin,HIGH);
myStepper.step(pos);
Serial.print(" open ");
digitalWrite(16, HIGH);//turn on green led
delay(2000); //wait 5 seconds
digitalWrite(16, LOW);// turn offgreen led
myStepper.step(pos2);
digitalWrite(enable_pin,LOW);
Serial.print(" Close ");
}
else
{
Serial.println(" Denied"); //if passwords wrong keep door locked
Serial.write(254);
delay(10);
myStepper.step(0);
Serial.println(" locked ");
digitalWrite(17, HIGH); //turn on red led
delay(2000); //wait 5 seconds
digitalWrite(17, LOW);//turn off red led
}

void process_helper_light(void)
{
if (helper_light_is_on)
{
if (millis() >= offtime)
{
digitalWrite(15,LOW); //turn off the helper light
helper_light_is_on = false;
}
}
}

Discussions

0
None
steveastrouk

Best Answer 5 years ago

Where did you see it, so I can understand the context ? It looks like one of those clever-clever bits of code that C is notorious for.

0
None
WWC

5 years ago

While frollards answer was excelent with clarification from Kelseymh. The answer closest to what i was asking about, what was this counting for? was this:

  
Mar 15, 2013. 8:31 AMsteveastrouk says:
It would just pulse a line feed to the terminal. A bit pointless I agree.

Steve  


Thanks Steve, You are never ending help.
 

0
None
frollard

5 years ago

http://arduino.cc/en/Reference/Static
Static variables are visible only to the function that calls them, and they persist between calls.

Every time you ever see ++variable, you increment and save that variable to itself (in one tidy function)
http://arduino.cc/en/Reference/Increment

& is bitwise AND. It takes the 2 values in binary and does an AND on each bit of those numbers, and returns the result.
http://arduino.cc/en/Reference/BitwiseAnd

Your code
***
static int counter = 0;
if ((++counter & 0x3f) == 0)
Serial.println();
***

Counter starts at value zero,
if compares the values of (one more than counter, saved = 1)
now 0x01 = B00000001
AND 0x3f =B00111111, result is B00000001

That does not equal zero, if statement does not fire.

Again, the if statement has no curly braces, so it only fires the next line of code when the statement finally does become true. The statement will be true 63 counts later, where counter will = 64, (B01000000).
counter = 64 = B01000000)
AND
mask = 63 = B00111111)
=0
statement is now true (result equals zero) and the statement fires, in this case causing a println();

delay(100); //wait 1/10 second
keypad.getKey(); //check the keypad
myStepper.step(0); //do nothing (step zero times), remove?


*****Unfortunately, Counter is set to a value of Zero EVERY TIME the loop loops, so it will NEVER EVER increment higher than 1, and the if statement will NEVER fire.

0
None
WWCfrollard

Answer 5 years ago

Thanks for the input and the links

0
None
kelseymhfrollard

Answer 5 years ago

Hi Frollard. At the end you wrote, "*****Unfortunately, Counter is set to a value of Zero EVERY TIME". That isn't true. The variable is declared static, so the initializer is applied only once, when the variable is created (i.e., when the function is called the first time). After that, it continues to increment. This is a typical pattern for singletons:


class Foo {
public:
static Foo* GetInstance() {
static Foo* theInstance = 0;
if (!theInstance) theInstance = new Foo;
return theInstance;
}
};


(I've left out the rest of the class declaration for clarity.) The if-block above is not necessary in this simple, single-threaded case, but if you want to have a separate static instance for each thread of a multithreaded application, then you do need the separate instantiation.
0
None
WWCkelseymh

Answer 5 years ago

Thanks for the input

0
None
frollardkelseymh

Answer 5 years ago

That helps clarify a bit! Thanks!

0
None
steveastroukfrollard

Answer 5 years ago

*****Unfortunately, Counter is set to a value of Zero EVERY TIME

Spotted that too, but obfuscated C is so tricky it could just have been Clever.....

0
None
kelseymhsteveastrouk

Answer 5 years ago

Hi, Steve. As I noted to Frollard, that isn't true. With a static declaration, the initializer is invoked only once, when the variable is first allocated.

0
None
steveastroukkelseymh

Answer 5 years ago

.......but obfuscated C is so tricky it could just have been Clever.....

Thanks for the heads up, my pet languages are Delphi, Pascal and lately ADA.....

0
None
WWCfrollard

Answer 5 years ago

This supposed to be counting for the helper light?

0
None
steveastroukWWC

Answer 5 years ago

No, it sends a string to the serial port every 64 times through the loop.

0
None
WWCsteveastrouk

Answer 5 years ago

If i comment it out i can see no difference in the programs operation, so what info is this sending to the serial monitor, the Serial.println(); has no info to send. I know frollard said it was not working. If it was working how would this help us?

0
None
steveastroukWWC

Answer 5 years ago

It would just pulse a line feed to the terminal. A bit pointless I agree.

Steve

0
None
steveastroukWWC

Answer 5 years ago

Put code in there to toggle the LED then.

0
None
WWCsteveastrouk

Answer 5 years ago

In the loop, one block of code then the static int is next

0
None
steveastroukWWC

Answer 5 years ago

No, I meant where did you find the snippet !

0
None
WWCsteveastrouk

Answer 5 years ago

oh woops. It is my keyless entry with stepper we were working on last week.

0
None
WWCsteveastrouk

Answer 5 years ago

Not sure where it came from but it is in there