Saturday, September 27, 2014



Lab #3
 first research:

The Piezo or knock sensor is a little disk that detects sound over a certain threshold.
you connect to ground (black)and an analog pin with a resistor.. A Piezo is an element that can be used to produce or detect sound.. It can be used to perhaps open a door when a loud enough knock is sensed..Also the result can be printed to the serial port to be used in serial communication with processing.


Now for the sensor experiments.. I was able to get the light sensor to read in a variety of configurations.. The potentiometer I had (at home) kept falling off the breadboard ..it worked but was unstable so did not video it. The photocell was much better and got two to work.. just worked with fade and calibrating but understand smoothing and mapping..

here is some code and video..

nt buttonPinA = 8; //set buttonpin#
int buttonPinB = 9; //set buttonpin#
int buttonPinC = 10; //set buttonpin#
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


const int ledPin = 5; //set ledpin#
const int sensorPin = A0;    // pin that the sensor is attached to
byte leds = 0;

void setup() {
 
  pinMode(ledPin, OUTPUT); //led output
  pinMode(buttonPinA, INPUT_PULLUP); //button input
  pinMode(buttonPinB, INPUT_PULLUP); //button input
  pinMode(buttonPinC, INPUT_PULLUP); //button input
  //digitalWrite(buttonPin,HIGH);
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);

}
void loop() {
   // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);

  //buttonState = digitalRead(buttonPin); //read state of button

  if (digitalRead(buttonPinA) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
  // else{
  //turn led off
  if (digitalRead(buttonPinB) == LOW)
  {
    digitalWrite(ledPin, LOW);
  }

  if (digitalRead(buttonPinC   ) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
}

and another which I put in a little left over box project to finally finish it.. was able to connect 9V battery so no wires ma!

//Pins:
int sensorPin = 0;
int lightPin = 2;
int lightState = 0;
int lowThreshold = 8;
int highThreshold = 20;

void setup()  {
  //start serial & set pin to output

  Serial.begin(9600);
  pinMode(lightPin,OUTPUT);



}
void loop(){

  //read the sensor pin:
  int sensorValue = analogRead(sensorPin);

  //if light level low switch light on
  Serial.println(sensorValue);
  if(sensorValue < lowThreshold){

    digitalWrite(lightPin, HIGH); 

  }

  if (sensorValue > highThreshold)
    digitalWrite(lightPin, LOW);

}
I changed the calabration so would respond to my readings


here is little compiled video of experiments






No comments:

Post a Comment