Pages: [1] 2   Go Down
Author Topic: USB gets turned off?  (Read 1205 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey new to coding but i am trying to have it setup so that when i push a 1 button out of 2 it records it for 10 button hits after that i hit a third button  it play back the hits with LEDs each button has a differnt color(red, green) then when i hit third button again it resets.

my problem is that when i hit any of the 3 buttons my ardunio shuts off until i hit the button again.

could anything in my code be causing this or is it a hardware thing?

Code:
//button 1 is in pin 2
//button 2 is in pin 4
//button 3 is in pin 7
// pin 13 is green
// pin 12 is red
int start = 0;
int total = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
int ten =0;

void setup() {
  pinMode(13,OUTPUT);
  pinMode(2,INPUT);
  pinMode(4,INPUT);
  pinMode(7,INPUT);
  pinMode(12,OUTPUT);
}

void loop() {
  Serial.println(total);
  Serial.println(digitalRead(4));

  if (total == 11 && digitalRead(4) == 1){
    start = 2;
  }
  if (digitalRead(4) == 1 && total == 0) {
    start = 1;  // 1 = start recording
    delay(2000);
  }
  if (start == 1) {
    total = 1;
    Serial.println(total);
  }
  if (total == 1) {
    if (digitalRead(2) == 1) { // red = 1
      one = 1;
      total = 2;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      one = 2;
      total = 2;
      Serial.println(total);
    }
    else {
      one = 0;
      total = 1;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 2) {
    if (digitalRead(2) == 1) { // red = 1
      two = 1;
      total = 2;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      two = 2;
      total = 2;
      Serial.println(total);
    }
    else {
      two == 0;
      total = 3;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 3) {
    if (digitalRead(2) == 1) { // red = 1
      three = 1;
      total = 4;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      three = 2;
      total = 4;
      Serial.println(total);
    }
    else {
      three == 0;
      total = 3;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 4) {
    if (digitalRead(2) == 1) { // red = 1
      four = 1;
      total = 5;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      four = 2;
      total = 5;
      Serial.println(total);
    }
    else {
      four == 0;
      total = 4;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 5) {
    if (digitalRead(2) == 1) { // red = 1
      five = 1;
      total = 6;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      five = 2;
      total = 6;
      Serial.println(total);
    }
    else {
      five == 0;
      total = 5;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 6) {
    if (digitalRead(2) == 1) { // red = 1
      six = 1;
      total = 7;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      six = 2;
      total = 7;
      Serial.println(total);
    }
    else {
      six == 0;
      total = 6;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 7) {
    if (digitalRead(2) == 1) { // red = 1
      seven = 1;
      total = 8;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      seven = 2;
      total = 8;
      Serial.println(total);
    }
    else {
      seven == 0;
      total = 7;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 8) {
    if (digitalRead(2) == 1) { // red = 1
      eight = 1;
      total = 9;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      eight = 2;
      total = 9;
      Serial.println(total);
    }
    else {
      eight == 0;
      total = 8;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 9) {
    if (digitalRead(2) == 1) { // red = 1
      nine = 1;
      total = 10;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      nine = 2;
      total = 10;
      Serial.println(total);
    }
    else {
      nine == 0;
      total = 9;
      Serial.println(total);
    }
    delay(2000);
  }
  if (total == 10) {
    if (digitalRead(2) == 1) { // red = 1
      ten = 1;
      total = 11;
      Serial.println(total);
    }
    else if (digitalRead(7) == 1) { // green = 2
      ten = 2;
      total = 11;
      Serial.println(total);
    }
    else {
      ten == 0;
      total = 10;
      Serial.println(total);
    }
    delay(2000);
  }
  if ( start == 2)
    if (one == 1) {
      digitalWrite(12,HIGH);
      delay(500);
      digitalWrite(12,LOW);
      delay(500);
    }
  if (one == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }

  if (two == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (two == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (three == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (three == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (four == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (four == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (five == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (five == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (six == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (six == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (seven == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (seven == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (eight == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (eight == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (nine == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (nine == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (ten == 1) {
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12,LOW);
    delay(500);
  }
  if (ten == 2) {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
  if (start == 2 && digitalRead(4) == 1) {
    total = 0;
  }
}

Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12919
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


At this point, you need to assume the problem is hardware and you need to resolve the problem before you damage your Arduino.

Do you have current-limiting resistors in series with the LEDs?

How do you have the buttons wired?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have an ardunio uno
so i have pin 12, 13 going through a 220 ohms resistors then LED then to ground

then i have the 5v pin hooked up to the 3 buttons,  pretty much setup like this (http://arduino.cc/en/Tutorial/DigitalReadSerial) but with 3 buttons and the 5v going to all 3 (might be my problem?) then i ground them all to the same ground.  and the inputs from the buttons are going to pin 2, 4, 7

i could take a pic and upload it if it makes it more clear

if i'm doing something wrong let me know i'm fairly knew to electronics and coding.

edit: oh yea i have 10k ohms resistors in between buttons and ground
« Last Edit: October 03, 2012, 01:18:42 am by norsk » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12919
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...my ardunio shuts off...

What do you mean by "shuts off"?  The green LED labeled ON near the reset button goes out?
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe it might be of some use to start your serial in the setup function, as I don't see it anywhere?
For example
Code:
Serial.begin(9600);

rockwallaby . .
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You appear to have a problem remembering things.
Code:
  Serial.println(digitalRead(4));

  if (total == 11 && digitalRead(4) == 1){
    start = 2;
  }
  if (digitalRead(4) == 1 && total == 0) {
    start = 1;  // 1 = start recording
Reading the various anonymous pins ONCE per pass through loop should be sufficient. It hardly seems likely that the state of the pin will have changed much in the time it takes to execute this code. But, what if that happened, while the switch was bouncing, maybe. What would that do to your code?

How are your switches wired?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...my ardunio shuts off...

What do you mean by "shuts off"?  The green LED labeled ON near the reset button goes out?


yea the light goes out when ever i hit the button and my computer cant recognize the USB anymore until i hit button again. it is weird.


You appear to have a problem remembering things.
Code:
  Serial.println(digitalRead(4));

  if (total == 11 && digitalRead(4) == 1){
    start = 2;
  }
  if (digitalRead(4) == 1 && total == 0) {
    start = 1;  // 1 = start recording
Reading the various anonymous pins ONCE per pass through loop should be sufficient. It hardly seems likely that the state of the pin will have changed much in the time it takes to execute this code. But, what if that happened, while the switch was bouncing, maybe. What would that do to your code?

How are your switches wired?


you mean digitalRead(4)? but i want to make sure the the button is pushed and and its currently not reading any of the other 2 buttons. maybe i'm confused by what you mean. ( new to coding)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you mean digitalRead(4)?
Yes. There should be one call to digitalRead(4). Not three. That way, all the concerns about the state of pin 4 are concerned about the state of pin 4 AT THE SAME TIME, not at 3 different times.

At the risk of repeating myself:
Quote
How are your switches wired?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12919
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yea the light goes out when ever i hit the button and my computer cant recognize the USB anymore until i hit button again. it is weird.

You have the buttons wired wrong.  You have to fix that problem before continuing.  You are damaging your Arduino and possibly your computer.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So like make an if statement with digitalRead being first test
Then have if for each "total" ?
Like this

If ( digitalRead(4) == 1) {
If (total == 0) {
   Start = 1;
}
If (total == 11) {
Etc
}
What about the other buttons?    
I think this project was to hard for me for where I'm at in my learning  of coding
Also I thought I explained my wiring already? Is there a better way of explaining it?

Thanks for helping me out
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also I thought I explained my wiring already?
You said:
Quote
the 5v going to all 3 (might be my problem?) then i ground them all to the same ground.  and the inputs from the buttons are going to pin 2, 4, 7

Quote
Is there a better way of explaining it?
Yes. A switch has two legs. A wire has two ends. Describe how many wires there are (for one switch; we'll assume that they are all wired the same, unless you say otherwise). Describe where each end of each wires goes.

You don't mention any pullup or pulldown resistors. Without them, or the internal pullup resistors, the switches will float, and you will have all kinds of grief.

The simplest way to wire a switch is to connect one leg to the digital pin and the other leg to ground. Then, turn on the pullup resistor for that pin.
Code:
pinMode(somePin, INPUT);
digitalWrite(somePin, HIGH);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so my buttons are setup almost  exactly like image in link
(http://arduino.cc/en/Tutorial/DigitalReadSerial)
 all 3 buttons are connected to the same 5v source, and are all connected to the same ground.
Then each button has a different input pin being used.

They are 4 pin not 2 pin

The resistor in the link is a pull-up resistor? Or pull down?

Hope that helps

Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2892
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 "all 3 buttons are connected to the same 5v source, and are all connected to the same ground.
Then each button has a different input pin being used." (Quoted from OP's post...)
This would explain why, Quoted from the thread title "Re: USB gets turned off?"...
When you connect a switch from power to ground and activate it (a N.O. Switch... obviously)
You've connected power to ground and after having done that, discovered there's no power left
to do anything else.
In the real world you would 1. connect a resistor (~1K to ~47K) from the input pin to EITHER power
OR ground and connect the switch from the input pin to the OPPOSITE of where you connected the
resistor to. In this manner of connection If the Switch goes to ground, testing the input pin will show
a high state EXCEPT when the switch is activated and a low state EXCEPT when the switch is activated.
Or 2. If the resistor is connected to ground and the input pin... and the switch is between power and the input pin.
will show a low state Except when the switch is activated and a high state when the switch is activated.
If the resistor is connected to ground and the input  ... and the switch is between power and the input pin

Bob
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
They are 4 pin not 2 pin
4 physical legs does not equate to 4 pins. Two legs get connected to two other legs when the switch is pressed.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2302
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What type of switches are you using? It sounds like you're using on/off switches (like light switches).


Mark
Logged

Pages: [1] 2   Go Up
Jump to: