Author Topic: USB output, but standalone operation  (Read 6450 times)

Bergie

  • Newbie
  • *
  • Posts: 8
USB output, but standalone operation
« on: March 25, 2015, 08:25:00 am »
I don't know how to phrase the subject but here is what I am trying to achieve:

I want to log data to EEPROM with only the DC connected, no USB in sight.
Then after a period of time, collect the Digispark, plug into USB, and it should print out the contents of EEPROM as a keyboard.

I did a lot of coding to get data compression to work, extract data etc etc.
Test bench powered from the USB port everything work. Then I disconnect from USB, 5VDC only and nothing happens...
I can't even get an LED to flash by itself when I  "#include DigiKeyboard.h"

It looks like I have to have USB around to get the clock to run.
I looked in the libraries to see if there is a way to get it to run from the internal oscillator when the USB is not around.

Is there any suggestions how to get my code to run when I am not connected to USB? (Other than using a USB hub as power...)


{
Working method I am trying to achieve:

Upload code via USB.
Install Spark in sensor with DC only supply.
Write data to EEPROM.
Fetch Spark. Disconnect from DC.
Plug into USB fro power and comms.
Spark print EEPROM data as HID keyboard to PC.
}



DeuxVis

  • Full Member
  • ***
  • Posts: 107
Re: USB output, but standalone operation
« Reply #1 on: March 26, 2015, 06:36:49 am »
Can you tell us how you give it power when usb is not plugged ?

A Digispark should run fine without usb if you give it regulated 5V on the 5V line or 6V to 12V - or even more - on the VIN line.

Bergie

  • Newbie
  • *
  • Posts: 8
Re: USB output, but standalone operation
« Reply #2 on: March 27, 2015, 08:18:55 am »
As for power I have a simple "plug in" connector whereby I simply pull two links out to disconnect the data lines from the PC's USB port.
Thus, the normal 5V from USB is used for now. (The final version will run of 12VDC with a 5V regulator.)

I am not with my PC now, but as far as code goes even a simple LED flasher won;t work when I call DigiKeyboard. anything

Here is some more or less correct (pseudo) test code I can recall.

Code: [Select]
#include "digikeyboard.h"

void setup {
pinMode{5,OUTPUT);
}

void loop{
digitalWrite(5, HIGH);
DigiKeyboard.delay(500);
digitalWrite(5, LOW);
DigiKeyboard.delay(500);
}

As soon as a call to digikeyboard.anything is made, it will not start with databusses disconnected. There is 5V available. Sometimes the LED will flash, then stop then change flash speed.

My guess is that once the DigiKeyboard library is called, it will only start its clock once it detects the USB reset clock. (I am unable to confirm this.) What I do know, is that I can plug it with datalines on into a standalone USB powered hub, and it will still run. Once running, I can pull the links out disconnecting the datalines, and it will continue operation. Its only from cold start without the USB datalines present that it will refuse to run.

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: USB output, but standalone operation
« Reply #3 on: March 27, 2015, 01:53:04 pm »
From the wiki page about DigiKeyboard: "It is essential that DigiKeyboard.update() is called at the beginning of the loop to maintain USB communication"

So, try adding DigiKeyboard.update() to your loop?

Bergie

  • Newbie
  • *
  • Posts: 8
Re: USB output, but standalone operation
« Reply #4 on: March 28, 2015, 03:59:45 am »
Apologies Ralph for my code not compiling. I was sort of expecting it, since I just recalled from memory. I was not using my home PC, so I could not remember the exact code.
Some more experimenting proved this:
Code below:

Code: [Select]
#include "DigiKeyboard.h"

void setup() {
    pinMode(0,OUTPUT);
}

void loop() {
     DigiKeyboard.delay(500);
     digitalWrite(0,1);
     DigiKeyboard.delay(500);
     digitalWrite (0,0);
}
When I keep the datalines connected with the links, the code will run with only power connected. So it seems that for some reason, the zener diode portion of the hardware should stay connected to the pins. Probably the 1k pull-up resistor on the pin is required. (The links disconnected the zeners and resistors used for to drop the voltage.

Then I loaded my final sketch, kept the links in, but still not running the code. Because of extensive usage of other functions, I will just show some test code - no need to discuss my method of pushing bit level data to EEPROM.  :)
Below the code I am trying out.

Code: [Select]
#include "DigiKeyboard.h"

#define LED_PIN 0


void setup() {
pinMode(LED_PIN, OUTPUT);
//DigiKeyboard.update();
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.delay(5000);
DigiKeyboard.print("abcd");
}

void loop() {
       // DigiKeyboard.update();
digitalWrite(LED_PIN,HIGH);
DigiKeyboard.delay(250);
digitalWrite(LED_PIN,LOW);
DigiKeyboard.delay(250);
}
With the same setup links in or out, it won't run. I've added the "Digikeyboard.update();" line as well, but it still won't run.
Editing out these lines
Quote
DigiKeyboard.sendKeyStroke(0);
   DigiKeyboard.delay(5000);
   DigiKeyboard.print("abcd");
}
and leaving in
Quote
DigiKeyboard.update();
works.

I will add and remove lines one by one and post the results. But now my PC need a reboot, it does not see the Sparky plugged in anymore. It happens from time to time!  >:(

Bergie

  • Newbie
  • *
  • Posts: 8
Re: USB output, but standalone operation
« Reply #5 on: March 28, 2015, 04:28:28 am »
More testing was done.
Code below show only the setup{} section as the loop{} stay unchanged.

This work stand alone:

Code: [Select]
void setup() {
pinMode(LED_PIN, OUTPUT);
DigiKeyboard.update();
// DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.delay(5000);
// DigiKeyboard.print("abcd");

}

Below does not work stand alone:
Code: [Select]
void setup() {
pinMode(LED_PIN, OUTPUT);
DigiKeyboard.update();
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.delay(5000);
DigiKeyboard.print("abcd");

}

As soon as I include
Quote
DigiKeyboard.sendKestroke() or DigiKeyboard.print()
stand alone operation won't...
It does not matter if I include
Quote
DigiKeyboard.update();
or not. The offender is when I try and send out data on the bus when there is no actual USB bus around to send to.

Now what?

Bergie

  • Newbie
  • *
  • Posts: 8
Re: USB output, but standalone operation
« Reply #6 on: March 28, 2015, 04:56:01 am »
This code work if I pull pin 3 (with the USB bus) to ground.

Code: [Select]
void setup() {
pinMode(LED_PIN, OUTPUT);

if (digitalRead(3) == HIGH)  //The pull up resistor is connected to this USB line. Pull it low, code run stand alone.
{
DigiKeyboard.delay(5000);
DigiKeyboard.print("abcd");
}
DigiKeyboard.update();

}

This proof that writing to USB will cause code not to execute when there is no actual USB device connected. Putting the write out code as part of a condition work all the time. Maybe this problem can be addressed in the bootloader?
 

Bergie

  • Newbie
  • *
  • Posts: 8
Re: USB output, but standalone operation
« Reply #7 on: March 28, 2015, 05:43:53 am »
Thanks Ralph

Is there some function that I am unaware of that will check if USB bus is present or not before sending? Perhaps something in the line of
Quote
if(usb.available)

For now I will test with a simple check if the pin is high, but I am worried that the input may pulse and may miss the real event. I don't want to use extra pins as a check because I may need  it later.