DC MOTOR

this is my code for the motor:

int left1= 3;
int left2= 4;
int PWMLEFT=10;
int right1= 5;
int right2= 6;
int PWMLEFT=11;

void setup ()
{
pinMode(left1, OUTPUT);
pinMode(left2, OUTPUT);
pinMode(right1, OUTPUT);
pinMode(right2, OUTPUT);
}

void loop()

{
digitalWrite(left1, HIGH);
digitalWrite(left2, LOW);
digitalWrite(right1, HIGH);
digitalWrite(right2, LOW);
}

/
but how do u control the speed of the wheels etc using PWM??????

How is it wired?

heres the schematic

int PWMLEFT=10;
int PWMLEFT=11;

That's one way to do things, I guess.

An even better way is to post code that will actually compile.

Have you looked at the analogWrite() function?

int left1= 4;
int left2= 3;
int PWMLEFT=10;
int pwml=0;
int right1= 5;
int right2= 6;
int PWMRIGHT=11;
int pwmr=0;
int ldrleftvalue;
int ldrleftpin = 0;
int ldrrightvalue;
int ldrrightpin=1;

void setup ()
{
pinMode(left1, OUTPUT);
pinMode(left2, OUTPUT);
pinMode(PWMLEFT, OUTPUT);
pinMode(right1, OUTPUT);
pinMode(right2, OUTPUT);
pinMode(PWMRIGHT, OUTPUT);
pinMode(ldrrightpin, INPUT);
pinMode(ldrleftpin, INPUT);
}

void right()
{
pwml = (analogRead(PWMLEFT))/7;
pwmr = (analogRead(PWMRIGHT))/7;
analogWrite(PWMLEFT,pwml);
analogWrite(PWMRIGHT,pwmr);
digitalWrite(left1, LOW);
digitalWrite(left2, LOW);
digitalWrite(right1, HIGH);
digitalWrite(right2, LOW);

}

void forward()
{
pwml = (analogRead(PWMLEFT))/7;
pwmr = (analogRead(PWMRIGHT))/7;
analogWrite(PWMLEFT,pwml);
analogWrite(PWMRIGHT,pwmr);
digitalWrite(left1, HIGH);
digitalWrite(left2, LOW);
digitalWrite(right1, HIGH);
digitalWrite(right2, LOW);

}

void backward()
{
pwml = (analogRead(PWMLEFT))/7;
pwmr = (analogRead(PWMRIGHT))/7;
analogWrite(PWMLEFT,pwml);
analogWrite(PWMRIGHT,pwmr);
digitalWrite(left1, LOW);
digitalWrite(left2, HIGH);
digitalWrite(right1, LOW);
digitalWrite(right2, HIGH);

}

void left()
{ pwml = (analogRead(PWMLEFT))/7;
pwmr = (analogRead(PWMRIGHT))/7;
analogWrite(PWMLEFT,pwml);
analogWrite(PWMRIGHT,pwmr);
digitalWrite(left1, HIGH);
digitalWrite(left2, LOW);
digitalWrite(right1, LOW);
digitalWrite(right2, LOW);

}

void loop()

{
ldrleftvalue=analogRead(ldrleftpin);
ldrrightvalue=analogRead(ldrrightpin);
delay(1000);

if (ldrrightvalue<250 && ldrleftvalue<80)
{
backward();

}
else if (ldrrightvalue<250 && ldrleftvalue>80)
{
right();

}
else if (ldrrightvalue>250 && ldrleftvalue<80)
{
left();

}

else if (ldrrightvalue>250 && ldrleftvalue>80)
{
forward();

}

}

i got this its a line follwing robot but the else if statements dont work? they dont loop basically

i got this its a line follwing robot but the else if statements dont work? they dont loop basically

Not a single Serial.begin(), Serial.print(), or Serial.println() in sight.

Please send me the robot, so I can debug your code for you.

      pwml = (analogRead(PWMLEFT))/7;
      pwmr = (analogRead(PWMRIGHT))/7;

PWMLEFT and PWMRIGHT are digital pins, on the digital side of the board. You can not call analogRead() to read them.

then it should be digital write aswell? if i do that the wheels wont even move anymore?

      pwml = (analogRead(PWMLEFT))/7;
      pwmr = (analogRead(PWMRIGHT))/7;
      analogWrite(PWMLEFT,pwml);
      analogWrite(PWMRIGHT,pwmr);

I'm not sure what you are trying to do here. It looks like you are trying to read the current motor speed, then slow it down to a seventh of it's value.

yeh thats what im trying to do.

But you know what speed the motor is going, because you wrote it to the PWM

yeh thats what im trying to do.

You can't do it that way. They are digital pins, so the analogRead function will not read them. If you do a digitalRead(), all you will learn is whether the pin is on AT THAT INSTANT, or not. The return value will be 0 or 1. Dividing that by 0 guarantees you a value of 0.

so how would i go about it? can you provide me with some links or examples? because the things i have learn at university are useless.

Can you tell us what you want to do?

basically my teachers gave me the schematic and then we had to make the PCB board and make a buggy, then program it to follow a track,

its a black track on a white background, like black tape on white paper, and the robot has to follow the track from start to finish, and i need help programming it to follow the track.

Nowhere do you set an initial value for the PWM pins. So, any attempt to read them, if it were possible, would find the value of 0.

Every one of your functions simply wants to slow the robot down. A few turns, or straightening, and your stopped, anyway.

Somewhere you need to set a speed. There is no real reason ever to reduce it. Just veer left or right (slow one side while speeding up the other) as needed to stay on the line.

The key is to start slow. Adjust the amount you speed up one side and slow own the other based on the difference between the two readings - not the absolute value of either one.

When you get it to crawl along the track, increase the speed, and run the track again. If you find your robot unable to follow the line, you are going too fast.

When you can follow the line at a fixed speed, you can investigate increasing the speed until you need to turn. Then, slow down.

But, get the robot to follow the line at a constant speed, first.

sorry what does this mean:

Somewhere you need to set a speed. There is no real reason ever to reduce it. Just veer left or right (slow one side while speeding up the other) as needed to stay on the line. <----thats not true the speed is too much and it does not follow the track so yes there is a real reason to reduce the speed.

The key is to start slow. Adjust the amount you speed up one side and slow own the other based on the difference between the two readings - not the absolute value of either one.<---------based on which two readings differences, the PWM?

also can i use such an outline to control speed?:

int   sensorVal;          
int   sensorPin = 3;
int   maxInput = 1023;
float voltage;
float maxVoltage = 5.0;

void setup () {
  Serial.begin(9600);  
}

void loop () {
  sensorVal = analogRead(sensorPin);
  voltage = ((float)sensorVal)*maxVoltage/maxInput;
  Serial.print(“Voltage = “);
  Serial.println(voltage);
}

also this is the gearbox im using:

http://www.robotshop.com/eu/tamiya-twin-motor-gearbox.html?utm_source=google&utm_medium=base&utm_campaign=GoogleUK