Program only seems to run properly immediately after uploading

I’m not convinced it’s a bootlader issue since I’ve run other programs without problems. I wrote a quiz show sketch, hacking into some domed buttons that light an LED on the base and locks out the other buttons until a reset is pressed. When I upload the sketch, everything works great, but after losing power and restoring it (without uploading the sketch) it gets… stuck. It acts as if one of the buttons has already and is continually being pressed and ignores the reset button.

Before I post the code, I admit to realizing two things:

  1. It’s entirely possible I borked my wiring somewhere, but I don’t understand why it would make a difference since it works as expected, only after I upload it.

  2. I might need to show my work, as it were, but I don’t have my schematic handy at the moment. I’m hoping there will be something glaring with my sketch that someone can just point out my mistake and I can move on, but i’ll work up a Fritzring if all else fails.

Anyhoo, here’s the sketch:

#include <FastLED.h>
#define LED_PIN     6
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    1
#define BRIGHTNESS  50

CRGB leds[NUM_LEDS];

const int BButtonPin = 9;
const int GButtonPin = 10;
const int RButtonPin = 11;
const int YButtonPin = 12;
const int masterButtonPin = 7;

int buttons[] = {
  BButtonPin, GButtonPin, RButtonPin, YButtonPin
};

int activePins[4];

String colors[] = {
  "Blue", "Green", "Red", "Orange"
};

int buttonLock = 1;
int pinCount = 0;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);

  for (int thisPin = 0; thisPin < 4; thisPin++) {
    pinMode(buttons[thisPin], INPUT);
    if (digitalRead(buttons[thisPin]) == LOW) {
      //int pinNum = buttons[thisPin];
      //Serial.println(pinNum);
      activePins[thisPin]=buttons[thisPin];
      Serial.print("Pin Active: ");
      Serial.println(buttons[thisPin]);
      pinCount=pinCount+1;
    }else{
      activePins[thisPin]=0;
    }
  }
  pinMode(masterButtonPin, INPUT_PULLUP);
  Serial.print("Pin Count: ");
  Serial.println(pinCount);
  leds[0] = CRGB::Black;
  FastLED.show();
  buttonLock = 0;
}

void loop() {
  if (buttonLock == 0 && pinCount!=0) {
    for (int i = 0; i < 4; i++) {
      if (digitalRead(buttons[i]) == HIGH && activePins[i]!=0) {
        changeLED(i);
        buttonLock = 1;
        Serial.println(colors[i]);
        break;
      }
    }
  }
  if (buttonLock == 1) {
    if (digitalRead(masterButtonPin) == LOW) {
      buttonLock = 0;
      leds[0] = CRGB::Black;
      FastLED.show();
      Serial.println("reset");
    }
  }
};

void changeLED(int color) {
  switch (color) {

    case 0:
      leds[0] = CRGB::Blue;
      FastLED.show();
      break;
    case 1:
      leds[0] = CRGB::Green;
      FastLED.show();
      break;
    case 2:
      leds[0] = CRGB::Red;
      FastLED.show();
      break;
    case 3:
      leds[0] = CRGB::Orange;
      FastLED.show();
      break;
  }
};

Hi,

Are you using pin0 and pin1 as I/O.
They are reserved for programming the Arduino.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

This can cause problems as you are using Serial Monitor, which also uses pin0 and pin1.

Tom.. :slight_smile:

Some of your input pins are set to INPUT instead of INPUT_PULLUP. There is no schematic, so do your inputs have pullup or pulldown resistors?

Okay, here's some more details and a few answers that should help to clear things up.

First, my project is loosely based off of a tutorial on instructables here: Quiz Game. I'm hacking those same stand-alone buzzers, just slightly differently. I'm supplying voltage to the button in order to light the LEDs inside via the LEFT channel of the audio jack, Sleeve/Common is ground and RIGHT is used to read the button state.

The 4 quiz buttons are paired with 10k resistors. Only the "master" button is set as INPUT_PULLUP (I don't remember why I did this exactly, but I think it was because it's a 2 pole button with no voltage being routed to it).

I've included the Fritzing sketch below that hopefully helps to illustrate my connections better (minus the individual buttons' internal components seen in the link above) but to answer your question TomGeorge, no, I'm not using pin 0 or 1.

For a moment, assuming that all my wiring is good, is there any reason for it to not work unless a direct connection from IDE had JUST been made? I'm going to dig up another Uno to see if maybe it's the board itself, but I couldn't find any similar behavior aside from bootloader issues that only tangentially made sense.

Hi,

jedimasta:
I've included the Fritzing sketch below that hopefully helps to illustrate my connections better (minus the individual buttons' internal components seen in the link above) but to answer your question TomGeorge, no, I'm not using pin 0 or 1.

Why don't you post something like this?
Its still not a proper schematic, but it shows oodles much more information.
Although it lacks current limit resistors for its LEDs.

It is more informative.
Have your illuminated press buttons got internal current limit resistors for the LED in them?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Also show what the three connections to your buttons.
Sorry but Fritzy has not got any labels, for pins, components, where/what is the power supply...
The symbols it uses are not standard and again have no labels to indentify them.
Tom... :slight_smile:

Hi,

no, I’m not using pin 0 or 1.

This part of your code.

for (int thisPin = 0; thisPin < 4; thisPin++) {
    pinMode(buttons[thisPin], INPUT);
    if (digitalRead(buttons[thisPin]) == LOW) {
      //int pinNum = buttons[thisPin];
      //Serial.println(pinNum);
      activePins[thisPin]=buttons[thisPin];
      Serial.print("Pin Active: ");
      Serial.println(buttons[thisPin]);
      pinCount=pinCount+1;
    }else{
      activePins[thisPin]=0;
    }
  }

But you code does.
You are assigning pins 0 and 1 as inputs.
In fact your fritzy shows D0 to D3 not being used at all.
In the “destructible” link, it uses D2 to D5.
Tom… :slight_smile:

I think you may be misinterpreting my FOR statement. The int value is the index of the buttons/ activePins arrays, not the literal pin number of the board. It checks to see if there's a button actually hooked up (since I made this modular) and then writes a value into a separate array to compare against later. The loop only checks pins 9, 10, 11, and 12 whether or not they've just been pressed, provided it's considered an active button (by way of the activePins array). The master button is on D7.

As for the instructibles link, I'm not using his code, I just referenced the way he connected his buttons to the board.

The buttons are normally battery operated stand alone toys. Hit the button, it lights up and make a noise. I'm using panel mount stereo jacks to connect to the Uno. 2 poles of the jack are for VIN and ground, the other is spliced from the button's components to the Uno.

I'm sorry I don't have a clearer image for the circuit. The schematic view is a mess and, for the moment, I just don't have time to sit and draw things out. What isn't clear about the fritzing image? Aside from not having the power supply (5v via USB), I think it's all pretty clear, but I'm happy to explain if something is missing.

jedimasta:
The schematic view is a mess and, for the moment, I just don't have time to sit and draw things out.

Yet you hope that we have lots of time to waste guessing what you are doing...

aarg:
Yet you hope that we have lots of time to waste guessing what you are doing...

What I was hoping was that those more knowledgeable and experienced than I would offer insight to the challenge I'm having. I've provided all the information I have at hand. I didn't post my code and say "what's wrong". I explained in detail what I was trying to do, how I was going about it and my errors thus far.

I understand the frustration of having users post to boards that expect you to do their work for them, which is why I've included all that I have. If you don't want to help arrg, please don't.

You have ignored a request for important information in reply #5 [edit - in several replies...]. A schematic for that would help others help you enormously... whether it is me, or someone else.

I fail to see how this is any clearer than that breadboard image I provided earlier, but since I've become some sort of pariah without it, here is the schematic view, organized to the best of my ability.