0

Why is my machine suddenly turns ON when I unplugged the USB on my computer? Does it have connection on the auto reset feature of my arduino board?

enter image description here

String input = "";

void setup(){

  Serial.begin(9600);   // Sets up communication with the serial port

  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);   // for start of program operation


  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);

  digitalWrite(11,LOW);

}

void loop(){

  int recipe;
  //Serial.print("Enter recipe number: ");

  while(Serial.available()==0){    // Waits for data from serial port
  }

  while(Serial.available() > 0){
    int data = Serial.read();
      // convert the incoming byte to a char
      // and add it to the string
    input += (char)data;

    recipe = input.toInt()+'0';    //recipe=0 if not a digit
    if(recipe == '0')
      recipe = data;

    if(data == '\n'){
      //Serial.print("Recipe: ");
      //Serial.println(recipe);
      //Serial.print("String: ");
      //Serial.println(input);
      input = "";
    }
  }

  if(recipe == 's'){     
    digitalWrite(11,HIGH);
    delay(1000);
    digitalWrite(11,LOW);

    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
    digitalWrite(9,LOW);
    digitalWrite(10,LOW);    
  }

  else if(recipe>'0' && recipe<'32'){    

    recipe = recipe-'0';
    char binary[7] = {0};   // This is where the binary representation will be stored
    recipe += 32; // Adding 32 so that there will always be 6 digits in the string
    itoa(recipe,binary,2);  // Convert recipe to a string of base 2 and save it in array 'binary'
    char* signals = binary + 1; // Get rid of the most significant digit to get the 5 bits

  //Serial.print(signals);  // Print out the signals in binary

    int i;
    for(i=0; i<5; i++){
      if(signals[i] == '0')
        signals[i] = '1';
      else
        signals[i] = '0';
      digitalWrite(i+6, signals[i]-'0');    // write to pin; converts the bit of the string to HIGH or LOW
      //Serial.print(signals[i]);
    }

  }
  else
    loop();

}
  • Post your schematic or wiring, and point to the datasheet on your machine. Maybe when the arduino is depowered and the pins tristate, your machine is no longer inhibited from being ON. But that is only a guess based on the information in your question. – Dave X Mar 10 '16 at 3:32
  • hi Dave X, where can I send/post my schematic diagram? do you have an email or something so i can show both the wiring and arduino code. thanks in advance – Queen Quiazon Mar 10 '16 at 3:46
  • Please add it to your question. The editor has a little mountain-landscape icon that lets you upload graphics. – Dave X Mar 10 '16 at 3:53
  • Hi Dave x, here you go. I have added the sketch as well for you to correlate. this is to automatic select recipe once press "send recipe" and automatic start the tool once press "start" button – Queen Quiazon Mar 10 '16 at 4:09
  • Is your Arduino powered only through the USB or is there alternate power? – Dave X Mar 10 '16 at 5:11
1

Do not call loop from loop. You will quickly run out of RAM and the sketch will crash:

void loop(){

  int recipe;
  //Serial.print("Enter recipe number: ");

  while(Serial.available()==0){    // Waits for data from serial port
  }

  while(Serial.available() > 0){
...

  }
  else
    loop();

}

For the pins that are supposed to be high, I suggest pull-up resistors:

  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);   // for start of program operation


  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);

When the Arduino powers up it enters the bootloader for a second or so. Those pins won't be set high yet. A pull-up resistor would make sure they are high until such time as you want to drive them low.


If you are saying that the machine powers up when you disconnect the power to the Arduino, that is still a good reason for using pull-up resistors, to make sure that it does not operated until commanded to do so.


where will i connect the pull up resistors and how?

Typically pull-up resistors will be around 10k, and are connected from the pin to +5V. Of course, if the Arduino is not powered, then you need another source of the +5V.


please to clarify the "do not call from loop to loop"

Remove these two lines:

else
    loop();

When loop exits, is it immediately re-called. You don't need to do it that way, in fact it is a Bad Idea.

|improve this answer|||||
  • Hi Nick, where will i connect the pull up resistors and how? and also please to clarify the "do not call from loop to loop". What line should i need to remove? thanks! – Queen Quiazon Mar 10 '16 at 4:47
  • See amended answer. – Nick Gammon Mar 10 '16 at 4:52
0

It looks like the this line in your setup:

  digitalWrite(11,LOW);

... would trigger contact input 211, and do "Start of Program Operation when D11 switches from off to on" every time the sketch starts.

If the arduino resets, it will pull this line low. Autoreset would initiate this.

By changing the line to the following, the code will wait until it gets an 's' before pulling the line low.

  digitalWrite(11,HIGH);

I don't have an Arduino, but perhaps http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection may help in managing the Auto-Reset issue.

|improve this answer|||||
  • Hi Dave X, thanks, so what i need to do is to remove the digitalWrite (11,LOW) line on the code? – Queen Quiazon Mar 10 '16 at 4:45
  • It defaults to LOW, so removing it won't do much. Set it to HIGH if that is what is required here. – Nick Gammon Mar 10 '16 at 4:53
  • Yes, what Nick said. – Dave X Mar 10 '16 at 4:56
  • Sorry fo the confusion. Pin 11 is set to HIGH initially that is why when I unplugged the controller , the machine will suddenly turns ON. If i set it to low, machine will turn ON when i unplug then plugged my ports to PC. – Queen Quiazon Mar 10 '16 at 5:04
  • Your code seems to set Pin 11 to LOW initially. – Dave X Mar 10 '16 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.