How to run a micro without USB?

Hello I have some troubles to run my sketch without an usb connection. If my micro is connected to my pc all runs fine. If i power it with an extern source, the sketch stops working and the rx led is turned on steady. I have commented out every line that contains 'Serial.' and double checked it with 'grep -v '//' sketch.ino | grep 'Serial.' Can someone help me out?

greetings Sebastian

How are you powering the board and what type of board is it?

It is an Arduino Micro. I have regulated 5V connected to the 5V pin right beside the reset pin.

Regulated 5v

From what? A wall wart?

And the ground is connected?

Does BLINK work?

Measure the 5 volts. What is it?

bigred1212:

Regulated 5v
From what? A wall wart?
And the ground is connected?
Does BLINK work?

A selfmade step down regulator.
The blink example works without USB.

LarryD:
Measure the 5 volts.
What is it?

The exact voltage is 4.96V and the ripple is very low.

I did some further testing. I have powered the micro with my android phone charger.
The same result. It only works with an usb data connection.
So i assume it is my code.
I only use software serial and Serial1 for the native serial port and commented out every
use of the usb port.

#include <SD.h>
#define CS 2 //ChipSelect Pin
#define RFID_POWER 8
#define MUTE_A1 3
File newFile;
File keyFile;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 3); // RX, TX
unsigned char buffer[512];
unsigned short bytes_received;
boolean update = false;
unsigned long update_start;
unsigned long newfilesize;
boolean debug=false;


void setup()
{
  //Serial.begin(115200);
  //while (millis()<10000) debug=Serial;
 //  while (!Serial) { ;}
  
  Serial1.begin(115200);
  Serial1.setTimeout(1000);
  if(debug) Serial1.println("Serial1");
  
  mySerial.begin(9600);
  while(mySerial.available()) mySerial.read();
  
   pinMode(CS, OUTPUT);
   pinMode(RFID_POWER, OUTPUT);
   pinMode(MUTE_A1, OUTPUT);
   
  if (!SD.begin(CS)) {
  //  if(debug)Serial.println("initialization failed!");
    Serial1.print(0x7701);
  } else {
  //  if(debug)Serial.println("initialization done");
  }
}

boolean error()
{
  //if(debug)Serial.println("error");
  return false;
}

boolean wrong_key()
{
  //if(debug)Serial.println("wrong_key");
  return false;
}

boolean read_rfid()
{
//  unsigned char rfid[255];
  unsigned long starttime=millis();
  unsigned long timeout=100;
  unsigned short bytes=0;
  char i;

  while((millis()-starttime)<timeout)
  {
    if (mySerial.available())
    {
      buffer[bytes] = mySerial.read();
      bytes++;
      starttime=millis();
    }
  }
  digitalWrite(RFID_POWER, HIGH);
  for (i=0;i<bytes;i++)
  {
    //if(debug)Serial.print(buffer[i],HEX);
    //if(debug)Serial.print(" ");
  }
  //if(debug)Serial.println();
  if (verify_key(buffer, bytes))
  {
    //if(debug)Serial.println("passt");
  }
  else
  {
    //if(debug)Serial.println("falsch");
  }
}

boolean verify_key(unsigned char rfid[],short bytes)
{
  unsigned short key_count, i, j, length;
  boolean a;
  boolean b;
  boolean key_match=true;
  keyFile = SD.open("KEY.BIN", FILE_READ);
  if (!keyFile) 
  {
    //if(debug)Serial.println("SD.open fehlgeschlagen");
    return error();
  }
  keyFile.read();
  key_count=keyFile.read()<<8;
  key_count=key_count | keyFile.read();
  keyFile.read();
  for (i=0;i<key_count;i++)
  {
    length=keyFile.read()<<8;
    length=length | keyFile.read();
    unsigned char key[length];
    for (j=0;j<length;j++) key[j]=keyFile.read();
    keyFile.read();keyFile.read();
    if(bytes==length)
    {
      for (j=0;j<length;j++)
      {
        if(rfid[j]!=key[j])
        {
          key_match=false;
          break;
        } 
      }
      if (key_match)
      {
        keyFile.close();
        return true;
      }
    }
    key_match=true;
  }
  keyFile.close();
  return false;
}

unsigned short receive_keys()
{
  bytes_received=0;
  unsigned short bytes_written=0;
  unsigned long transfer_start=millis();
  unsigned char copy;
  unsigned short rv;
  
  while((millis()-transfer_start)<200)
  {
    while(Serial1.available())
    { 
      buffer[bytes_received++]=Serial1.read();
      transfer_start=millis();
    }
  }
  //if(debug)Serial.print("bytes received: ");if(debug)Serial.println(bytes_received);
  for(int z=0;z<=bytes_received;z++){
    //if(debug)Serial.print(buffer[z],HEX);
    //if(debug)Serial.print(" ");
  }
  //if(debug)Serial.println();

  if(buffer[0]=='U' && !update)
  {
    SD.remove("KEY.NEW");
    newFile = SD.open("KEY.NEW", FILE_WRITE);
    update_start=millis();
    update=true;
    newfilesize=0;
    if(!newFile)
    {
      update=false;
      newFile.close();
      return 0x7601;
    }
  }
  if(update && bytes_received<511)
  {
    bytes_written=newFile.write(buffer, bytes_received);
    newfilesize=newfilesize+bytes_received;
    //if(debug)Serial.print("bytes_written: ");if(debug)Serial.println(bytes_written);
    newFile.close();
    update=false;
    //if(debug)Serial.print("update zeit: ");if(debug)Serial.println((millis()-update_start));
    if(bytes_written != bytes_received) return 0x7602;
    rv=check_file();
    if(rv!=0x7600) return rv;
    rv=copy_file();
    if(rv!=0x7600) return rv;
    return 0x7600; 
  }
  if(update)
  {
    bytes_written=newFile.write(buffer, bytes_received);
    newfilesize=newfilesize+bytes_received;
    //if(debug)Serial.print("bytes_written: ");if(debug)Serial.println(bytes_written);
    Serial1.write(0x76);
    Serial1.write(0xFF);
    if(bytes_written != bytes_received)
    {
      update=false;
      newFile.close();
      return 0x7601;
    }
  }
}

unsigned short copy_file()
{
  unsigned short i,tmp;
  unsigned long bytes_copied=0;
  SD.remove("KEY.BIN");
  keyFile=SD.open("KEY.BIN", FILE_WRITE);
  if(!keyFile) return 0x7604;
  newFile=SD.open("KEY.NEW", FILE_READ);
  if(!newFile) return 0x7605;
  //if(debug)Serial.print("newfilesize: "); if(debug)Serial.println(newfilesize);
  while((newfilesize-bytes_copied)>512)
  {
    for(i=0;i<512;i++) buffer[i]=newFile.read();
    tmp=keyFile.write(buffer, 512);
    if(tmp<512) return 0x7605;
    bytes_copied += tmp;
  }
  for(i=0;i<(newfilesize-bytes_copied);i++) buffer[i]=newFile.read();
  tmp=keyFile.write(buffer, (newfilesize-bytes_copied));
  if(tmp<(newfilesize-bytes_copied)) return 0x7605;
  bytes_copied += tmp;
  keyFile.close();
  newFile.close();
  //if(debug)Serial.println("file copied");
  return 0x7600;
}

unsigned short check_file()
{
  unsigned short i,j;
  unsigned short key_length;
  unsigned short sum1,sum2;
  unsigned short fletcher1,fletcher2;

  newFile=SD.open("KEY.NEW", FILE_READ);
  if(!newFile)
  {
    newFile.close();
    return 0x7603;
  }
  
  newFile.read();
  unsigned short keys = newFile.read()<<8;
  keys=newFile.read();
  newFile.read();

  for(i=0;i<keys;i++)
  {
    key_length=newFile.read()<<8;
    key_length=key_length|newFile.read();

    fletcher1=0;
    fletcher2=0;
    
    for(j=0;j<key_length;j++)
    {
      fletcher1 = (fletcher1 + newFile.read()) % 255;
      fletcher2 = (fletcher2 + fletcher1) % 255;
    }
    sum1=newFile.read();
    sum2=newFile.read();

    if(sum1!=fletcher1 || sum2!=fletcher2) return 0x7605;
  }
  return 0x7600;
}

void loop()
{
  if(update && (millis()-update_start)>180000)
  {
    while(Serial1.available()) Serial1.read();
    //if(debug)Serial.println("update abgebrochen");
    update=false;
  }
  if (Serial1.available())
  {
    //Serial.println(receive_keys());
    receive_keys();
    digitalWrite(RFID_POWER, HIGH);
  } else {
  if(!update)
  {
    digitalWrite(MUTE_A1, HIGH);
    digitalWrite(RFID_POWER, LOW);
    if (mySerial.available()) read_rfid();
    digitalWrite(MUTE_A1, LOW);
    delay(5);
  }
  }
}

Now i have replaced all the stuff in loop() with {;} If i comment out all the lines related to the SD library the micro runs alone. The chipselect pin is digital 2. changing it to 7 doesn't help.

This may be a really stupid question - How do you know your sketch isn't working?

...R

This may be a really stupid question - What make you think that the Arduino Micro had two native serial port?

Robin2: This may be a really stupid question - How do you know your sketch isn't working?

The rx led is on all the time and D3 should be triggered frequently when data is received on the sofwareserial port.

BillHo: This may be a really stupid question - What make you think that the Arduino Micro had two native serial port?

I don't think so. One real usb and one real serial.

while((millis()-transfer_start)<200)
  {
    while(Serial1.available())
    { 
      buffer[bytes_received++]=Serial1.read();
      transfer_start=millis();
    }
  }

I think this is meant to allow for a timeout but I’m not sure that it does. I think it can block indefinitely.

Anyway …

If this was my problem I would start with a minimal sketch that works. I would have a special LED flash to confirm that it is working, one that has no other role. An awful lot of people who post code here seem to have assumed it will work. I always assume my code won’t work and build in stuff that allows me to verify it piece by piece and identify where it is failing. This is especially important if you are not connected to the PC and can’t view debug prints in the Serial Monitor.

Then I would add other segments of the application one at a time, making sure that each works before moving on to the next.

I believe this would be more instructive than trying to eliminate segments by commenting things out.

And I’m afraid I’m too lazy to study all of your code carefully. Short pieces of code that demonstrate a problem are much better.

…R

Hi Robin I have started with a minimal sketch like you do. And then it grew and grew... And then came the point were everything worked fine and I disconnected the usb cable :) Tomorrow I will follow your tip and delete everything below setup() and put only blinking in loop().

thanks so far

As running without the USB connection is a critical requirement test it like that at every stage.

...R