Go Down

Topic: setup (Read 611 times) previous topic - next topic

UKHeliBob

Where are the curly brackets around the body of the setup() and loop() functions ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

spruce_m00se

sorry dave

Code: [Select]
// First Example in a series of posts illustrating reading an RC Receiver with
// micro controller interrupts.
//
// Subsequent posts will provide enhancements required for real world operation
// in high speed applications with multiple inputs.
//
// http://rcarduino.blogspot.com/
//
// Posts in the series will be titled - How To Read an RC Receiver With A Microcontroller

// See also http://rcarduino.blogspot.co.uk/2012/04/how-to-read-multiple-rc-channels-draft.html

#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead

#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car

volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriod = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
int led = 13;
int led2 = 3;
int led3 = 5;
int buzz = 4;
int val = 50;

int Min = 0 ;
int thresh=50;
int cal =0;


// we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
// is clearer for this first example

void setup()
{
  // tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
  // catching these changes will allow us to calculate how long the input pulse is
  attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);

  Serial.begin(9600);
  pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
tone (4, 1500, 50);
delay (2000);



////////////////////if RC is high at setup , calibrate HIGH point according to RC max ////////////////// 
if(nThrottleIn> 1800){
   cal = nThrottleIn;
   digitalWrite(led2,HIGH);
   delay (300);
   digitalWrite(led2,LOW);
   delay (300);
   digitalWrite(led2,HIGH);
   delay (300);
   digitalWrite(led2,LOW);
   delay (300);
   digitalWrite(led2,HIGH);
   delay (300);
   digitalWrite(led2,LOW);
}
/////////////////if no calibration value was set at startup , set it to 2000 default ////////////////////
if (cal=0){
  cal = 2000;
}
Serial.println("calibration = ");
Serial.println(cal);
delay(10000);
}

void loop()
{

     
     
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
if(bNewThrottleSignal){


   Serial.println(nThrottleIn);
   Serial.println(val);
   Serial.println(cal);


   // set this back to false when we have finished
   // with nThrottleIn, while true, calcInput will not update
   // nThrottleIn
   bNewThrottleSignal = false;
}




////////////scaling the RC inputs to PWM values /////////////////////////
val = map(nThrottleIn,1000,1950, 0, 255);




////////////////buzzer tone control to mimic motor RPM for testing //////////////////////
if (nThrottleIn > 1000){
tone(4,nThrottleIn,2);
}


///////////////////// motor control within normal limits,///////////////////
if ((val > thresh)&&(val<=255)){
analogWrite(led3, val);

}
//////////// turn off motor if throttle is below threshold////////////////////////
if (val < thresh){
  analogWrite(led3, 0);
  }
 
///////////max motor output ////////////////////// 
if (val > 255 ){
analogWrite(led3, 255);
digitalWrite(led2, HIGH);

}
 
//////////////////turn of leds during mid range ///////////////////////////////
if ((nThrottleIn<=1900)&&(nThrottleIn>=1100)){
digitalWrite(led2, LOW);
digitalWrite(led, LOW);


////////////////////7turn on leds for high and low throttle////////////////////////////
if(nThrottleIn>1900){
  digitalWrite(led, HIGH);
  }
  if(nThrottleIn<1100){
  digitalWrite(led2, HIGH);
  }
// other processing ...
}

void calcInput()
{
  // if the pin is high, its the start of an interrupt
  if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
  {
    // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
    // easy to understand and works very well
    ulStartPeriod = micros();
  }
  else
  {
    // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
    // start time ulStartPeriod from the current time returned by micros()
    if(ulStartPeriod && (bNewThrottleSignal == false))
    {
      nThrottleIn = (int)(micros() - ulStartPeriod);
      ulStartPeriod = 0;

      // tell loop we have a new signal on the throttle channel
      // we will not update nThrottleIn until loop sets
      // bNewThrottleSignal back to false
      bNewThrottleSignal = true;
    }
  }
    }


everything works as expected, until i try to change the
val = map(nThrottleIn,1000,1950, 0, 255);
to
val = map(nThrottleIn,1000,cal, 0, 255);

cal never gets changed from zero, despite there being an if statement in setup to deal with a zero, and the claibration routine that should change cal to the max input received during startup



PeterH

Code: [Select]

if (cal=0){
  cal = 2000;
}


Needs a ==.
I only provide help via the forum - please do not contact me for private consultancy.

spruce_m00se

great ,
that should stop the error occuring due to not having changed the value of cal just before that point in the code,

but the question still exists as to why the "cal" hasnt been changed to the "nthrottlein" value,

thanks,

spruce_m00se

ah ,
or not,
apparently it now works,
great,


thanks,

Go Up