Schematics Programs, and Transducers

<-back to errata for Physical Computing.

MultiTasking (p191)

/*
 * The defines below allow you to normalize which way you build the circuit.
 * in general it is easier to pull and led current down than it is to source it.
 * also since the avr has built in pull ups you can do a normally open switch to
 * ground with less components. 
 *
 */
#define INPUT_PIN 7
#define LED_PIN 8
#define LED_ON LOW
#define LED_OFF HIGH
#define SWITCH_ON LOW
#define SWITCH_OFF HIGH

bool needFlashingFlag;
uint8_t timesFlashed;
bool ledState;
uint16_t counter =1;

void setup() {
   pinMode (INPUT_PIN, INPUT);
   digitalWrite(INPUT_PIN, HIGH); // enable pullup.
   pinMode (LED_PIN, OUTPUT);
   digitalWrite(LED_PIN, LED_OFF);
}

void loop(){
   if (digitalRead(INPUT_PIN)==SWITCH_ON) { 
       needFlashingFlag=true;
   }
   if (needFlashingFlag) {
     counter-=1;
     if (!counter){
       counter=1000;
       if (timesFlashed < 3) {
         if (ledState==LED_OFF) {
           timesFlashed += 1;
           ledState=LED_ON;
           digitalWrite(LED_PIN,ledState);
         } else {
           ledState=LED_OFF;
           digitalWrite(LED_PIN,ledState);
         }
       } else {
         timesFlashed=0;
         needFlashingFlag=false;
         counter=1;
         ledState=LED_OFF;
         digitalWrite(LED_PIN,ledState);
       }
     }
   }
}

Edge Detection (p195):

#define INPUT_PIN 7
#define SWITCH_ON LOW
#define SWITCH_OFF HIGH
#include "debug.h"

bool buttonState;
bool lastButtonState;
uint8_t buttonCount;

void setup(){
  pinMode(INPUT_PIN, INPUT);;
  digitalWrite(INPUT_PIN, HIGH);
}

void loop (){
  buttonState=digitalRead(INPUT_PIN);
  if (buttonState != lastButtonState) {
      if (buttonState == SWITCH_ON) {
        buttonCount++;
        DEBUG("Button is pressed");
      } else {
        DEBUG("Button is nos pressed");
        DEBUG("Button hits: %d", buttonCount);
      }
      lastButtonState=buttonState;
  }

}

Finding Peeks in an Analog Signal (P202)

Photo on 2-9-13 at 11.33 PM
#define ANALOG_PIN 0
#include "debug.h"

uint16_t peakValue=0;
uint16_t noise=7;
uint16_t sensorValue;
uint16_t lastSensorValue=0;
uint16_t threshold=300;

void setup(){
  INIT_DEBUG();
}

void loop (){
  sensorValue=analogRead(ANALOG_PIN);
  DEBUG("%d",sensorValue);
  if (sensorValue>=threshold+noise){
    if (sensorValue>=lastSensorValue+noise){
      peakValue=sensorValue;
    }
  } else {
    if(peakValue >= threshold){
      DEBUG("peak reading: %d", peakValue);
    }
    peakValue = 0;
  }
  lastSensorValue=sensorValue;

}

Button Debouncing (P205)

#define SWITCH_ON LOW
void setup () {
  pinMode(SWITCH_PIN, INPUT);
  digitalWrite(SWITCH_PIN, HIGH);
}

void loop(){
  if(digitalRead(SWITCH_PIN) == SWITCH_ON){
    delay(10);
    if(digitalRead(SWITCH_PIN) == SWITCH_ON) {
      switchOn=true;
    }else{
      switchOn=false;
    }
  }
}

Smoothing, Sampling, and Averaging(P207)

#define ANALOG_PIN 0
#define HISTORY_SIZE  4
#include "debug.h"

uint8_t positionInPastArray=HISTORY_SIZE;
uint16_t past[HISTORY_SIZE];
uint16_t sortedPast[HISTORY_SIZE];
uint16_t averageArray();
uint16_t medianArray();

void setup(){
  INIT_DEBUG();
  delay(500);
}

void loop (){
 uint16_t temp;
 uint16_t average;
 uint16_t median;
 temp=analogRead(ANALOG_PIN);
  if (++positionInPastArray >= HISTORY_SIZE) {
      positionInPastArray=0;
  }
  past[positionInPastArray]=temp;
  average=averageArray();
  median=medianArray();
  DEBUG("Average = %d Median = %d", average,median);
}

uint16_t averageArray() {
  long int total = 0;
  int i;
  for (i=0;i<=HISTORY_SIZE;i++){
    total+=past[i];
  }
  return (uint16_t)(total/HISTORY_SIZE);
}

uint16_t medianArray() {
  long int total = 0;
  int i,j;
  int finger=-1;
  for (i=0;i<=HISTORY_SIZE;i++){
    for (i=0;j<=HISTORY_SIZE;j++){
      if (past[i]>=past[j]){
        finger++;
      }
    }
    sortedPast[finger]=past[i];
  }
  for (i=0;i<=HISTORY_SIZE;i++){
    DEBUG("sortedPast[%d]=%d",i,sortedPast[i]);
  }
  return (uint16_t)sortedPast[HISTORY_SIZE/2];
}

Leave a Reply

  • (will not be published)