Avrdude: ser_open(): can't set com-state for "\\.\COM4"

hi there, ive try to call void in another new tab in arduinoIDE.but the problem like this happen.i dont know why if i make new inilitatiton like void interupt(){ } in new tab and call it in void loop(){ },the problem like this happen.i also search on google how to solve this ,ive already unplug usb cable,restart my arduino uno but problem still happen.

this is my program tab.


i call button tab in void loop(){ } with void interupt();

this is my code in new_menu tab.


#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

Adafruit_SSD1306 lcd(128, 64, &Wire, -1);

#define btnUp   7
#define btnOk   6
#define btnDown 5
#define btnBack 4

boolean statusBtnUp   = false;
boolean statusBtnOk   = false;
boolean statusBtnDown = false;
boolean statusBtnBack = false;

boolean statusAkhirBtnUp   = false;
boolean statusAkhirBtnOk   = false;
boolean statusAkhirBtnDown = false;
boolean statusAkhirBtnBack = false;

boolean UP   = false;
boolean OK   = false;
boolean DOWN = false;
boolean BACK = false;

int halaman  = 1;
int menuItem = 1;

void setup() {
  Serial.begin(115200);
  lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  lcd.clearDisplay();

  pinMode(btnUp,   INPUT_PULLUP);
  pinMode(btnOk,   INPUT_PULLUP);
  pinMode(btnDown, INPUT_PULLUP);
  pinMode(btnBack, INPUT_PULLUP);
}

void loop() {
  tampil();
  interupt();
  
}

//--------------------------------------------------------------------------------
void saatUpDitekan() {
  if (statusBtnUp != statusAkhirBtnUp) {
    if (statusBtnUp == 0) {
      UP = true;
    }
    delay(50);
  }
  statusAkhirBtnUp = statusBtnUp;
}

void saatOkDitekan() {
  if (statusBtnOk != statusAkhirBtnOk) {
    if (statusBtnOk == 0) {
      OK = true;
    }
    delay(50);
  }
  statusAkhirBtnOk = statusBtnOk;
}

void saatDownDitekan() {
  if (statusBtnDown != statusAkhirBtnDown) {
    if (statusBtnDown == 0) {
      DOWN = true;
    }
    delay(50);
  }
  statusAkhirBtnDown = statusBtnDown;
}

void saatBackDitekan() {
  if (statusBtnBack != statusAkhirBtnBack) {
    if (statusBtnBack == 0) {
      BACK = true;
    }
    delay(50);
  }
  statusAkhirBtnBack = statusBtnBack;
}
//----------------------------------------------------------------------------

//semua yang tampil di lcd ada di fungsi ini
void tampil() {
  if (halaman == 1) {
    lcd.clearDisplay();
    lcd.setTextSize(1);
    lcd.setTextColor(WHITE);
    lcd.setCursor(30, 0);
    lcd.print("DAFTAR MENU");

    if (menuItem == 1) {
      lcd.setCursor(5, 17);
      lcd.setTextColor(WHITE);
      lcd.print("> KM/h");
    } else {
      lcd.setCursor(5, 17);
      lcd.setTextColor(WHITE);
      lcd.print("  kecepatan sepeda");
    }

    if (menuItem == 2) {
      lcd.setCursor(5, 27);
      lcd.setTextColor(WHITE);
      lcd.print("> Volt");
    } else {
      lcd.setCursor(5, 27);
      lcd.setTextColor(WHITE);
      lcd.print("  Voltase baterai");
    }

    if (menuItem == 3) {
      lcd.setCursor(5, 37);
      lcd.setTextColor(WHITE);
      lcd.print("> Ampere");
    } else {
      lcd.setCursor(5, 37);
      lcd.setTextColor(WHITE);
      lcd.print("  Arus baterai");
    }

    if (menuItem == 4) {
      lcd.setCursor(5, 47);
      lcd.setTextColor(WHITE);
      lcd.print("> Newton");
    } else {
      lcd.setCursor(5, 47);
      lcd.setTextColor(WHITE);
      lcd.print("  Nilai Torsi");
    }

  } else if (halaman == 2) {
    lcd.clearDisplay();
    lcd.setTextSize(2);
    lcd.setTextColor(WHITE);
    lcd.setCursor(25, 25);
    lcd.print("ImamID");
  } else if (halaman == 3) {
    lcd.clearDisplay();
    lcd.setTextSize(2);
    lcd.setTextColor(WHITE);
    lcd.setCursor(10, 15);
    lcd.print("VOLTASE");
    lcd.setCursor(30, 35);
    lcd.print("VOLT");
  } else if (halaman == 4) {
    lcd.clearDisplay();
    lcd.setTextSize(2);
    lcd.setTextColor(WHITE);
    lcd.setCursor(25, 25);
    lcd.print("Arus bat");
  } else if (halaman == 5) {
    
    lcd.clearDisplay();
    lcd.setTextSize(1);
    lcd.setTextColor(WHITE);
    lcd.setCursor(25, 25);
    lcd.print("nilai torsi");
  }

  lcd.display();
}

and this is my code in button tab menu.

void  interupt(){
  statusBtnUp   = digitalRead(btnUp);
  statusBtnOk   = digitalRead(btnOk);
  statusBtnDown = digitalRead(btnDown);
  statusBtnBack = digitalRead(btnBack);

  saatUpDitekan();
  saatOkDitekan();
  saatDownDitekan();
  saatBackDitekan();

  //  untuk button up
  if (UP && halaman == 1) {
    UP = false;
    menuItem --;
    if (menuItem < 1)menuItem = 4;
  }
  //untuk button down
  if (DOWN && halaman == 1) {
    DOWN = false;
    menuItem ++;
    if (menuItem > 4)menuItem = 1;
  }

  //  untuk button ok
  if (OK) {
    OK = false;
    if (halaman == 1 && menuItem == 1) {
      halaman = 2;
    } else if (halaman == 1 && menuItem == 2) {
      halaman = 3;
    } else if (halaman == 1 && menuItem == 3) {
      halaman = 4;
    } else if (halaman == 1 && menuItem == 4) {
      halaman = 5;
    }
  }

  //  untuk button back
  if (BACK) {
    BACK = false;
    if (halaman == 2 || halaman == 3 || halaman == 4 || halaman == 5) {
      halaman = 1;
    }
  }

  Serial.println(menuItem);
  delay(100);
}

and this is my error.

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring_pulse.S.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\WInterrupts.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\hooks.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring_analog.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring_digital.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring_pulse.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\wiring_shift.c.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\CDC.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\HardwareSerial.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\HardwareSerial0.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\HardwareSerial1.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\HardwareSerial2.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\HardwareSerial3.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\IPAddress.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\PluggableUSB.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\Print.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\Stream.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\Tone.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\USBCore.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\WMath.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\WString.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\abi.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\main.cpp.o"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc-ar" rcs "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\core.a" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\core\new.cpp.o"

Archiving built core (caching) in: C:\Users\ridho\AppData\Local\Temp\arduino_cache_455474\core\core_arduino_avr_uno_94baf922c9d38d8316eaa2affe0e6dcb.a

Linking everything together...

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.elf" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\sketch\new_menu.ino.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Wire\Wire.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Wire\utility\twi.c.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Adafruit_SSD1306\Adafruit_SSD1306.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\arduino_696289\glcdfont.c.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\arduino_696289\Adafruit_GFX.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\arduino_696289\Adafruit_GrayOLED.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\arduino_696289\Adafruit_SPITFT.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\SPI\SPI.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Adafruit_BusIO\Adafruit_BusIO_Register.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549\libraries\Adafruit_BusIO\Adafruit_SPIDevice.cpp.o" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/core\core.a" "-LC:\Users\ridho\AppData\Local\Temp\arduino_build_848549" -lm

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.elf" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.eep"

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-objcopy" -O ihex -R .eeprom "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.elf" "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.hex"

Multiple libraries were found for "Adafruit_SSD1306.h"

Used: C:\Users\ridho\Documents\Arduino\libraries\Adafruit_SSD1306

Not used: C:\Users\ridho\Documents\Arduino\libraries\Adafruit_SSD1306_Wemos_Mini_OLED

Not used: C:\Users\ridho\Documents\Arduino\libraries\Adafruit_SSD1306-master

Using library Wire at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire

Using library Adafruit_SSD1306 at version 2.4.4 in folder: C:\Users\ridho\Documents\Arduino\libraries\Adafruit_SSD1306

Using library arduino_696289 at version 1.10.10 in folder: C:\Users\ridho\Documents\Arduino\libraries\arduino_696289

Using library SPI at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SPI

Using library Adafruit_BusIO at version 1.7.2 in folder: C:\Users\ridho\Documents\Arduino\libraries\Adafruit_BusIO

"C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-size" -A "C:\Users\ridho\AppData\Local\Temp\arduino_build_848549/new_menu.ino.elf"

Sketch uses 15622 bytes (48%) of program storage space. Maximum is 32256 bytes.

Global variables use 689 bytes (33%) of dynamic memory, leaving 1359 bytes for local variables. Maximum is 2048 bytes.

An error occurred while uploading the sketch

avrdude: ser_open(): can't set com-state for "\.\COM4"

azhar

Is COM4 indeed the Arduino? What does Windows device manager say?

Note: this has nothing to do with your code :wink:

have done yet...thanks sir....have a nice day!!