Go Down

Topic: Arduino infrared problems (Read 93117 times) previous topic - next topic

jrfitzny

I'm actually using the arduino to drive a MOSFET, which which control the motor. Thanks for the help!

jrfitzny

I'm using pmalmsten's code on pg 1 of this thread.

I added the following to the end; it uploads, but doesn't output any PWM voltage



int motor_pin = 10;  // define the pin which will send the motor commands
int motor_input = result;


void setup ();

 
pinMode (motor_pin, OUTPUT);

void loop ();

analogWrite(motor_pin,motor_input);} //uses PWM of Arduino to change motor speed.


Even if I change int_motor pin = (to a value 0-255) it still doesn't output a voltage. Any idea what I'm doing wrong?

gbulmer

If you Serial.println(motor_input);
what value do you get?

Have you tried using an LED+resistor in place of the MOSFET to debug it?
Does the LED come on?

GB

jrfitzny

I tried Serial.PrintIn(motor_input)....the serial monitor is only reading the IR values

which shows Key Received: (number from 219-500+); depends on what button I'm pressing.

I'm actually trying the LED + resistor, but it's not coming on. I'll just insert the code, and if you could please troubleshoot it, i'd be in your debt. I put the //////////////// where the IR programming stops and the PWM programming begins

Code: [Select]

int ir_pin = 2;                        //Sensor pin 1 wired through a 220 ohm resistor
int led_pin = 13;                      //"Ready to Recieve" flag, not needed but nice
int debug = 0;                         //Serial connection must be started to debug
int start_bit = 2000;                  //Start bit threshold (Microseconds)
int bin_1 = 1000;                      //Binary 1 threshold (Microseconds)
int bin_0 = 400;                       //Binary 0 threshold (Microseconds)


void setup() {
 pinMode(led_pin, OUTPUT);            //This shows when we're ready to recieve
 pinMode(ir_pin, INPUT);
 digitalWrite(led_pin, LOW);          //not ready yet
 Serial.begin(9600);
}

void loop() {
 int key = getIRKey();                //Fetch the key
 Serial.print("Key Recieved: ");
 Serial.println(key);
}


int getIRKey() {
 int data[12];
 digitalWrite(led_pin, HIGH);         //Ok, i'm ready to recieve
 while(pulseIn(ir_pin, LOW) < 2200) { //Wait for a start bit
 }
 data[0] = pulseIn(ir_pin, LOW);      //Start measuring bits, I only want low pulses
 data[1] = pulseIn(ir_pin, LOW);
 data[2] = pulseIn(ir_pin, LOW);
 data[3] = pulseIn(ir_pin, LOW);
 data[4] = pulseIn(ir_pin, LOW);
 data[5] = pulseIn(ir_pin, LOW);
 data[6] = pulseIn(ir_pin, LOW);
 data[7] = pulseIn(ir_pin, LOW);
 data[8] = pulseIn(ir_pin, LOW);
 data[9] = pulseIn(ir_pin, LOW);
 data[10] = pulseIn(ir_pin, LOW);
 data[11] = pulseIn(ir_pin, LOW);
 digitalWrite(led_pin, LOW);

 if(debug == 1) {
   Serial.println("-----");
 }
 for(int i=0;i<11;i++) {              //Parse them
   if (debug == 1) {
       Serial.println(data[i]);
   }
   if(data[i] > bin_1) {              //is it a 1?
     data[i] = 1;
   }  else {
     if(data[i] > bin_0) {            //is it a 0?
       data[i] = 0;
     } else {
      data[i] = 2;                    //Flag the data as invalid; I don't know what it is!
     }
   }
 }

 for(int i=0;i<11;i++) {              //Pre-check data for errors
   if(data[i] > 1) {
     return -1;                       //Return -1 on invalid data
   }
 }

 int result = 0;
 int seed = 1;
 for(int i=0;i<11;i++) {              //Convert bits to integer
   if(data[i] == 1) {
     result += seed;
   }
   seed = seed * 2;
 }
 return result;                       //Return key number



/////////////////////////////////////////////////////////////////////

int motor_pin = 10;  // define the pin which will send the motor commands
int motor_input = result;


void setup ();

 
pinMode (motor_pin, OUTPUT);

void loop ();

analogWrite(motor_pin,motor_input); //uses PWM of Arduino to change motor speed.

Serial.println(motor_input);
}


gbulmer

#34
Apr 16, 2010, 02:40 am Last Edit: Apr 16, 2010, 02:42 am by gbulmer Reason: 1
jrfitzny - sorry for the slow response, the email alert got lost in some other stuff.

Is this exactly the code, or have you changed it before posting it here?

Nothing after the ///////////////////////// will be run.
Is that what you meant to happen?

You might want to make a copy of the code, and then trim out the stuff you don't want. It's a bit confusing to decide whether there is a mistake, and it should be executed or not.

Have I misunderstood something?

The only code I can see which would control the motor is after the return statement, so it will never get done.
Code: [Select]
 return result;                       //Return key number



/////////////////////////////////////////////////////////////////////

int motor_pin = 10;  // define the pin which will send the motor commands
int motor_input = result;


void setup ();

 
pinMode (motor_pin, OUTPUT);

void loop ();

analogWrite(motor_pin,motor_input); //uses PWM of Arduino to change motor speed.

Serial.println(motor_input);


I suggest tidy up the code, and take all the code after return, delete what you dont want, and put the rest wherever it should be.

You almost certainly don't want this code in getIRKey
Code: [Select]
void setup ();

 
pinMode (motor_pin, OUTPUT);

void loop ();


pinMode should be set in the setup function.

HTH
GB

jrfitzny

Gbulmer,

Did exactly what you said...code now works like a charm!
Thanks for the help :)

prashant.park

I am using TSOP 1838 IR receiver sensor to receive the signal,
surprisingly it gives readings with mega8 mcu, but it dosent work
with mega 328 or 168. however it dosent show any error while uploading the code. need support desperatly . . . . . :-?

Code: [Select]
int ir_pin = 2;    //Sensor pin 1, the data pin
unsigned long data[12];

void setup() {

 pinMode(ir_pin, INPUT);
  Serial.begin(9600);
}

void loop() {
 data[0] = pulseIn(ir_pin, LOW);    //Start measuring bits --the sensor defaults high, goes low on infrared
 data[1] = pulseIn(ir_pin, LOW);
 data[2] = pulseIn(ir_pin, LOW);
 data[3] = pulseIn(ir_pin, LOW);
 data[4] = pulseIn(ir_pin, LOW);
 data[5] = pulseIn(ir_pin, LOW);
 data[6] = pulseIn(ir_pin, LOW);
 data[7] = pulseIn(ir_pin, LOW);
 data[8] = pulseIn(ir_pin, LOW);
 data[9] = pulseIn(ir_pin, LOW);
 data[10] = pulseIn(ir_pin, LOW);
 data[11] = pulseIn(ir_pin, LOW);
 

 Serial.println("-----");    //Send them all
 Serial.println(data[0]);
 Serial.println(data[1]);
 Serial.println(data[2]);
 Serial.println(data[3]);
 Serial.println(data[4]);
 Serial.println(data[5]);
 Serial.println(data[6]);
 Serial.println(data[7]);
 Serial.println(data[8]);
 Serial.println(data[9]);
 Serial.println(data[10]);
 Serial.println(data[11]);
 
}




Go Up