Hey guys, first of all sorry about my english, i'm brazilian... So,
I have a little doubt about the USB connection...
In my project, i want that arduino does something when it is only connected in USB, and when the arduino is connected to the 127V, i want it to do other thing...
How can i code this little part? I tried "if (Serial.available() > 0)" but that didn't worked. The code always "ran", even if the arduino is connected to the 127V and without the USB connection...
Another method would be to connect Vin to an analog pin (eg. A0) via a voltage divider, to guard against too high a voltage on the analog input. Then take a reading to see if the voltage is above or below 5V. In my case with USB power I measure 4.5V on Vin, and over 8V when connected via the power plug.
Don't connect Vin directly to A0 because the voltage will be too high - use a voltage divider.
I tried "if (Serial.available() > 0)" but that didn't worked.
All that tells you if serial data is available, not if the USB is plugged in or not.