Tuesday, November 30, 2010

code for slave arduinos

#include <Wire.h>
#define ADDRESS 65

// these are digital pins (leds)
int led[]={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

// these are analog pins (photoresistors and pir)
int photo[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int ambient = 15; // this is used for the comparison of each photoresistor
int light[15];

void setup() {      

  Serial.begin(9600);
  Wire.begin(ADDRESS);
  Wire.onReceive(receiveEvent);
 
  for(int i = 0; i< 15; i++){
    pinMode(led[i],OUTPUT);
  }

  for(int i = 0; i< 15; i++){
    pinMode(photo[i],INPUT);
  }

  pinMode(ambient, INPUT);
  
}


void loop() {
}

void receiveEvent(int val){

while (Wire.available() > 0){
val = Wire.receive();
int compare = analogRead(ambient);
Serial.println(val);

for(int i = 0; i< 15; i++){
 light[i] = analogRead(photo[i]);
}

if (val == 111){

  int delayTime = 250; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
 
  for(int i = 0; i < 15; i++){
    int offLED = i - 1;  //Calculate which LED was turned on last time through
    if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
      offLED = 15;        //turn on LED 2 and off LED 1)
    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                         //instead we turn off LED 7, (looping around)
    digitalWrite(led[i], HIGH);     //turn on LED #i
    digitalWrite(led[offLED], LOW); //turn off the LED we turned on last time
    delay(delayTime);
}
}
else if( val != 0){

// for the control of leds----------------------------------
for(int i = 0; i< 15; i++){
 
   if (light[i] > compare + 10){
      digitalWrite(led[i], HIGH);
   }
  
   else {
   digitalWrite(led[i], LOW);
   }
  
}
}

else if (val == 0){
  for (int i = 0; i< 15; i++){
    digitalWrite(led[i], LOW);
  }
}
}
}

No comments:

Post a Comment