Go Down

Topic: Controlling a DC motor using PWM and a potentiometer (Read 3 times) previous topic - next topic

matt9522

stopping the motor isn't really a priority, the code you wrote for getpot had an error saying void was not ignored how it should be and motorForward has an error about too many arguments.  As the code stands, all i want is to make everything go in one direction.

by the way, 100 and 140 are arbitrary numbers, I modified the program when I thought i needed to turn the motor off.

@retroplayer what do you mean by void run()?

Retroplayer


@retroplayer what do you mean by void run()?


void run(){

}

is a function named "run" that "returns" nothing (void). A function is a collection of code wrapped together that does something. Since your code has to make decisions and branch around and do different things, you use functions to separate stuff.

int run(){

}

would also work fine, but the int means that you expected to return an int FROM the function.

Here's an example:
Code: [Select]

int getPot()
{

  val = analogRead(pot);
  constrain(val,90,255); //this restricts the analog value to between 90 and 255
  if(val=<90){
    val = 0;
  }
 
  return val;

}

void loop(){
int newval;

newval=getpot(); //newval will now call getpot, which returns val, and assign it to newval. So newval = val

analogWrite(motorpin, newval);

}


see the difference in how I called getpot from this code and my original? This time it calls getpot() and getpot() actually is the value. I didn't do it this way before because it is not as clear for a beginner what is happening.

btw, here is the complete code with the changes I suggested you make:

Code: [Select]


int pot = 0;
int relay = 3;
int motorPin = 11; 
int val = 0;

void setup()                    // run once, when the sketch starts
{
  //Serial.begin(9600);           // set up Serial library at 9600 bps
//  pinMode(pot, INPUT); //don't need this
  pinMode(relay, OUTPUT);
  pinMode(motorPin, OUTPUT);
}

void getPot()
{

  val = analogRead(pot);
  constrain(val,90,255); //this restricts the analog value to between 90 and 255
  if(val=<90){
    val = 0;
  }


}

void run()
{

  analogWrite(motorPin, val); //this will run the motor at the speed set by the pot value

}


void loop()                     // run over and over again
{
  getpot(); //THIS CALLS THE FUNCTION NAMED getpot ABOVE WHICH SETS val BASED ON THE ANALOG INPUT
  run(); //THIS CALLS THE FUNCTION NAMED run ABOVE

}


The flow is controlled by "loop()." In that function, I am calling the "getpot" function. That reads the value of the potentiometer and stores it in the variable "val" after it adjusts it to 90 to 255. Then control returns back to loop where it next calls "run" which sends "val" to the analog output which runs the motor at the desired speed. Loop is called automatically over and over again, so whenever a function that is called completes, control returns to loop. and when loop runs out of stuff, it just starts over and runs it all again. It is the only function that does this. Any other functions will not do anything unless you call them.

robtillaart


Quote
constrain(val,90,255); //this restricts the analog value to between 90 and 255

no it does not, however

val = constrain(val,90,255); //this restricts the analog value to between 90 and 255

does work
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Retroplayer



Quote
constrain(val,90,255); //this restricts the analog value to between 90 and 255

no it does not, however

val = constrain(val,90,255); //this restricts the analog value to between 90 and 255

does work


Of course... my mistake

Go Up