0

I wrote my sketch and I can communicate with it with the Serial Monitor and it answers well. Unfortunately, once I try to make something with port with stty, screen, minicom and others, Arduino turns mad and doesn't work anymore. It prints garbage in the monitor or prints nothing. It is unable to re-upload the sketch.

Binary sketch size: 6,564 bytes (of a 32,256 byte maximum)
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00

Only unplugging and plugging back helps.

The sketch follows if it is important:

int relay_pins[] = {7, 6, 5, 4};
boolean relay_state[] = {true, true, true, true};

String command;
int i;

String awaitingMessage = "Relay module awaiting commands\r\nCommand \'help\' is for help";

void awaiting() {
  Serial.println(awaitingMessage);  
}

void help() {
  Serial.println("Relay module commands:");
  Serial.println("reset NUMBER\t- temporary turns OFF then ON relay number NUMBER (NUMBER = 0..3)");
  Serial.println("on NUMBER\t- turn ON relay number NUMBER (NUMBER = 0..3)");
  Serial.println("off NUMBER\t- turn OFF relay number NUMBER (NUMBER = 0..3)");
  Serial.println("flip NUMBER\t- flips relay number NUMBER (NUMBER = 0..3)");
  Serial.println("wait\t- print awaiting message");
  Serial.println("help\t- output this text");  
}

void set(int i, boolean on) {
  if( on ) {
    digitalWrite(relay_pins[i],HIGH); // This will Turn ON the relay 1  
  } else {
    digitalWrite(relay_pins[i],LOW); // This will Turn OFF the relay 1  
  }
  relay_state[i] = on;
}

boolean get(int i) {
  return relay_state[i];
}

void on(int i) {
  set(i, true);
}

void off(int i) {
  set(i, false);
}

void flip(int i) {
  set(i, !get(i));
}

void reset(int i) {
  off(i);
  delay(5000);
  on(i);
}

void unknown() {
  if(command.length() > 0) {
      Serial.println("Unknown command was \'" + command + "'");  
    }
}

void setup() {
  for(int i=0; i<4; ++i) {
    pinMode(relay_pins[i], OUTPUT); //Defining the pin 7 of the Arduino as output
    set(i, get(i));
  }
  Serial.setTimeout(50000);
  Serial.begin(9600, SERIAL_8N1);
  awaiting();
}

void loop() {
  command = Serial.readStringUntil('\n');
  if(command.startsWith("reset")) {
    command = command.substring(5);
    command.trim();
    i = command.toInt();
    reset(i);
  } else if(command.startsWith("on")) {
    command = command.substring(2);
    command.trim();
    i = command.toInt();
    on(i);
  } else if(command.startsWith("off")) {
    command = command.substring(3);
    command.trim();
    i = command.toInt();
    off(i);
  } else if(command.startsWith("flip")) {
    command = command.substring(4);
    command.trim();
    i = command.toInt();
    flip(i);
  } else if(command.startsWith("wait")) {
    awaiting();
  } else if(command.startsWith("help")) {
    help();
  } else {
    unknown();
    //awaiting();   
  }
}
  • Probably hupcl has been disabled. Try stty -F /dev/ttyACM0 hupcl – Majenko Jan 6 '18 at 21:03
  • Also at a more basic level make sure no program has the serial device open when you attempt a download. The Arduino IDE will get it's won serial monitor out of the way before invoking avrdude, but just about anything else will cause exactly the problem you report. – Chris Stratton Jan 6 '18 at 22:27

Your Answer

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

Browse other questions tagged or ask your own question.