-1

Im working with Arduino IDE, which doesn't allow nested functions, or calling a function before it had been declared(it doesnt jump to find it).. In my code, I need either one or the other.. Also, making it all one big function does not work because when i check back to an earlier sensor, i have to write what to do if yes and if no over and over forever. Here, im trying to sense light, if theres light, go to the PIR sensor, if not, run light sensor again. now if the PIR senses motion twice in 3.5 sec, check color, if not, check light. Now the color senses color and clasifies it(r g or b). now it checks for PIR again and if there's movement, it check color again and checks pir again. now if the pir doesnt sense movement, it sends to the lines to serial..

I declared some functions before I used them(print lines) but some of them not because i get stuck in a loop. When i check light, if its positive, i run the PIR,so i would need to declare the pir function before ligth. but in the pir, if its negative(no movement), i run light sensor, implying that i should declare light sensor before pir. Im stuck in that loop...

int lightPin = A0;
int valLight = 0;
int ledTrans = 2;
int pirTrans = 3;
int pirPin = 4;
int pirState = LOW;
int CalTime = 30;
int colorTrans = 5;
int s2 = 6;
int s3 = 7;
int OUTpin= 8;
boolean RD= false;
boolean GD = false ;
boolean BD = false;
int rfPin = 9;



void setup() {
pinMode(A0,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,INPUT);
pinMode(9,OUTPUT);

  Serial.begin(9600);
}



  void printlines();
  void pirSensor();
  void colorSensor();
  void pirSensor2();







void lightSensor() {
  //light sensor
  valLight = analogRead(A0);
  delay(1000);

  if (valLight >= 300) {
    ledTrans = HIGH;
    pirTrans = HIGH;
    pirSensor();
  } else {
    delay(8000);
    }
    }



void pirSensor() {

  // Calibration

  for ( byte i = 0; i < 30; i++) {
    delay(1000);
  }


// Check PIR state

int pirState =  digitalRead(2);
delay(1500);
pirState = digitalRead(2);

if (pirState == HIGH) {
  colorTrans = HIGH;
  colorSensor();
      } else if (pirState == LOW) {
  pirState = LOW;
  lightSensor();
}

}



      void colorSensor(){
      //Check Color reads pulse for RGB

    // void checkred
      digitalWrite(s2, LOW);
      digitalWrite(s3, LOW);
      unsigned int RW = 255 - (pulseIn(OUTpin, LOW)/ 400 - 1);  // turns into 0-255
      delay(6000);

  // void checkgreen
  digitalWrite(s2,LOW);
  digitalWrite(s3,HIGH);
  unsigned int GW =  255 - (pulseIn(OUTpin, LOW)/  400 - 1);
  delay(6000);

    // void checkblue
    digitalWrite(s2, HIGH);
digitalWrite( s3, HIGH);
  unsigned int BW = 255 - (pulseIn(OUTpin, LOW) / 400 - 1);
  delay(6000);
// seeing which color I got(r g or b)


if (RW > BW && RW > GW){
RD = true;    
 delay(7000);
  }  else if  (GW > RW && GW > BW){
    GD = true;
  delay(7000);
      } else if  (BW > RW && BW > GW){
    BD = true;
    delay(4000);
  }
}


void pirSensor2(){

pirState =  digitalRead(2);
delay(1500);
pirState = digitalRead(2);

  if(pirState = HIGH){
      colorSensor();
    }else{
      printlines();

    }
    }
















 void printlines(){
   if(RD){
   Serial.print("RED DETECTED");
 } else if(GD){
   Serial.print("GREEN DETECTED");
   }else if(BD){
    Serial.print("BLUE DETECTED");
  }else if(RD && GD){
    Serial.print("RED & GREEN DETECTED");
  }else if(RD && BD){
    Serial.print("RED & BLUE DETECTED");
  }else if(BD && GD){
    Serial.print("GREEN & BLUE DETECTED");
  }else if(RD && GD && BD){
    Serial.print("RED, GREEN, BLUE DETECTED");
  }
  delay(7000);
   }
MPardo3
  • 57
  • 7

1 Answers1

0

You can declare a function before you define it:

void setup();

Then you can call it:

setup();

...and somewhere later in the file you can write its definition:

void setup() { 
    // implementation here
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I fixed it with the declaration ^^.. NOw it sais it can;t compile.. can anyone find any mistakes? I've checked and rechecked everything including the {}..(check updated question code) – MPardo3 Apr 18 '17 at 13:01
  • @iMarioOfficial: The error message would be helpful. – SF. Apr 18 '17 at 14:04
  • @SF. thanks but all i needed to do was add void loop(){} – MPardo3 Apr 18 '17 at 17:25