DC motor control optimization

Hello, I am new to Arduino and training myself to use its many functions step by step. My project is in the infant stages of an autonomous boat. Right now I am trying to control a motor with keyboard commands. I have successfully done this using serial and processing to move the motor at different speeds and one speed in reverse. Right now my forward speeds 0, 63.75, 85, 127.5, 170, 212.5, 255 and reverse speed 255. My problem comes in to play when I turn the motor to 42.5 the motor just makes a weird sound and doesn't spin and 85 the motor spins with the same noise and again spins at 170 with the noise.

Here is my Arduino code:

char val;         // Data received from the serial port
int motorpin = 4; // Wiring: Connect L293D Pin En1 connected to pin PWM 4

void setup()
{
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop()
{
  if (Serial.available())         // If data is available,
  {
    val = Serial.read();          // read it and store it in val
    if (val == '2')               // If '2' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 63.75);   // turn the motor on at 42.5 speed
    }
    if (val == '3')            // If '3' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 85);      // turn the motor on at 85 speed
    }
    if (val == '4')            // If '4' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      digitalWrite(3, 127.5);  // turn the motor on at 127.5 speed
    }
    if (val == '5')            // If '5' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 170);     // turn the motor on at 170 speed
    }
    if (val == '6')            // If '6' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 212);     // turn the motor on at 212 speed
    }
    if (val == '7')           // If '7' was received,
    {
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A
      analogWrite(3, 255);    // turn the motor on at 255 speed
    }
    else if (val == '1')      // If '1' was received
    {
      analogWrite(3, 0);      // turn the motor off
    }
    if (val == '8')           // If '8' was received,
    {
      digitalWrite(12, LOW);  //Establishes backward direction of Channel A
      digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      digitalWrite(3, 255);   // turn the motor on at 255 speed
  }
  delay(100); // Wait 100 milliseconds for next reading
}
}

Here is my Processing code:

import processing.serial.*;
import controlP5.*;

ControlP5 cp5; 

Serial port;
int KnobValue = 100;
boolean knobOn = false;   // Status of the button
boolean firstTime = true;

Knob myKnobA;
Knob myKnobB;

void setup() {
  size(480, 480);
  smooth();
  noStroke();
 
 cp5 = new ControlP5(this);
 
   myKnobA = cp5.addKnob("motorPower")
               .setRange(0,255)
               .setValue(220)
               .setPosition(135,20)
               .setRadius(90)
               .setNumberOfTickMarks(6)
               .setTickMarkLength(7)
               .snapToTickMarks(true)
               .setColorForeground(color(255))
               .setColorBackground(color(0, 160, 100))
               .setColorActive(color(255,255,0))
               .setDragDirection(Knob.HORIZONTAL)
               .setValue(0)
               ;
               
    myKnobB = cp5.addKnob("knobDirection")
               .setRange(0,255)
               .setValue(220)
               .setPosition(135,260)
               .setRadius(90)
               .setNumberOfTickMarks(6)
               .setTickMarkLength(7)
               .snapToTickMarks(true)
               .setColorForeground(color(255))
               .setColorBackground(color(0, 160, 100))
               .setColorActive(color(255,255,0))
               .setDragDirection(Knob.VERTICAL)
               ;

    println(Serial.list());
    
    port = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  ellipse(225, 110, 200, 200);
  fill(0,100);
  ellipse(225, 350, 200, 200);
  fill(4,100);  
}

void keyPressed() {
  switch(key) {
    case('1'):myKnobA.setValue(0);break; 
    case('2'):myKnobA.setValue(42.5);break;
    case('3'):myKnobA.setValue(85);break;
    case('4'):myKnobA.setValue(127.5);break;
    case('5'):myKnobA.setValue(170);break;
    case('6'):myKnobA.setValue(212.5);break;
    case('7'):myKnobA.setValue(255);break;
  }
  switch(key) {
  }
    if (keyPressed) {
    if (key == '1') {
      port.write('1');
    }
    }
    if (keyPressed) {
    if (key == '2') {
      port.write('2');
    }
    }
    if (keyPressed) {
    if (key == '3') {
      port.write('3');
    }
    }
    if (keyPressed) {
    if (key == '4') {
      port.write('4');
    }
    }
    if (keyPressed) {
    if (key == '5') {
      port.write('5');
    }
    }
    if (keyPressed) {
    if (key == '6') {
      port.write('6');
    }
    }
    if (keyPressed) {
    if (key == '7') {
      port.write('7');
    }
    }
    if (keyPressed) {
    if (key == '8') {
      port.write('8');
    }
    }
}

Any suggestions?
Constructive criticism always welcomed this is my first post.

What does this do?
digitalWrite(3, 127.5); // turn the motor on at 127.5 speed
And
digitalWrite(3, 255); // turn the motor on at 255 speed

To save time and $$$, you might want to speed hack a large servo like below to drive the boat propeller (forward/reverse with speed control).

LarryD

I was experimenting with some ideas and didn't change it back here is what the code should be.

char val;         // Data received from the serial port
int motorpin = 4; // Wiring: Connect L293D Pin En1 connected to pin PWM 4

void setup()
{
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop()
{
  if (Serial.available())         // If data is available,
  {
    val = Serial.read();          // read it and store it in val
    if (val == '2')               // If '2' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 63.75);   // turn the motor on at 42.5 speed
    }
    if (val == '3')            // If '3' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 85);      // turn the motor on at 85 speed
    }
    if (val == '4')            // If '4' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 127.5);  // turn the motor on at 127.5 speed
    }
    if (val == '5')            // If '5' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 170);     // turn the motor on at 170 speed
    }
    if (val == '6')            // If '6' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 212);     // turn the motor on at 212 speed
    }
    if (val == '7')           // If '7' was received,
    {
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A
      analogWrite(3, 255);    // turn the motor on at 255 speed
    }
    else if (val == '1')      // If '1' was received
    {
      analogWrite(3, 0);      // turn the motor off
    }
    if (val == '8')           // If '8' was received,
    {
      digitalWrite(12, LOW);  //Establishes backward direction of Channel A
      digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      analogWrite(3, 255);   // turn the motor on at 255 speed
  }
  delay(100); // Wait 100 milliseconds for next reading
}
}

Zoomkat,
Thanks you, I will check it out. But I don’t think it will work for me because the servo will be incased and I will need to cool it down. Once I get the motor control and servo control down, I intend to use a motor that is water cooled.

analogWrite() supports 0-255 INTEGER (not floating)

So analogWrite(3, 63.75); //will not work
analogWrite(3, 63); //will work