I need to use the PWM pins (5,6) here and map values from 0-255 to 0-99 so when i enter 50 on one of the pins the motor runs with 50% speed

const int motorPin1 = 6;
const int motorPin2 = 5;

char input;

void setup()  {
  Serial.begin(9600);
  
  pinMode(motorPin1,OUTPUT);
  pinMode(motorPin2,OUTPUT);
}

void loop() {
 
  if(Serial.available() > 0) {
    input = Serial.read();
    
    if (input == '+') {
      Serial.println("Clockwise");
      digitalWrite(motorPin1,HIGH);
      digitalWrite(motorPin2,LOW);
    }
    else if (input == '-') {
      Serial.println("CounterClockwise");
      digitalWrite(motorPin1,LOW);
      digitalWrite(motorPin2,HIGH);
    }
    else if (input == 'S') {
      Serial.println("Stop");
      digitalWrite(motorPin1,LOW);
      digitalWrite(motorPin2,LOW);
    }
  }
}

Where are you stuck ?

Is it with entering and interpreting the number or is it in using the map() function ?

1 Like

well basically my prof asked me to edit the code to make the range of the speed on my pins for the motor start from 0-99 instead of 0-255 since my pins are PWM and i dont know how to do that honestly

Where does the 0-99 come from?

Hi,

Your code at the moment just stops, fwd, reverse, there is no PWM, no speed control.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What are you using as a motor driver?
What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

i totally get what are you saying but my prof asked me to edit the code using analogWrite instead of digitalWrite and change the speed range which is 0-255 to 0-99 , so i was thinking about some sort of mapping the range but i dont know how and im not sure if mapping would work

as i said before he wants the range starts from 0-99 instead 0-255 ( i have to use analogWrite to use that) for example analogWrite(0,50); which should equal to analogWrite(0,127); somehow and i dont know how to do that.

Yes, but where does the 0-99 value come from?

idk he just wants us to convert it ig somehow
he came up with this idea all of a sudden
its not the deal right here
my problem is how to convert that if you get what i mean
the number doesnt mean anything here, he couldve chosen different range but he wanted 0-99

Have you tried using the map function?
What went wrong?

I don't know how to apply it in this situation honestly that's what I have been trying the whole time

do you have a clue on how to apply the map function in this situation?

Then step away from the hardware, and write a simple sketch that generates (for loop) values in the range 0-255, and uses the map function to convert them to 0-99.
Print both values.

after converting them i have to send the values to my motor to run within a specific speed, how is that possible?

You'd have to have some sort of feedback.
This could be as simple as a once-per-revolution pulse to act as an rpm counter, or some sort of encoder.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.