I'm trying to write a piece of code which will allow me to enter a number from 0-5 on my computer keyboard which will then through the use of PWM select drive my motor at given speed.
//Program by Jeremy Blum
//Test Motor Speed Using a Transistor
int motorPin = 6;
int motorSpeed = 0;
//Set the PWM Motor pin as an output
byte incomingbyte = Serial.read(); // this is value entered in serial monitor
motorSpeed = (incomingbyte -48)*51; // subtracting 48 to get decimal value. Multiply by 51 as 255/51 = 5
analogWrite(motorPin, motorSpeed); // write to the motor pin the motorSpeed
This is for a university project so unfortunately I only have an arduino simulator here at home to test with but I'm having the following problems:
This doesn't seem to drive the motor at the desired speed
It doesn't loop
Also I want to hold the speed selected until a new value is entered how would this be achieved?
Hope this is clear, thank you for your help