Variable and Function conflict

Hello everyone,

I am trying to get the program running correct, and I am just trying to get the PORTB input to compare to an array, and then turn on a green LED if the PORTB input = one of the array pin numbers, or red if it doesn’t. Any assistance would be appreciated

I am getting the following error while compiling:
In function ‘void loop()’:
18:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
25:19: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

my code is:

int validPIN[10]={0001, 0111, 1110, 1000, 0100, 1011, 1001, 1101, 1010, 0101};
const int greenLED = 5;
const int redLED = 6;
const int yellowLED = 7;//represents physical locking mechanism
int PINentry = PINB;

void setup()
{
DDRB=B000000; // Configures PORTB as input
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
}

void loop()
{

if (PINentry == validPIN) {
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
digitalWrite(yellowLED,HIGH);
delay(20000);
digitalWrite(yellowLED,LOW);
}
if (PINentry != validPIN) {
digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH);
digitalWrite(yellowLED,LOW);
}
}

code for PIN lock.txt (1.31 KB)

what exactly do you think you're comparing in

if  (PINentry == validPIN) {

what are the values of PINentry and validPIN?

I want PINentry to equal whatever input it is given by PORTB. And validPIN should be one of the values in the validPIN[10] array.

I am trying to get the input of PORTB compared to the validPIN array. If the PORTB entry is equal to one of the numbers in the validPIN array then the green LED should go on, if not then the red LED should go on

And validPIN should be one of the values in the validPIN[10] array.

which array element is it in the comparison?

what does "validPIN" represent in the comparison?

So the 10 "PIN" numbers in the validPIN array are all valid numbers to "unlock" and turn on the green LED.

So the 4 digit PIN input by PORTB should be compared to all of the validPIN array elements. If it equals one of them then the led should be green, and if it doesnt equal any of the array elements then the red LED should com on.

the 4 digit PIN input by PORTB should be compared to all of the validPIN array elements. If it equals one of them then the led should be green, and if it doesnt equal any of the array elements then the red LED should com on.

That sounds reasonable, but where in your code do you test each of the entries in the array ?

As it stands you are comparing PORTB with the address of the array, not its contents

Do some research on for loops as a starting point

int validPIN[10]={0001, 0111, 1110, 1000, 0100, 1011, 1001, 1101, 1010, 0101}; That’s going to bite you on the ass.

Have some of these: 0b 0b 0b

C++ is a very low level language, nothing fancy happens unless you code it - if you have an array and an integer it has no operations available between them as they are different structurally.

Consider writing a function to compare the integer with each element, suitably named, and use that in
the condition.

The error message may seem confusing because C/C++ treat an array name to be a pointer to the zeroth
element of the array. The array itself resides in memory, and crucially is not an object in its own right as
in many more advanced languages.

TheMemberFormerlyKnownAsAWOL:
int validPIN[10]={0001, 0111, 1110, 1000, 0100, 1011, 1001, 1101, 1010, 0101}; That's going to bite you on the ass.

Have some of these: 0b 0b 0b

In C and C++ any string of digits starting with a zero is treated as base 8, ie octal. So that line is equivalent to

int validPIN[10]={1, 73, 1110, 1000, 64, 1011, 1001, 1101, 1010, 65};

MarkT:
In C and C++ any string of digits starting with a zero is treated as base 8, ie octal. So that line is equivalent to

int validPIN[10]={1, 73, 1110, 1000, 64, 1011, 1001, 1101, 1010, 65};

Thanks for the info! It would have bite me in the ass

int PINentry = PINB;

What are you expecting this to do? As written in the code, you are declaring a global variable PINentry and setting its initial value to the state of the port B input ports at the time the code initially runs (either when the arduino is first powered on, or when it is reset). Nowhere in the code is this value ever changed. It is a common misconception that this will cause PINentry to be updated if the state of the input port changes, but that does not happen automatically.

You are comparing the 8 bits of PORTB with values where only 4 bits are defined. If any of those unused inputs float HIGH your comparison will fail. You should mask PORTB with B00001111 (or B1111 or 0b1111 or 0x0F) before doing the comparison to make sure those upper bits are 0.

This is how I would do it:

const byte ValidPINs[10] = {B0001, B0111, B1110, B1000, B0100, B1011, B1001, B1101, B1010, B0101};


// I/O Pins. (Note: also uses PORTB: Pins 8-13 on the UNO)
const byte GreenLEDPin = 5;
const byte RedLEDPin = 6;
const byte LockPin = 7; //represents physical locking mechanism


void setup()
{
  DDRB = B000000; // Configures PORTB as input


  digitalWrite(GreenLEDPin, LOW);
  pinMode(GreenLEDPin, OUTPUT);


  digitalWrite(RedLEDPin, HIGH);  // Locked
  pinMode(RedLEDPin, OUTPUT);


  digitalWrite(LockPin, LOW);
  pinMode(LockPin, OUTPUT);
}


void loop()
{
  byte PINentry = PINB & B00001111; //Read bottom 4 bits of PORTB
  boolean unlocked;


  // Note: 'static' means 'keep this variable set between function calls'
  static boolean alreadyUnlocked = false;


  unlocked = false;
  for (int i = 0; i < 10; i++)
  {
    if  (PINentry == ValidPINs[i])
    {
      unlocked = true;
      break;  // No need to test any more so break out of the 'for' loop
    }
  }


  // State Change Detection
  // Only unlock if not already unlocked
  // Without this the unlock cycle would repeat as long as the
  // bit pattern matched any valid PIN
  if (unlocked && !alreadyUnlocked)
  {
    // Unlock
    digitalWrite(GreenLEDPin, HIGH);
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(LockPin, HIGH);


    // Keep unlocked for 20 seconds
    delay(20000);


    // Relock
    digitalWrite(LockPin, LOW);
    digitalWrite(GreenLEDPin, LOW);
    digitalWrite(RedLEDPin, HIGH);
  }


  // Remember the previous state for State Change Detection
  alreadyUnlocked = unlocked;
}