Error: "can't set com-state"

Hello Everyone,

I'm relatively new to programming, but I've always had an interest in robotics. I've just begun playing around with a vilros arduino ultimate starter kit.

In example circuit 10 you spin a motor by inputting the speed you want in the serial monitor.

The code in the example is supposed to spin the motor until I change the speed.

I have two problems (which may be the same problem) I am trying to address. 1) Against my understanding of the function (posted below), the speed is reset back to zero after a few seconds of running at the speed I input. 2) If I make either a small modification, or no modification, and re-upload the sketch to the arduino I will begin to get the following error message:

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

I have checked to make sure that the arduino is being spoken to via serial com1.

Here is the function of the sketch that I am utilizing:

void serialSpeed()
{
  int speed;
  
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();  // Print a blank line

  // In order to type out the above message only once,
  // we'll run the rest of this function in an infinite loop:

  while(true)  // "true" is always true, so this will loop forever.
  {
    // First we check to see if incoming data is available:
  
    while (Serial.available() > 0)
    {
      // If it is, we'll use parseInt() to pull out any numbers:
      
      speed = Serial.parseInt();
      
      // Because analogWrite() only works with numbers from
      // 0 to 255, we'll be sure the input is in that range:
  
      speed = constrain(speed, 0, 255);
      
      // We'll print out a message to let you know that the
      // number was received:
      
      Serial.print("Setting speed to ");
      Serial.println(speed);
  
      // And finally, we'll set the speed of the motor!
      
      analogWrite(motorPin, speed);
    }
  }
}

Any help would be appreciated.

Thanks!

How are you powering the motor?

The motor is supplied by the 5V power pin on the arduino. It can draw more power than the digital output pins can drive, so I am using a digital outpin pin to toggle a transistor in series with the motor.

The motor is supplied by the 5V power pin on the arduino

Power your motor with its own 5 volt power supply. Make sure you connect the external power supply ground to the Arduino ground.

.

Why does it need it's own power supply?

It's a very small motor, draws 100mA at 5V. I am using a transistor because the digital outputs can only output up to 40mA. The transistor can handle up to 200mA.

What does this have to do with the inability to upload the sketch and the ser_open() error message?

Motors can cause voltage spikes on a power supply. These spikes can play havoc on digital circuits.

Also at turnon time, the motor draws more current than it does when it is running.

I am now receive the com_state error when I try to upload ANY sketch that uses Serial.begin().

What could cause this?

For example, if I just go into the examples that come with arduino, in basics, the analogReadSerial example and try to upload that sketch I get the same error:

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

Again, I have verified that COM1 is the arduino.

What could cause this?

I have uninstalled and reinstalled the drivers. Problem solved.