Arduino does not boot... sometimes?!?

Hi!

I took my Arduino and build a device with it. I "hardwired" the Arduino board (since
I need the USB-Port) inside a box as a permanent installation.
To recieve a feedback that that Arduino is booted and ready I implemented a soundbuzzer
that beeps, when the Arduino is ready to recieve commands via USB.
The problem is that the Arduino does not boot/beep when USB is plugged in, <>. It also
does not accept any commands when it has not beeped.
Only when I upload the sketch again it works for a few times then this behaviour happens again.
I allready replaced the atmega (because after a while it didnt work anymore even when uploaded the sketch again)
but with the new one it happens again and now Im afraid that it will die too... :frowning:
(Since I dont really know what the problem is I hope this is not the wrong forum)

This is my initialisation code:

//////////////////////////////////////////
// scribLAB´s CNC Control ////////////////
//////////////////////////////////////////

// SETUPS --------------------------------------------
#include <string.h>
#include "AxisCtrl.h"
#include <EEPROM.h>
#include "Speaker.h"
#include "ToolRemote.h"

//Axis Control
///////////////////////////
AxisCtrl x(12,9,7,200,0.8,10);
AxisCtrl y(11,9,6,200,0.8,30);
AxisCtrl z(10,9,8,200,0.8,40);

Speaker sp(5);
ToolRemote toolRemote(3,4);

//AxisControlButtons
///////////////////////////
boolean homeAll = false;
boolean homeZ = false;
boolean calibrateZ = false;
boolean selfTest = false;
int ledPin = 13;
long x2 = 0;
long y2 = 0;
long z2 = 0;
//Communication
///////////////////////////
String gCodeCmd = "G01";
String planeSel = "XY";
String cmdString;
boolean serialHandshake = false;
boolean cmdComplete;
boolean readyForNextCmd = true;
boolean otherCmdRecieved = false;
// SETUPS --------------------------------------------

void setup() {
  //Serial conncetion
  sp.begin();
  toolRemote.begin();
  sp.playStart();
  cmdString.reserve(200);  
  Serial.begin(115200);
  Serial.println("Init machine...#");
  x.begin();
  x.setDirOverride(true);
  y.begin();
  z.begin();
  Serial.println("Machine ready.#");
  pinMode(ledPin,OUTPUT);
  //Establish contact
  Serial.write("R");
}

What could be the problem? Software, hardware? :\

Thanks in advance!

Try and narrow down where it gets stuck.
When you open the serial monitor, do your Serial.prints & writes work?
Is the code actually making it to the last line of setup?
Use the output LED as a test point too.
move the
pinMode(ledPin,OUTPUT);
farther up in setup.
Do things like after every library call make it blink on for a half second, off half second, see where the problem is.

What could be the problem? Software, hardware?

Those are the usual two choices. I'm voting for software, though.

Starting here:

String gCodeCmd = "G01";
String planeSel = "XY";
String cmdString;

Get rid of this crap. Use a char array, of a known size, and reject any input data that is larger than that size.

Although, given that you don't have a loop() function, never mind.

Thanks guys for your replies!

CrossRoads:
move the
pinMode(ledPin,OUTPUT);
farther up in setup.

I made this change and moved the pinMode() up and it seems that this was the problem. It
seems to boot everytime until now.
Just for knowledge purposes: why should one make all pinModes() first before doing other
setup stuff?

Thanks!

pin 13 is also the SCK pin for SPI transfers.
Which you don't appear to be using, but it may have been interfering with the other libraries called out first.
Having it called before you started making library calls my have removed the interference.

Thank you very much for explanation! :slight_smile: