Logic vaildation of port levels by a switch

I am using this line:

int switchcount = (!digitalRead(2) * 2 + !digitalRead(0)) + 1;

and the port 2 never registers. The switches are 1k ohm pullups to v+ so when they are on the port should read low. I invert each digitalread to ascribe to positive logic and not NAND. Port 0 works just fine.

Any help would appreciated.

If that is valid code, I don't understand its purpose. Maybe explain it for us mere mortals.

Create a 2 bit digital number that equates to 0 through 3 for a case of 4 digital options represented by a integer. Read ports 2 & 0, Make port 2 read an msb(most significant bit) * 2 and port 0 as lsb(least significant bit).

won't the minimium value be 1?

const int PinBut0 = A1;
const int PinBut1 = A2;

void
loop () {
    Serial.println ((2 * ! digitalRead (PinBut1)) + ! digitalRead (PinBut0));
    delay (1000);
}

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

    pinMode (PinBut0, INPUT_PULLUP);
    pinMode (PinBut1, INPUT_PULLUP);
}

Post your code. Say what Arduino board you have in front of you.

Have you tried just printing the value returned by the digitalRead() of pin 2?

Since digitalRead on,y returns LOW or HIGH, your expression is correcter if you write

int switchcount = ((digitalRead(2) == LOW) ? 2 : 0) +  ((digitalRead(0) == LOW) ? 1 : 0);

Add that + 1 if you really want to see 1 .. 4, not 0 .. 3.

a7

When is our homework due?

On classic Arduinos port 0 is the serial port which could cause problems, depending on what else is attached.

Yes. As the default is 1 for a multiplier further in the code.

And the serial code will work on a Digispark?

//**************************************************************************************
int delayval = 11738; // microseconds Brain LED 40hz half cycle not specific
//int delayval = 59; // microseconds StunGun 6.5khz half cycle not specific
//int delayval = 23; // microseconds Ringing 21.4khz half cycle not specific
//int delayval = 14; // microseconds Ringing 32.4khz half cycle not specific
//The instruction cycle time has an effect on the timing.
int ontime = 500; // milliseconds Pod OnTime = 0.5 second
int waitinterval = 900; // wait is in milliseconds. 900 seconds equal 15 minutes
float minutes = .25; //default value for groups of 15 minutes
float seconds = minutes * 60;
float milliseconds = seconds * 1000;
// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(0, INPUT); //MOSFET Shield on Model B
  pinMode(1, OUTPUT); //LED on Model A  or Pro
  pinMode(2, INPUT); //LED on Model A  or Pro
  pinMode(3, OUTPUT); //LED on Model A  or Pro
  pinMode(4, OUTPUT); //LED on Model A  or Pro
  pinMode(5, OUTPUT); //MOSFET Shield on Model B
}
// the loop routine runs over and over again forever:
void loop() {
  prodloop();
}
void prodloop(){
  int offtime = 0;
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(ontime);  // wait for 25 milliseconds for 40 hz, 12 - 43.1khz, 13 - 39.8khz
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  offtime = timingswitches();
  delay(offtime);  // unsigned Long millseconds 900,000 = 15 minutes, wait till next pulse, production
}
int timingswitches() {
  int switchcount = (!digitalRead(0) * 2 + !digitalRead(2)) + 1; //msb, lsb
  // 900 seconds, 900,000 milliseconds = 15 minutes
  int tempinterval = 0; // default base setting
  tempinterval = switchcount * milliseconds;
  return tempinterval; 
}
```

If pins are only INPUT, do you have an external pullup or pull-down resistor?

Yes. 1k pullups. So I use an !(exclamation mark) to invert.

Problem solved. Micro slider switch used for testing not of high quality.

If you choose 2 adjacent pins on the same IO Port, you can read the whole port then mask the other bits off. You may have to shift the bits down but for a switch( maskedBits ) {} there will be 4 cases to cover, 4 values shifted or not.

Reading the port directly is many times faster than using Arduino digitalRead(), which you’d have to do TWICE if not for Direct AVR IO Port Manipulation… read/write up to 8 pins at a time! Note that on an Uno using Serial the most “free” pins on any Port is 6, not 8, and you get 3 Ports with 6 pins free-to-use. With a Mega2560 you get many full open ports to use with 52 IO pins.

BTW, in some old debounce routines I used bit 0 for the latest read and bit 1 for the previous. 01 and 10 are change detected with 1 byte. Before every read I left shifted the old bits 1 and the added the read in the next line, using digitalRead() for newbs.

My goto debounce expands on that; the byte tells the last 8 reads made ~1 ms apart, a change followed by all stable reads is debounce in under 10 ms. I can make it quicker by using fewer history bits with an and-mask.

Bitmath Tutorial for bit beginners. Arduino doc.

As was indicated digitalRead() returns HIGH or LOW. Good coding practice would be to use that and not some implicit promotion and assumptions