0 Members and 1 Guest are viewing this topic.
#include <IRremote.h> //Include remote library#include <Servo.h> //Include servo library //Define each of the necessary buttons as their respective codes#define play_button 765#define off_button 41565#define rewind_button 8925#define fastforward_button 49725#define funcstop_button 57885#define zero_button 26775 int receiver_pin = 2; //Initialize pin 2 for IR receiverint motor_right = 8; //Initialize pin 8 for motor to spin forwardint motor_left = 12; //Initialize pin 12 for motor to spin backwardint servo_pin = 5; //Initialize pin 5 for the servo int degree = 90; //define degree variable to be 90 at start Servo Servo1; //Create a servo object called Servo1 IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2decode_results output; //Decode the results from the IR receiver void setup(){ Serial.begin(9600); receiver.enableIRIn(); // Start to take the output from IR receiver //Setup the two motor pins as outputs and setup the servo pin pinMode(motor_right, OUTPUT); pinMode(motor_left, OUTPUT); Servo1.attach(servo_pin);} void loop(){ if (receiver.decode(&output)) //if receiver output is one of the previously defined values { unsigned int value = output.value; switch(value) //switch the function of the circuit //based on the output value from the IR receiver { case play_button: //spin motor forward when play button pressed for 3 sec digitalWrite(motor_right,HIGH); delay(1000); digitalWrite(motor_right,LOW); digitalWrite(motor_left,LOW); break; case funcstop_button: //Spin motor backward when func/stop button pressed for 3 sec digitalWrite(motor_left,HIGH); delay(3000); digitalWrite(motor_right,LOW); digitalWrite(motor_left,LOW); break; case off_button: //Stop motor when off button pressed digitalWrite(motor_right,LOW); digitalWrite(motor_left,LOW); break; case rewind_button: //Increase degree by 10 when rewind button pressed degree+=10; if(degree>180) degree=180; Servo1.write(degree); break; case fastforward_button: //Decrease degree by 10 when fast forward button pressed degree-=10; if(degree<0) degree=0; Servo1.write(degree); break; case zero_button: //Set servo back to 90 degrees when zero button pressed degree=90; Servo1.write(degree); break; //where's your default? } //switch } //if } //end of loop routine receiver.resume(); //Resume communication with IR receiver” // where is your main() ???? //e.g void main ( void) or int main (void) // { // setup(); // loop(); // do something else; // } If you only posted a code snippet please post the rest of your code, I haven't programmed an arduino but do have some experience with the C programming language
// where is your main() ???? //e.g void main ( void) or int main (void)
I cannot press any other buttons while the motor is running, and even thought the code is set to run the motor for 3 seconds, there is an awful delay until the IR receiver become available again for another.
Sometimes you need to do two things at once. For example you might want to blink an LED while reading a button press. In this case, you can't use delay(), because Arduino pauses your program during the delay(). If the button is pressed while Arduino is paused waiting for the delay() to pass, your program will miss the button press.
You don't need to write a main() function for arduino. IIRC there already is a canned one that calls setup() and loop(). (If you wrote one, the linker/loader would probably complain about a duplicate symbol.)Ed
As Rasputen mentioned, ...
Normally you would use a separate driver and supply a PWM signal to it.
In this case, the driver is a separate chip to control the motor, driven by a pulse width modulated signal from the Arduino.
You guys are having fun with me this aren't you??
#include <IRremote.h> //Include remote library#include <Servo.h> //Include servo library //Define each of the necessary buttons as their respective codes#define play_button 765#define off_button 41565#define rewind_button 8925#define fastforward_button 49725#define funcstop_button 57885#define zero_button 26775 int receiver_pin = 2; //Initialize pin 2 for IR receiverint motor_right = 8; //Initialize pin 8 for motor to spin forwardint motor_left = 12; //Initialize pin 12 for motor to spin backwardint servo_pin = 5; //Initialize pin 5 for the servo int degree = 90; //define degree variable to be 90 at start Servo Servo1; //Create a servo object called Servo1