Changeable servo motor and DC motor [HELP]

Hey, I'm having my programming exam in about a week and I really need some help! I've got this car and it uses two lego DC motors to go both forwards and backwards. Im using a IR remote to controll the car and if I hold in a button on the controller, it will repeat that same code until i stop holding it down. So, here is my problem. I want to use that function on the controller to controll the speed of the motor. I want it to start on 0, and as I hold down the button, I want it to increase until it reaches 255. I have figured out this code, but it doesn't work. PLEASE help me! :slight_smile:
(fart means speed in norwegian, and this is just a part of a longer code, but it is the part that doesn't work)

int fart = 0;
for (int motorValue = 0; motorValue >= 255;) {
if (results.value == 16730805) {
motorValue + 5;
fart = motorValue;
}
}

if (results.value == 16730805) {
move(1, fart, 1);
move(2, fart, 1);
}

      motorValue + 5;

needs to be

      motorValue += 5;

to have any effect. Calculating motorValue+5 and throwing it on the floor isn't useful!

Changed it, and it still doesn't work.

We need to see the rest of the code. Where is motorValue declared? This code is inside some loop - does motorValue keep its value outside of that loop?

When it "doesn't work", what does it actually do?

here is the entire code, some of the names may be on norwegian. the function I am looking for a function that makes the DC motor go faster as I hold the button on my controller down.

#include <Servo.h>

#include <IRremote.h>

Servo myservo;
int RECV_PIN = 7;
const int ledpin = 13;

IRrecv irrecv(RECV_PIN);

decode_results results;

boolean LEDon = true; // initializing LEDon as true
boolean SERVOon = true;

int STBY = 10; //standby

//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction

void setup()
{
pinMode (ledpin, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
myservo.attach(6);
myservo.write(90); // set servo to mid-point

pinMode(STBY, OUTPUT);

pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);

pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
}

void loop() {

if (irrecv.decode(&results)) {
Serial.println(results.value);
irrecv.resume(); // Receive the next value
}

if (results.value == 16738455) // change zero to your IR remote button number
{
if (LEDon == true) // is LEDon equal to true?
{
LEDon = false;
digitalWrite(ledpin, HIGH);
delay(100); // keeps the transistion smooth

}

else
{
LEDon = true;
digitalWrite(ledpin, LOW);
delay(100);

}
}

// svinge til venstre
if (results.value == 16716015) {

if (SERVOon == true) // is LEDon equal to true?
{
SERVOon = false;
myservo.write(0);
delay(15);
}

else
{
SERVOon = true;
delay(15);
myservo.write(90);
}
}

// svinge til høyre
if (results.value == 16734885) {

if (SERVOon == true) // is LEDon equal to true?
{
SERVOon = false;
myservo.write(180);
delay(15);
}

else
{
SERVOon = true;
delay(15);
myservo.write(90);
}
}

//sving reset
if (results.value != 16734885 ||16716015){
myservo.write(90);
}

// for å gå frammover
if (results.value == 16718055) {

move(1, 255, 0); //motor 1, full speed, right
move(2, 255, 0); //motor 2, full speed, right
}

// for å gå bakover
if (results.value == 16730805) {
int fart = 0;
for (int motorValue = 0; motorValue >= 255;) {
if (results.value == 16730805) {
motorValue += 5;
fart = motorValue;
}
}

if (results.value == 16730805) {
move(1, fart, 1);
move(2, fart, 1);
}
}

// stop
if (results.value == 16726215) {
stop();
}

}
void move(int motor, int speed, int direction) {

//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

digitalWrite(STBY, HIGH); //disable standby

boolean inPin1 = LOW;
boolean inPin2 = HIGH;

if (direction == 1) {
inPin1 = HIGH;
inPin2 = LOW;
}

if (motor == 1) {
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
} else {
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}

void stop() {
//enable standby
digitalWrite(STBY, LOW);
}

Please use [ code ] tags. The forum software eats some of the C program elements. Edit your post.

From the code that survived, it appears that there's no way for the results to become different while in that for() loop and motorValue isn't valid outside the loop. Then the loop seems to immediately exit because there's no way for motorValue to get above 255 within a single iteration.

Go back and read about for() loops and think about the sequence of your logic.

Why not change the code below:

 // for å gå bakover
   if (results.value == 16730805) {
  int fart = 0;
  for (int motorValue = 0; motorValue >= 255;) {
    if (results.value == 16730805) {
      motorValue += 5;
      fart = motorValue;
    }
  }

To:

  if (results.value == 16730805 && motorValue < 250) 
  {
    motorValue += 5;
  }

Don't you want to read from the remote each time you increment the value of motorValue?

I don't understand how "results.value" is supposed change in the for loop. I also don't understand the purpose of the "fart" variable.

Okay, so I've tried this now:

int motorValue = 0;
if (results.value == 16730805 && motorValue < 250)
{
motorValue += 5;
}

if (results.value == 16730805) {
move(1, motorValue, 1);
move(2, motorValue, 1);
}

problem with this is that after i push the button, it adds 5 to motorValue, and then the loop starts again, and int motorValue = 0; resets it back to 0. How do i get this value to not reset? so that it stacks for each time i push the button? 5-10-15... etc

Hey, im working on my arduino Lego car for a school project and it uses one servo motor together with some lego cogs and other lego parts. I Have the servo set to 90 degrees in the setup, so when i boot the arduino up, it starts in the center position.

For the left turn, I push a button on a IR controller and maken the stepper turn to 0 degreees.
and for the right turn I push a button to turn the stepper to 180 degrees, pretty simple.

Here is the problem:

I want to make the car center itself, like a real life car. So that if I not turn left, it wil drive straight forward, and if I don't turn right, it wil also drive straight forward. The problem with this is that if I say:

If(***){
(turn left)
}

else{
center
}

the servo will try to center when i turn right aswell.

Hope you understand my problem, hope some of you can help me :slight_smile:

if(***)
    turnRight();
else if(***)
    turnLeft();
else
    centre();

Thanks, but it still seems like when i try to turn one way, the code is "holding it back" in such a way that when I try to push the left button, the code wants it to stay center :S

oxywrath:
, the code is "holding it back"

If you stop holding back your code and post it here so we can see what you are talking about you might get some useful suggestions.

...R

if(results.value == 16716015){
     myservo.write(0);
      delay(15);
}
else if(results.value == 16734885){
        myservo.write(180);
      delay(15);
}
else{
    myservo.write(90);
    delay(15);
}

Okay so as suggested, this is now my code for turning. If i push one button, it turns left, another button it turns right, and if i dont push any of them, it should be standing in the middle.

problem is:

When i try to turn left, it won't turn left. It kind of just wiggles a little. Same for right.

I also want another function that you guys may help me with :slight_smile:

I want a function added to this, that lets me hold the button on the IR controller and when I do that, the servo will turn more and more that way.

ex;

if I hold the button for 2 seconds it goes from 90 degrees (the middle) to 115 degrees. If i hold it for 4 seconds it goes from 90 to 140....

oxywrath:

if(results.value == 16716015){

myservo.write(0);
     delay(15);
}
else if(results.value == 16734885){
       myservo.write(180);
     delay(15);
}
else{
   myservo.write(90);
   delay(15);
}




Okay so as suggested, this is now my code for turning. If i push one button, it turns left, another button it turns right, and if i dont push any of them, it should be standing in the middle. 

problem is:

When i try to turn left, it won't turn left. It kind of just wiggles a little. Same for right.

you're allowing a whole 15ms for the car to make the turn. It does exactly that, then self-centres as you asked. It's working perfectly, according to your specification.

Edit: If you hold the button down, more than likely you will be getting a repeat code of FFFFFFFF. You might want to handle that if you want the car to keep turning while the button is held down......

I also want another function that you guys may help me with :slight_smile:

I want a function added to this, that lets me hold the button on the IR controller and when I do that, the servo will turn more and more that way.

ex;

if I hold the button for 2 seconds it goes from 90 degrees (the middle) to 115 degrees. If i hold it for 4 seconds it goes from 90 to 140....

One thing at a time, hey?

Okay so by having the delay, it only turns for 15ms, then it resets back to center? In that case I dont think my two Ideas wil work together. because then I need the turn code, to ex:

turn left when I hold the button down, and center itself when I let it go.

And I don't think that will work If i want to change how much it turns by holding the button down, right?

So the new Idea should be, have a button for left, have a button for center, and have a button for right.
and make the left and right buttons so that if I hold the right button down, it wil turn more and more to the left?

does this seem reasonable?

oxywrath:
Okay so by having the delay, it only turns for 15ms, then it resets back to center? In that case I dont think my two Ideas wil work together. because then I need the turn code, to ex:

turn left when I hold the button down, and center itself when I let it go.

You should be able to get this to work. Load "IRrecvDemo.ino" and check what you get as a repeat code. As I said, you're probably getting FFFFFFFF, so the car immediately centres even when you hold the button down, because FFFFFFFF is not the turn left code (or turn right code). You can handle this in code if you think about it.
It's 12:30 am here, and I'm too tired to help you with it right now, but you'll get it with a little thought and planning. It's good practice. :smiley:

I'm off to bed. Good luck.........

Thanks, I will try my best! I'm currently looking at my teachers code for a earlier task we had, that may be to some help aswell. But thanks alot for the help, it really gave me some insight in whats really happening in the code :slight_smile: Good night! hope we can stay in touch if I need some more help with this project for my exam. cheers :smiley:

You'll need to define the codes, ('rightCode' and 'leftCode'), at the top of your sketch, and there might be errors since I'm so tired, but this might help:-
(And note that I commented "Allow 150mS" for the turns. 15mS isn't long enough for the servo to swing 90 degrees.)

void loop()
{
    if (irrecv.decode(&results))
    {
        unsigned long code=results.value;
        if (code == leftCode)                                               // Left.
        {
            lastCode = code;                                                // Store last code.
            turnLeft();                                                     // Allow 150mS.
        }
        else if ((code == 0xFFFFFFFFUL) && (lastCode == leftCode))          // Left.
            turnLeft();                                                     // Allow 150mS.
            
        else if (code == rightCode)                                         // Right.
        {
            lastCode = code;                                                // Store last code.
            turnRight();                                                    // Allow 150mS.
        }
        else if ((code == 0xFFFFFFFFUL) && (lastCode == rightCode))         // Right.
            turnRight();                                                    // Allow 150mS.
        irrecv.resume();                                                    // Prepare to receive the next value.
    }
    else                                                                    // Return to centre.
        centre();                                                           // Allow 150mS.
}

Even if there are errors, at least it's a starting point. Now I'm really off to bed. (1:25am)
And of course, this will only work if you do get an FFFFFFFF repeat code, so check that first. :wink:

Oops - you'll need to define:-

unsigned long lastCode;

Globally, at the top of your sketch

Thanks man! I will check it out, and hopefully it will work as I want it to :smiley: Hope you have a good night of sleep, because you deserve it :slight_smile:

oxywrath:
How do i get this value to not reset?

One way is to make it global.

I don't know how one initializes a variable within the loop function and not have the value reset to its initialized state. Maybe someone else does.

For now, make it global by initializing the value at the top of the program.

Read the "How to use this forum" thread. Particularly item #7 about how to use code tags.