Switch from Uno to Mega. Why are my Programs completely broken?

So as my project continued to expand I quickly found my UNO didn't have enough digital pins to support the expansion, so I invested into a mega. However upon simply switching over the pins that were previously on the UNO to the MEGA and cleaning up the circuit on the breadboard, when I uploaded my code to the MEGA everything bar my ultrasonic sensors broke and I have no clue as to why.

1 of the bigger problems I've noticed is in my Enum's & BSM which are enforced by button presses see below.

void loop() 
{
//  || ------------------ Sensors Output Mode --------------------||
  int audOn = digitalRead(audioBtn);
  int vibOn = digitalRead(vibroBtn);
   
  if (audOn == HIGH)
  {
  //  output = AUDIO_MODE;
  //  vibroIntro = true;
  //  dualIntro = true;
  }
  if (vibOn == HIGH)
  {
   // output = VIBRATION_MODE;
  //  dualIntro = true;
  //  audioIntro = true;
  }
  if (vibOn == HIGH && audOn == HIGH)
  {
    //output = DUAL_MODE;
   // audioIntro = true;
   // vibroIntro = true;
  }

What I've noticed is that for some unknown reason the buttons are being detected as automatically pressed when they haven't. This only happens if I uncomment one of the following:

output = AUDIO_MODE;
output = VIBRATION_MODE;
output = DUAL_MODE;

Regards of which ones I uncomment they will be overriden by the DUAL_MODE (If its uncommented) Does any one know what is causing this bug because I've got other code like my light sensor (photocell) which is pretty much doing the same by forcing a different mode when it shouldn't be.

See rest of code below:

enum Output
{
  OFF_MODE,
  AUDIO_MODE,
  VIBRATION_MODE,
  DUAL_MODE
}output = OFF_MODE;

  // Buttons
  pinMode(audioBtn, INPUT);
  pinMode(vibroBtn, INPUT);

void loop() 
{
//  || ------------------ Sensors Output Mode --------------------||
  int audOn = digitalRead(audioBtn);
  int vibOn = digitalRead(vibroBtn);
   
  if (audOn == HIGH)
  {
    output = AUDIO_MODE;
  //  vibroIntro = true;
  //  dualIntro = true;
  }
  if (vibOn == HIGH)
  {
    output = VIBRATION_MODE;
  //  dualIntro = true;
  //  audioIntro = true;
  }
  if (vibOn == HIGH && audOn == HIGH)
  {
    output = DUAL_MODE;
   // audioIntro = true;
   // vibroIntro = true;
  }
  static Output lastMode = OFF_MODE;
  if(lastMode != output) // state change events
  {
    switch(output)
    {
      case OFF_MODE:
        Serial.println(F("Now in OFF_MODE"));
        break;
      case AUDIO_MODE:
        Serial.println(F("Now in AUDIO_MODE"));
        break;
      case VIBRATION_MODE:
        Serial.println(F("Now in VIBRATION_MODE"));
        break;
      case DUAL_MODE:
        Serial.println(F("Now in DUAL_MODE"));
        break;
    }
    lastMode = output;
  }
  switch(output)
  {
    case OFF_MODE:
      //  do OFF_MODE things
      Serial.println("---------------------------------------OFF MODE");
      break;
    case AUDIO_MODE:
      Serial.println("----------------------------------------- AUDIO");
     /* if (audioIntro)
      {
        Serial.println("---------------------------------------Started Audio Intro");
        tone(buzzer, 300);
        delay(50);
        tone(buzzer, 520);
        delay(50);
        tone(buzzer, 240);
        delay(50);
        tone(buzzer, 880);
        delay(50);
        audioIntro = false;
      }*/
      break;
    case VIBRATION_MODE:
      Serial.println("----------------------------------------- VIBRATE");
     /* if (vibroIntro)
      {
        Serial.println("---------------------------------------Started Vibro Intro");
        vibroIntro = false;
      }*/
      break;
    case DUAL_MODE:
      Serial.println("----------------------------------------- BOTH MODE");
     /* if (dualIntro)
      {
        Serial.println("---------------------------------------Started Dual Intro");
        tone(buzzer, 600);
        delay(50);
        tone(buzzer, 520);
        delay(50);
        tone(buzzer, 740);
        delay(50);
        tone(buzzer, 480);
        delay(50);
      dualIntro = false;
      }*/
      break;
  }
}

Perhaps the newer C++ standards allow you to declare an instance of an enum inside the definition, but I would do this:

enum Output
{
  OFF_MODE,
  AUDIO_MODE,
  VIBRATION_MODE,
  DUAL_MODE
};

Output output = OFF_MODE;

Sadly that delivers the same result.

Hi,

you say that one of your problems resides withe the switches.

Have you tried int audOn = !digitalRead(audioBtn); int vibOn = !digitalRead(vibroBtn);

I am very new and trying to understand whatever I can about this.

Is the connection from the pin to the switch using a pullup/down resistor? That could explain why the button seems funky on a different device. If you just chop up the code to test the switch, does it act right?

jbellavance: Hi,

you say that one of your problems resides withe the switches.

Have you tried int audOn = !digitalRead(audioBtn); int vibOn = !digitalRead(vibroBtn);

So I try that and it sets it into off_mode which is what it's meant to do. However when I press audio button it will switch but if I press the vibro button it won't switch until I pull out the jumpwire and press the tip with my finger.

If I put !digitalRead as the mode for when the resistors/wires are on the left side of the breadboard that works. If I put digitalRead and put it on the right side of the breadboard it will show OFF_MODE as well

Sounds like a wiring problem.

Are the wires long? Are they alonside other signals (crosstalk)

As Gliscameria said: are using pullup or pulldown resistors?

As I can't see your setup, right or left of the breadboard is of no help to me.

Try to make sure that the switches work ok before continuing debugging (use a new sketch that will do just that)

Cheers

The only thing I can possibly think of is that I replaced all larger wires with jumpwires which had no male headers so I can to insert them myself. So I'll give that a switch and see what happens.

My breadboard as you can see is all wired fine for the buttons and yet here in and the code lies a problem that is forcing the modes over.

Fixed the image.

  if (audOn == HIGH)
  {
    output = AUDIO_MODE;
  }
  if (vibOn == HIGH)
  {
    output = VIBRATION_MODE;
  }
  if (vibOn == HIGH && audOn == HIGH)
  {
    output = DUAL_MODE;
  }

Would’nt you want to use an if statement to put “output” in OFF_MODE if no switch is pressed? If not, the last mode prevails from loop to loop. If it is what you want, then it’s OK not to force OFF_MODE. But then there is no way in your setup to place it back in OFF_MODE once it has been switched to another mode.

Other than that, your code looks OK to me. So, we are back to the wiring.

On your breadboard I suppose that audioBtn and vibroBtn are the two on the left. (the resistors on the two on the right seem unconnected)

I have attached a sketch to test the switches. Just change the pin numbers to match your setup.

testBtns.ino (297 Bytes)

Just switched back to the arduino and its now doing the exact same and yet 4 hours prior to my switch over to the Mega. Exact same code working perfectly fine. fml...

The audio & vibrobtn were the ones on the right hand side. The others are to do with something different but are also repeating the same bug. At current setup the only purpose of off_mode is just as a stating state. I would then click on either the audioBtn or vibroBtn or press both of them to switch different modes and then press them again to switch to an different mode.

Just tested with your test and this what I get regardless if button is pressed down or not. |500x160

That brings you back to the wiring... it seems.

Have you connected back the resistors on those switches? on the photograph they seem unconnected.

Is the upper rail connected to 5V?

And the lower rail connected to ground?

I brought out new resistors, wiring and buttons, placed them on the breadboard and I get the exact same result.

We need to see all the wiring at this point, not just the breadboard.

Run some simple example sketches on the Mega. Rule out problems of uploading.

Small question. is it possible for the resistors or buttons to get fried by the breadboard?

Cause I just need a test with an led on a brand new button:

int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

When the button wasn't pressed it would do the opposite of the code by keeping the LED lit and when pressed it would dim the light.

I then edited the wiring about whilst the usb was connected (done this in the past and still works) and connected to the opposite of the breadboard (this is connected up as well) and then the LED would stay on regardless if button is pressed or not. When I went to revert the cabling back the led stayed on and the button wouldn't turn the led off.

is it possible for the breadboard to short-circuit the components?

No. Not in the circuit that you have.

Try with the internal pullup-resistor

switch pin 1 to arduino's ground, switch pin 2 to arduino's pin 7

CHANGE pinMode(inPin, INPUT); // declare pushbutton as input FOR pinMode(inPin, INPUT_PULLUP); // declare pushbutton as input

The led should be OFF when the switch is pressed.