IR Receiver to control PWM

Hey all, I'm using an IR receiver to control the PWM output (for motor control)...just like the topic says.

I have an LED set up to pin 10 just for testing purposes, but I can't seem to get an output voltage (even connected a multimeter).

The IR receiver is doing it's job, and it's outputting a result; however, I can't seem to get that result to translate to a PWM value.

Please check out my code and let me know what I'm doing wrong. It compiles fine without problems; just doesn't work.

The PWM programming starts near the bottom of the code at the /////////////////////

Thanks

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);
}

you have 2 sketches in the same code window, if that is how it is in the arduino ide (i dont know why it would compile) it surley would ignore the dupe void setup and loop functions

alright, so I deleted void setup and void loop; the end now looks like this; still doesn't work, but it compiles.

 int motor_pin = 10;  // define the pin which will send the motor commands
 int motor_input = result;
 
 
 pinMode (motor_pin, OUTPUT);
 
 analogWrite(motor_pin,motor_input); //uses PWM of Arduino to change motor speed.
 
 Serial.println(motor_input);
}

I am not sure what board you are using so make sure pin 10 is a PWM pin.
Also just try putting some value in place of 'motor_input'

I'm using the Duemilanove.

pin 10 is a PWM pin, and I have tried putting in a value instead of "result," but it didn't work :frowning:

I'm not sure what I'm doing incorrectly.

Perhaps just write a program solely to test the PWM, you can use one of the examples I think.
My programming isn't great so I am struggling to debug your code, but just make sure the hardware is working.

Your function returns before the PWM code, so it won't get run.

  return result;                       //Return key number

You also declare setup() and loop() within the body of getIRKey(), which is bizarre to me.

MarkT, you were absolutely correct.

I just moved the "return result" to the end...and viola, it worked like a charm.

Thanks for the help.