Monday, December 3, 2012

LED code for Julia


// Project 38
#define sensorPin 9
int ledPin = 10;
float ledValue = 255; // The brightness of the LED
long pwmRange, inch;
void setup() {
  // Start serial communications
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
      pinMode(ledPin, OUTPUT); // Set the ledPin to an OUTPUT
  // Flash the LED twice to show the program has started
  digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);
  digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);
}
void loop() {
analogWrite(ledPin, int(ledValue) ); // Write brightness value to LED
pwmRange = pulseIn(sensorPin, HIGH);

  // 147uS per inch according to datasheet
  inch = pwmRange / 147;

  Serial.println(pwmRange);
  delay(175);
 
  if (ledValue <= 0) {
    ledValue = 0;
    }  // Make sure value does not go below zero
  ledValue = ((5*(inch))-25);
}

No comments:

Post a Comment