functions using PWM value - MOtor control w/ L293D

Hi everyone !

I do have some issues programming my sketch. What I want to do is controlling a bidirectionnal motor using PWM w/ L293D
First of all, my first version of the sketch worked well, but i couldn’t changed the motor speed when running, it took the speed only once a loop… (dunno if you get the point…)
Well, this is my code:

int pinmotor1 = 2;
int pinmotor2 = 3;
int potentiometer = 5;

// SETUP ************************************
void setup() {
Serial.begin(9600);

pinMode(5,INPUT);
pinMode(pinmotor1, OUTPUT);
pinMode(pinmotor2, OUTPUT);
}

//LOOP***************************************
void loop(){
  int motor_speed = getspeed(potentiometer);

  drive_forward(pinmotor1, pinmotor2, motor_speed);
  delay(4000);
  drive_backward(pinmotor1, pinmotor2, motor_speed);
  delay(4000);

}


// VOID DEFINITION******************************
int getspeed(int x){
  int v;
  v = analogRead(potentiometer);
  v /= 4;
  v = max(v, 90);
  v = min(v, 255);
  return v;
}

int drive_forward(pinmotor1, pinmotor2, motor_speed){
digitalWrite(pinmotor1, HIGH); 
analogWrite(pinmotor2, motor_speed); 
}

void drive_backward(pinmotor1, pinmotor2, motor_speed){
digitalWrite(pinmotor1, LOW); 
analogWrite(pinmotor2, 255-motor_speed); 
}

void motor_stop(pinmotor1, pinmotor2){
digitalWrite(pinmotor1, LOW); 
digitalWrite(pinmotor2, LOW); 
delay(25);
}

I get this error: error: ‘pinmotor1’ was not declared in this scope

I tried to insert motor_speed inside the function drive_forward and drive_backward in order to be able to change the speed during the 4 secondes while the motor is running, before I wasn’t…

Thanks for your help

I get this error: error: 'pinmotor1' was not declared in this scope

It would help if you posted the whole of the error code as that would say exactly where the compiler is confused.

Generally it is not a good idea to pass a global variable into a function with the same name.

it took the speed only once a loop

You have an 8 second delay between each reading of the potentiometer caused by calls to the the delay() function. You need to use a different method to allow the motor to run for 4 seconds in each direction whilst reading the potentiometer value.

Have a look at the BlinkWithoutDelay example to see how to use millis() to check whether the run time is up every time through the loop() function. This will allow you to read the potentiometer and adjust the speed whilst the motor is running.

@UKHeliBob: That seems to be perfect ! I understood it and I'm going to experimence it !

@Grumpy_Mike: here is the whole error code :fearful:

Feel free to tell me what is wrong... I started Arduino programming few weeks ago, and I would love to learn more !

motor_control_PWM_2:5: error: 'pinmotor1' was not declared in this scope
motor_control_PWM_2:5: error: 'pinmotor2' was not declared in this scope
motor_control_PWM_2:5: error: 'motor_speed' was not declared in this scope
motor_control_PWM_2:5: error: initializer expression list treated as compound expression
motor_control_PWM_2:6: error: variable or field 'drive_backward' declared void
motor_control_PWM_2:6: error: 'pinmotor1' was not declared in this scope
motor_control_PWM_2:6: error: 'pinmotor2' was not declared in this scope
motor_control_PWM_2:6: error: 'motor_speed' was not declared in this scope
motor_control_PWM_2:7: error: variable or field 'motor_stop' declared void
motor_control_PWM_2:7: error: 'pinmotor1' was not declared in this scope
motor_control_PWM_2:7: error: 'pinmotor2' was not declared in this scope
motor_control_PWM_2.ino: In function 'void loop()':
motor_control_PWM_2:18: error: 'drive_forward' cannot be used as a function
motor_control_PWM_2:20: error: 'drive_backward' was not declared in this scope
motor_control_PWM_2.ino: At global scope:
motor_control_PWM_2:36: error: redefinition of 'int drive_forward'
motor_control_PWM_2:5: error: 'int drive_forward' previously defined here
motor_control_PWM_2:36: error: 'motor_speed' was not declared in this scope
motor_control_PWM_2:41: error: variable or field 'drive_backward' declared void
motor_control_PWM_2:41: error: 'motor_speed' was not declared in this scope
motor_control_PWM_2:46: error: variable or field 'motor_stop' declared void

Most of your function definitions such as

int drive_forward(pinmotor1, pinmotor2, motor_speed)

do not have data types defined for the parameters.

Also, that particular function is defined as returning an int but it does not return anything. The getspeed() function is correctly declared in both respects.int getspeed(int x)

Incidentally, they are functions, not voids, despite what your comment says

// VOID DEFINITION******************************