Pages: [1]   Go Down
Author Topic: combine the auto and remote program  (Read 302 times)
0 Members and 1 Guest are viewing this topic.
malaysia
Offline Offline
Full Member
***
Karma: 1
Posts: 101
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yesterday i manage to do the auto detect the position of LDR (ty for people that help me out)

now i want to try to using a IR remote at a same time to control the position of the LDR,
when the power button on the remote is pressed, the processing stop the auto process,
and when the button on is press again it will continue the auto detect process...

here the code im try to do it, but failed..the auto need the pressing button on ir remote to operate. it not operate like before..
what wrong?
Code:
#include <IRremote.h>
#define ONREMOTE 2704 // decoded value when presing on button
#define NUMVALUES 4 //set array in 4

int c = 0;
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
int Position;
int position1;
int position2;
int position3;
int position4;

//====================position LDR=============
void setposition(){
  position1= digitalRead(9);
  position2=digitalRead(10);
  position3=digitalRead(11);
  position4= digitalRead(12);
 
  if (position1==1){
   if (position2 ==0){
       if (position3 ==0){
         if (position4 ==0){
              Position = 1;           //position on LDR 1
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
 if (position1==0){
   if (position2 ==1){
       if (position3 ==0){
         if (position4 ==0){
        Position = 2;              //position on LDR 2
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
   
  }
  if (position1==0){
    if(position2 ==0){
       if (position3 ==1){
         if (position4 ==0){
            Position = 3;            //position on LDR 3
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
   if (position1==0){
    if(position2 ==0){
       if (position3 ==0){
         if (position4 ==1){
            Position = 4;            //position on LDR 4
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
     
     
   if (position1 ==1){
       if (position2 ==1){
         if (position3 ==1){
          if (position4==1){
             Position = 0;     //no light detected
    Serial.print("Position");
    Serial.println("No light detected");
          }
         }
       }
   }


   }



void rem(){ //read the remote value
if (irrecv.decode(&results))
    c= results.value;
 else
 c= 0;
}
   
void setup()
{
  Serial.begin(9600);
  Serial.println("Begin");
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  rem(); //get remote reading
  if (c == 0){
     digitalWrite(13,LOW);
 
     int values [NUMVALUES];
       int maxVal = -1;
 
  // READ THE VALUES
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
   
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal);
 setposition(); //set the position LDR
 delay(1000);
 
 if (c != 0){   //using remote to control position lDr
   if (c == ONREMOTE){
     Serial.println("Remote ON");
     //find the next press button to set the position of LDR
     digitalWrite(13,HIGH);
    irrecv.resume(); // Receive the next value
  }
  }
  }
}


Logged

dut eden rockes

Pages: [1]   Go Up
Jump to: