Go Down

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

spruce_m00se

Hi
im looking for the answer, but I cant find one quickly
maybe this is a stupid question ,
but can i use an if statement in the setup part of the code ?

I added some things and now it wont compile and i wonder if its the if statement in the setup causing it,
the problem is that i need an IF to run only once at the start of the code,

anyone know a clever way of doing it  ?

UKHeliBob

Post your whole code and the error messages to make it easier to provide help
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 ,

i had missed one of the closing brackets of an if statement,

now its ok ,

happy times,

spruce_m00se

ok
the question has changed to ,

can an integer be modified in the setup void , and then that modified value be used in the loop ?

example
Code: [Select]

int cal =0;
void setup()

if(nThrottleIn> 1800){
   cal = nThrottleIn;
digitalWrite(led2,HIGH);

void loop()
Serial.println(cal);


the led lights, so i know the throttle has been measured and is higher than 1800
but i always get 0 on the serial print , leading me to think the loop takes the int cal =0; when it starts,

any ideas?

AWOL

Stop posting snippets.
nThrottleIn would have to be a constant defined as a global.

If it is a variable with no initialiser, it will be zero, otherwise it will be whatever value you gave it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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