Hello,
i have a strange problem with USB.
I have written a simple code to read and write data via USB on Arduino Console from/to Arduino Zero (powered by external battery).
When i use in the code the function USBDevice.detach() to disconnect Ardunino Zero from PC and subsequentely the function USBDevice.attach() to riconnect Arduino Zero to PC i can see the output on Arduino console of the SerialUSB.print command but i cannot send data via Arduino console to Arduino Zero.
SerialUSB don't receive characters sended via Arduino console.
If i reset the Arduino Zero and retry to send data via Arduino console i receive characters on Arduino Zero.
Attach or detach USB it's done thought an external push button connected to Arduino Zero.
There is some extra instruction for activation of USB input?
I also tried USBDevice.init() before USBDevice.attach() but without success.
Thank you in advance for reply.
#include <Arduino.h>
#define MULTI_FUNCTION_BUTTON_PIN A5
int buttonPushCounter = 0;
int buttonState;
int lastButtonState;
unsigned long buttonPushCounterLastRun = 0;
bool flipflop;
void setup()
{
// initialize SerialUSB
SerialUSB.begin(115200);
// initialize MULTI_FUNCTION_BUTTON
pinMode(MULTI_FUNCTION_BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
loopMultiFunctionButton();
readCommandSerialOut();
}
void loopMultiFunctionButton()
{
buttonState = digitalRead(MULTI_FUNCTION_BUTTON_PIN);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
SerialUSB.println("MULTI_FUNCTION_BUTTON pressed");
}
}
if (buttonPushCounter == 0)
buttonPushCounterLastRun = millis();
else if (millis() - buttonPushCounterLastRun > 5000)
{
switch (buttonPushCounter)
{
// 3 = enable/disable USB
case 3:
SerialUSB.println("MULTI_FUNCTION_BUTTON CASE 3");
if (flipflop)
{
flipflop = false;
//attach USB
USBDevice.init();
USBDevice.attach();
}
else
{
//detach USB
USBDevice.detach();
flipflop = true;
}
break;
}
buttonPushCounter = 0;
buttonPushCounterLastRun = 0;
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
#define SERIAL_OUT_INPUT_BUFFER_LENGTH 64
char inputBufferSerialOUT[SERIAL_OUT_INPUT_BUFFER_LENGTH];
int bufferPosSerialOUT = 0;
void readCommandSerialOut()
{
if (SerialUSB.available())
{
bool foundNewline = false;
uint8_t received = SerialUSB.read();
if (received != '\0')
{
if (received == '\n')
{
// If we've found a line ending, we should plan to run
// any registered hooks so they can check for unsolicited
// data that might be useful.
foundNewline = true;
SerialUSB.println(inputBufferSerialOUT);
// clear buffer
inputBufferSerialOUT[0] = '\0';
bufferPosSerialOUT = 0;
}
if (bufferPosSerialOUT + 1 == SERIAL_OUT_INPUT_BUFFER_LENGTH)
{
for (int32_t i = SERIAL_OUT_INPUT_BUFFER_LENGTH - 1; i > 0; i--)
{
inputBufferSerialOUT[i - 1] = inputBufferSerialOUT[i];
}
bufferPosSerialOUT--;
}
inputBufferSerialOUT[bufferPosSerialOUT++] = received;
inputBufferSerialOUT[bufferPosSerialOUT] = '\0';
}
}
}
