Blink works fine in standalone but code I wrote only works when rest pressed

Hello everyone,

I looked all over and cant find my specific issue. I load my code using my arduino platform and then place the atmega328 in my breadboard, blink works fine (without having to press reset button). However, the code below only works if I press the reset button. After pressing the reset button all works fine… I’m really baffled as everything I’ve seen surrounding breadboard atmega328 issues don’t seem to address this unless I’m just not connecting the dots. I keep thinking I’m missing something in the loop setup but I’m having a problem finding it.

Any help would be greatly appreciated. This is the first time I’ve created a code by adding to someones existing code…

//Bluetooth portion of code created By: Angelo Casimiro (4/27/14)
//Voice Activated Arduino (Bluetooth + Android)
//Feel free to modify it but remember to give credit
#include <Servo.h>
String voice;
int light = 2; //Connect LED 1 To Pin #2 
Servo servo1;

void setup() {
  Serial.begin(9600);
  serial.read();
  pinMode(light, OUTPUT); 
  int servoPin=9;
  servo1.attach(servoPin);
  servo1.write(0);


}
//-----------------------------------------------------------------------//  
void loop() {
  while (Serial.available()){  //Check if there is an available byte to read
    delay(10); //Delay added to make thing stable 
    char c = Serial.read(); //Conduct a serial read
    if (c == '#') {
      break;
    } //Exit the loop when the # is detected after the word
    if (c == '*') {
      voice =""; 
      continue;
    }
    voice += c; //Shorthand for voice = voice + c

  }  
  if (voice.length() > 0) 
  {
    Serial.println(voice); 

    if(voice == "servo on") 
    {
      digitalWrite(light, HIGH); //Turn On led
      servo1.write(90);   //Move servo to 90 degrees
      delay(100);
    }
    else if(voice == "servo off")
    {
      digitalWrite(light, LOW); //Turn Off light
      servo1.write(178);   //Move servo to 0 degrees
      delay(100);
    }

    //-----------------------------------------------------------------------//  
    voice="";
  }
} //Reset the variable after initiating

Assuming you have a pull-up resistor on the reset pin, you might try to add a diode from the reset pin to +5vdc, with the cathode wired to +5vdc and anode to reset pin.

This is the current design used in the Uno and was to solve a very intermittent problem such as you are seeing where one had to hit reset to actually have the sketch start after a upload. Any small signal diode will work.

Hello and thank you for such a quick response!!! I do have a 10K pull up on the reset.. I will give your suggestion a try. Would this also explain why blink works and not my code?

Also forgive me, but do I use the diode instead of the pull up?

lantonyj: Also forgive me, but do I use the diode instead of the pull up?

Use both.

The problem was sensitive to what I/O pins had loads on them. It's worth a try, but it may not be your problem as it was triggered by the upload operation from the IDE, specifically the auto-reset pulse applied to the reset pin. You said you are uploading to a arduino board and then moving the chip so it may not help in your case. But as I said it's the standard design now and can't hurt to try.

I tried the diode and dint work :(

Is there anything in the code that maybe I'm missing? The only thing that happens when reset is pressed, is the setup runs again, correct?

lantonyj: I tried the diode and dint work :(

Is there anything in the code that maybe I'm missing? The only thing that happens when reset is pressed, is the setup runs again, correct?

Well I'm still feeling hardware caused. What kind of current capacity does your voltage source have? It might be sagging on power up and causing some kind of latch up, which is then cleared when you hit reset. Servos can be current hogs.

I'm using a breadboard power supply purchased from Amazon...

I thought about the servo being an issue so I removed it and still with just the 1 led, the same thing happens.

I reloaded the blink code with everything still attached including the servo and as soon as I give it power the LED begins to blink, which is wired to pin 13, without hitting the reset button...

I am no expert on the String object but I am pretty sure if(voice=="servo on") isn't going to do what you want. You prolly want something likeif(voice.equals("servo on")) but even then, I think "servo on" is a string array not a String object. Most people on this forum will advise you to avoid the String object all together.

Hello, thank you for replying… everything works fine except for having to press reset any time the power is removed from the breadboard or when I first install the chip on the breadboard. Once I press the reset button there are no other issues, until power is removed, are you saying that if I change this string, the need to hit reset will be eliminated?

Thank you again for replying

Try a dummy "serial.read();" after "Serial.begin(9600);" in Setup. I think it may be hanging on the serial interface.

Please go back and modify your first post, highlight the code section and put it in "code" tags with the [ # ] icon. It might be nice to use the "Tools=>Auto Format" option on the code in the IDE and re-insert it first.

Hello and thank you for your reply... I have tried your suggestion with no luck.. also made the changes to the post as well...

How about this?

//Bluetooth portion of code created By: Angelo Casimiro (4/27/14)
//Voice Activated Arduino (Bluetooth + Android)
//Feel free to modify it but remember to give credit
#include <Servo.h>
String voice;
int light = 2; //Connect LED 1 To Pin #2 
Servo servo1;

void setup() {
  Serial.begin(9600);
  Serial.read();
  pinMode(light, OUTPUT); 
  int servoPin=9;
  servo1.attach(servoPin);
  servo1.write(0);
  voice="";
  digitalWrite(light, LOW); //Turn Off light
  servo1.write(178);        //Move servo to 0 degrees


}
//-----------------------------------------------------------------------//  
void loop() {
  char c = Serial.read();   //Conduct a serial read
  if (c != -1) {  
    if (c == '#') {
      if (voice.length() > 0) 
      {
        Serial.println(voice); 
        if(voice == "servo on") {
          digitalWrite(light, HIGH); //Turn On led
          servo1.write(90);   //Move servo to 90 degrees
          delay(100);
        }
        else if(voice == "servo off") {
          digitalWrite(light, LOW); //Turn Off light
          servo1.write(178);   //Move servo to 0 degrees
          delay(100);
        }
        voice="";
      }
      else if (c == '*') {
        voice =""; 
      }
      else  voice += c; //Shorthand for voice = voice + c
    }
  }  
}

Hello Paul__B I tried the code you suggested and it didnt work at all.

I took retroleftys advise and looked at hardware, ordered new chips and also replaced the power supply from Amazon with a 7805 and brand new 10uF capacitors... still the same issue. :(

Oh I forgot to add...

I added "blink" in the code posted above just to see if its hardware and the LED begins to blink as soon as the chip has power. This is leading me to believe that either its something wrong with my code or the bluetooth module is somehow not working until the reset is pressed.