3 way switch using interrupts

Learing how to code the arduino uno. Working this practice problem that is supposed to be a 3way switch to light an LED using interrrupts. i keep getting an error and i dont know why. Help please?

const byte LED = 10;
const byte PBInterrupt = 2;
const byte PB2Interrupt = 3;

volatile byte state = HIGH;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(PBInterrupt, INPUT_PULLUP);
pinMode(PB2Interrupt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PBInterrupt), blink, CHANGE);
attachInterrupt(digitalPinToInterrupt(PB2Interrupt), blink, CHANGE);
}

void loop() {
if (((PBInterrupt == LOW || PB2Interrupt == LOW) && LED == LOW))
{digitalWrite(LED, state);}
else if (((PBInterrupt == LOW || PB2Interrupt == LOW) && LED ==HIGH))
{digitalWrite(LED, state);}

void blink() {
state = !state;
}

The reason it won't compile is blink() needs to be defined before setup() and you are missing a closing brace in loop(). However, even if you get it to compile it will not work.

I can't tell from the code how it is supposed to work and your description doesn't make sense to me. Can you better describe how the switches are supposed to make the LED light? Should it light when either switch is pressed? Should it only stay lit while the switch is pressed?

The following condition will never be true:

  if (((PBInterrupt == LOW || PB2Interrupt == LOW) && LED == LOW))

PBInterrupt, PB2Interrupt, and LED are pin numbers and they are constants so they will never equal LOW. I assume you meant the following:

  if (((digitalRead(PBInterrupt) == LOW || digitalRead(PB2Interrupt) == LOW) && digitalRead(LED) == LOW))

If that is indeed what you meant and you are reading the switches in loop() then why do you need the interrupt?

The reason it won't compile is blink() needs to be defined before setup(). . .

Don’t underestimate the Arduino IDE C++ “extension” for creating hidden function prototypes.

6v6gt:
Don’t underestimate the Arduino IDE C++ “extension” for creating hidden function prototypes.

You are correct. Just adding the closing brace for loop() makes it compile because without that the IDE can't properly create the blink prototype. I keep forgetting about the IDE "extension".

It is easy to forget but you really start paying attention to it when it puts the prototype in the wrong place, say before the declaration of the data type of arguments used in that prototype, giving rise to mysterious and inappropriate error messages.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.