IR sensor and motor beginner help please!

I have been trying to make a program that uses the 2,5 numbers on my IR remote to control a motor's speed(later the direction with other keys). But when I upload the code the motor does not move :frowning: .I have tried everything I can think of, the problem is not a physical error because using this code I can make the motor move.

#include <IRremote.h>

int motorPin = 6;
void setup()
{
pinMode(motorPin, OUTPUT);

}

void loop()
{
analogWrite(motorPin, 100);
}

All I want is to use the integer "Num" to control the speed of the motor, I change Num using the IR remote.
Other info
when I look at what it is printing it is printing the correct numbers.
The minimum speed of my motor is 40.
I am using a 3-6v motor from an arduino kit.
I am very new to arduino, just got one this Christmas.
The code does not come up with any errors
The majority of the code was not written by me, it was just copied and pasted from other places and merged together.
I have spent over 3 hours trouble shooting but I have not gotten any results.

Here is my code:

#include <IRremote.h>

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

int motorPin = 6;

int Num = 60;
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(false);
while (! Serial);

}

void loop()
{

if (irrecv.decode(&results)){

switch(results.value){
case 0xFF38C7: //Keypad button "5"
Num = Num - 10;
if(Num > 250)
{Num = 250;}
if(Num < 40)
{Num = 40;}
Serial.println(Num);
analogWrite(motorPin, Num);
}

switch(results.value){
case 0xFF18E7: //Keypad button "2"
Num = Num + 10;
if(Num > 250)
{Num = 250;}
if(Num < 40)
{Num = 40;}
Serial.println(Num);
analogWrite(motorPin, Num);
}

irrecv.resume();

}

Why are you using a switch case statement for one case?

I not 100% sure how the switch cases work but I think if I want to add more buttons to my program I should use a switch to be able to easily add more.

GymBoyB:
I not 100% sure how the switch cases work but I think if I want to add more buttons to my program I should use a switch to be able to easily add more.

But you aren't even doing that... You are using a separate switch case for every different case. A single test combined with a single code block is what an "if" statement is for.

You obviously haven't even looked at how the switch-case is used in the library that you're using.

It seems to work at what it's purpose is, could that be the problem? If not there is no reason to change it.

So to be clear, on the serial monitor you see Num printed, increasing and decreasing with the appropriate IR buttons, from 40 to 250? But the motor never does anything. Is that correct?

Problems with motors not moving are often connected to power. So how exactly is your motor connected? Since you're just using one pin I guess you don't have any motor driver or similar? So what is powering the motor?

If you can post a complete circuit diagram, that would be ideal. A photo of a hand-drawn circuit is fine provided it shows all components and pin numbers.

Using switch/case like that is very odd but it should still work.

Steve

Yes on the serial monitor I see Num printed, ranging from 40-250 and increasing / decreasing as I press the
buttons on the remote. I know that the problem is most likely not power because I can get the motor to spin without changing any physical thing, all I have to do is run the first program in my original post. My motor is connected to a 5 volt line, my motor can run from 3v to 6v. I think it could be because I have my IR receiver connected to the same 5v line as my motor, could that be causing the problem? I don't know how to post a circuit diagram but I am sure they are wired correctly become they bother work individually.
Thank you for replying!

I have done some more tests and you are right. The power was and is the problem. I can get the motor to move or the IR sensor to detect. All i have to do is unplug the motors 5v power and then the sensor works. Then if I unplug the sensors power the motor works. I can use the controler when the sensor is plugged in and it works, then I unplug that and plug in the motor and then the motor spins according to what Num is, but not both at once. How can I supply the motor and sensor 5v? I have tried a power supply unit that came in my kit. It supply's 5v and 3v from a 9v battery.( there is an indicator light that it is on so I don't think the battery is dead) But it did not work. I have tried putting the positive wire for the motor on the power supply 5v and the other negative in the arduino ground. Also tried putting the negative in the ground on the power supply. How can I supply 5v to both the motor and sensor?

Good you're getting closer. But I don't understand your description. That's why circuit diagrams are needed. The motor speed is apparently controlled by PWM from Pin6. But how exactly is Pin6 connected to the motor?

Anyway I'm guessing your 9V battery is one of those rectangular PP3 types. They cause loads of problems being far too feeble to drive motors, servos or most other things. If you replace it with 6 x AA batteries you have a much better chance of things working.

Steve

I don't know how to create a circuit diagram but I do have a video on youtube showing my setup as best I can. Sorry for bad quality.

Link:

No sorry, can't make much out in that video except a rat's nest of wires. It looks like there may be a transistor of unknown type in there that you have never mentioned.

Also in that video I can't see any power of any sort connected to anything. And since we had already decided that the problem is with the power that's not helpful.

So I'll guess that it is all powered via the Arduino USB connection. If so, then that's your problem. It is NEVER a good idea to power motors from the Arduino board (even if it sort of seems to work). They can take too much power and can damage the board. Leave the IR Receiver powered from the board but get a set of 4 x AA batteries or some other 5-6V supply and use that just for the motor power.

Steve