231a-aj hbCal

From CSclasswiki
Jump to: navigation, search
/*
* Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */
#include <DateTime.h>

//int samples[100];
int i = 0;
unsigned long sum = 0;
int avg = 0;
int t;
int maxP = 0;
int minP = 1025 ;
int printMax = 0;
int printMin = 0;
int brightPin = 13;
int LDR = 0;
int led1Pin = 11;
int led2Pin = 10;
int led3Pin = 9;
int led4Pin = 8;
int led5Pin = 7; 
int led6Pin = 6; 
//LDR input
int val = 0;                     //setup inital stored value

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);

  pinMode(LDR, INPUT);
  pinMode(brightPin, OUTPUT); 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);// sets the digital pin as output
  digitalWrite(brightPin, HIGH);
}

void loop4(){
  t = DateTime.now();
  if (i <10000){
    i++;
    val = analogRead(LDR);
    sum = sum+val; 
    avg = (sum)/i;

    if (val > maxP){
      maxP = val;
    }
    if (val < minP){
      minP = val;
    }}
    else if (i == 10000){
      printMax = (maxP-avg)/2;
      printMax = avg + printMax;
      printMin = (avg-minP)/2;
      printMin = avg - printMin;
      Serial.println(avg);
      Serial.println(printMax);
      Serial.println(printMin);
      Serial.println(" ");
      i++;
    }
    else {
      if (val >= printMax){
       Serial.print(t);
       Serial.print(" ");
       Serial.println(val);
      }
       if (val <= printMin){
       Serial.print(t);
       Serial.print(" ");
       Serial.println(val);
      }
      
   
    
  }
}
void loop(){
  t = DateTime.now();
  for (i= 1; i <10000; i++){
    val = analogRead(LDR);
    sum = sum+val; 
    avg = (sum)/i;

    if (val > maxP){
      maxP = val;
    }
    if (val < minP){
      minP = val;
    }

  }
  Serial.print("Time: ");
  Serial.println(t);
  Serial.print("Avg: ");
  Serial.println( avg);
  Serial.print("Max: ");
  Serial.println(maxP);
  Serial.print("Min :");
  Serial.println(minP);
  Serial.println(" ");

  sum = 0;
  minP = 1025;
  maxP = 0;
}

void loop3(){


  // int j;
  //val = analogRead(LDR);
  //Serial.println(DateTime.now());
  //samples[i++] = val;
  // if(i>= 100){
  // i = 0;
  // for (j = 0; j<100; j++){
  //  Serial.println(samples[j]);
  //delay(10);
  //} 
  //}
}

void loop2()                     // run over and over again
{

  val = analogRead(LDR);
  Serial.println(val);
  if (val == HIGH){
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
    digitalWrite(led5Pin, HIGH);
    digitalWrite(led6Pin, HIGH);
    digitalWrite(brightPin, HIGH);
  }   // sets the LED on                // waits for a second
  else{
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    digitalWrite(brightPin, LOW);  // sets the LED off                 // waits for a second
  }
}