Output only works when seriël monitor is started and closed agian

Hi Guys,

I’m trying my first real project on an arduino micro (did only some breadboard experiments before).

The purpose of this project are 2 seperate thing. First, I need to Led lights to dim according to a 0-12V (0-5V true a voltage divider) input signal. second is switching the main AC power of a router connected to two relais. There is a switch, the main purpose is that a short press toggles the router on and off and a long press switches the led off when a input signal is present and on when there is no input.

This is my code for now

PWM dims a 2 leds according an 0 to 12V input

int switchIn = 3;
int ledOut1 = 6;
int ledOut2 = 11;
int routerSwitchOut = 9;
int statusLed = 12;

boolean switchLedState = false; //led switched
boolean routerState = false;
int ledOut = 0;
boolean switchActive = false; //previous run switch pressed
unsigned long switchStart = millis();
unsigned long statusLedTime = millis();

void setup()
  Serial.println("Setup running");
  pinMode(switchIn, INPUT);
  pinMode(ledOut1, OUTPUT);
  pinMode(ledOut2, OUTPUT);
  pinMode(routerSwitchOut, OUTPUT);
  pinMode(statusLed, OUTPUT);
  statusLedTime = millis();

void loop()
  unsigned long currentTime = millis();
  int val = analogRead(A4); //check input voltage
  //set switch state
  if(digitalRead(switchIn) == LOW){
    if(switchActive == true && currentTime - switchStart < 2000){
      if(routerState == false){
        routerState = true;
      } else {
        routerState = false;
    switchActive = false;
  } else {
    Serial.println("switch high");
    if(switchActive == false){
      switchStart = currentTime;
      switchActive = true;
    } else if(switchActive == true && currentTime - switchStart > 2000) {
      switchLedState = true;
  //switch led
  if(val == 0){
    if(switchLedState == true){
      ledOut = 255;
    } else {
      ledOut = 0;
  } else {
    if(switchLedState == false){
      ledOut = val / 4;
    } else {
      ledOut = 0;
  //status led blink
  if(currentTime - statusLedTime >= 1000){
    Serial.println("blink time expired");
    statusLedTime = currentTime;
    if(digitalRead(statusLed) == HIGH){
      digitalWrite(statusLed, LOW);
    } else {
      digitalWrite(statusLed, HIGH);
  //write data to outputs
  analogWrite(ledOut1, ledOut);
  analogWrite(ledOut2, ledOut);
  if(routerState == true){
    digitalWrite(routerSwitchOut, HIGH);
  } else {
    digitalWrite(routerSwitchOut, LOW);

The problem is that the relays are working fine as long as the arduino is connected to the PC. Once I disconect and everything is power over a 12V adapter, the relays are switching like at least 10 times per second and stop doing this as long as the button is pressed. I can’t find any circuit problems and the switching is done by the adruino if I may believe my scope.

Can someone please help me searching for a way to debug? Because it is only occuring while there is no USB connected, I am unable to use the serial monitor.

Thanks for the support


How is the input switch wired ?

I'm trying my first real project on an arduino micro (did only some breadboard experiments before).

Are you definitely using a Micro?

You seem to have an LED connected to pin 0 and on an Uno or Mega that would be part of the serial communication with the PC. I think pins 1 and 0 on the Micro are used by Serial1 but I can't confirm that from the pinmapping link on this page. If they are used by Serial1 then they can be used for an LED if you are not using Serial1