The Big FourSchematics Programs, and Transducers

<-back to errata for Physical Computing.

(P92)

/* ... include debug code from previous chapter ...*/

#define INPUT_PIN 7
uint8_t x;
//declare a global variable called x
void setup {
   Serial.begin(9600); //for debug
   pinMode(INPUT_PIN_NO,INPUT);
}

void loop {
   x=digitalRead(INPUT_PIN);
   DEBUG("x = %d", x);
};

(p94)

#define INPUT1 2
#define INPUT1 4
#define INPUT1 6
#define ALL_SWITCHES_OFF 7
#define ANY_SWITCHES_ON 8
#define ON HIGH
#define OFF LOW

void setup() {
  pinMode(INPUT1,INPUT);
  pinMode(INPUT2,INPUT);
  pinMode(INPUT3,INPUT);
  pinMode(ALL_SWITCHES_OFF,OUTPUT);
  pinMode(ANY_SWITCHES_ON,OUTPUT);
}

void loop() {
  digitalWrite(ANY_SWITCHES_ON,OFF);
  if(digitalRead(INPUT1)&&digitalRead(INPUT1)&&digitalRead(INPUT1)) {
     digitalWrite(ALL_SWITCHES_OFF,ON);
     //digitalWrite(ANY_SWITCHES_ON,OFF);//already done above!
     delay(100);
     digitalWrite(ALL_SWITCHES_OFF,OFF);
     delay(100);
     digitalWrite(ALL_SWITCHES_OFF,ON);
     delay(100);
     digitalWrite(ALL_SWITCHES_OFF,OFF);
  } else {
     digitalWrite(ANY_SWITCHES_ON,ON);
     digitalWrite(ALL_SWITCHES_OFF,OFF);
  }
}

(p96)

void loop(){
   digitalWrite(25,0);
   digitalWrite(26,1);
   delay(200);
   digitalWrite(25,0);
   digitalWrite(26,1);
   delay(200);
}

(p102)

void loop(){
   digitalWrite(5,1);
   delay(300);
   digitalWrite(5,0);
   delay(300);
}

Analog-to-Digital Converters (p108)Photo on 2-5-13 at 5.48 PM #2

/*... setup and debug here ...*/

int ADCValue;
uint8_t OutputValue;

void loop() {
/* read the ADC on pin a0 */
ADCValue=analogRead(0);
DEBUG("ACDValue = %d", ADCValue);
}

Rctime (P110)

I think if we really wanted to engage this 80s era technique then there is the arduino documentation for RCTime.
http://arduino.cc/en/Tutorial/RCtime

/* RCtime
 *   Duplicates the functionality of the Basic Stamp's RCtime
 *   Allows digital pins to be used to read resistive analog sensors
 *   One advantage of this technique is that is can be used to read very wide ranging inputs.
 *   (The equivalent of 16 or 18 bit A/D)
 *  

 Schematic
                  +5V
                   |
                   |
                  ___
                  ___    Sensing Cap
                   |      .001 ufd  (change to suit for required resolution)
                   |      (102) pfd
                   |
sPin ---\/\/\/-----.
       220 - 1K    |
                   |
                   \   
                   /     Variable Resistive Sensor
                   \     Photocell, phototransistor, FSR etc.
                   /
                   |
                   |
                   |
                 _____ 
                  ___
                   _

*/

int sensorPin = 4;              // 220 or 1k resistor connected to this pin
long result = 0;
void setup()                    // run once, when the sketch starts
{
   Serial.begin(9600);
   Serial.println("start");      // a personal quirk 
}
void loop()                     // run over and over again
{

   Serial.println( RCtime(sensorPin) );
   delay(10);

}

long RCtime(int sensPin){
   long result = 0;
   pinMode(sensPin, OUTPUT);       // make pin OUTPUT
   digitalWrite(sensPin, HIGH);    // make pin HIGH to discharge capacitor - study the schematic
   delay(1);                       // wait a  ms to make sure cap is discharged

   pinMode(sensPin, INPUT);        // turn pin into an input and time till pin goes low
   digitalWrite(sensPin, LOW);     // turn pullups off - or it won't work
   while(digitalRead(sensPin)){    // wait for pin to go low
      result++;
   }

   return result;                   // report results   
}

Mostly at this point. If its important to actually emulate RCTime you should replace the function above with something more accurate and based on your current platform.

Pulsewidth Modulation for Input (P111)

FWIW the arduino also supports  pulseIn()

LED Dimming(P114)

The led dimming and motor speed control examples are included int David Mellis’s fading example code which is included with the Arduino. If you cut the bottom half of the loop where he walks the value back from bright to off. See Also http://arduino.cc/en/Tutorial/Fade

int ledPin = 13;    // LED connected to digital pin 9

void setup()  { 
  // nothing happens in setup 
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}

For pins which have the ~ next to them on the arduino this is handled quite nicely in hardware. For other pins this is done using timer interrupts.

Generating Tones (p117)

#define SPEAKER 9
const int note[] = {
262, // C
277, // C#
294, // D
311, // D#
330, // E
349, // F
370, // F#
392, // G
415, // G#
440, // A
466, // A#
494, // B
523  // C next octave
};

void setup() {
}
int thisNote;
void loop() {
   for(thisNote=0; thisNote<=9;thisNote++) {
      tone(SPEAKER,note[thisNote]);
     delay(1000);
     //noTone(SPEAKER);
  }
}

RC Servo Motors (p121)

#include <Servo.h> 

#define MIN_ANGLE 0
#define MAX_ANGLE 180
#define SERVO_PIN

Servo myservo;  // create servo object to control a servo 
int pos = MIN_ANGLE;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 
{ 
  for(pos = MIN_ANGLE; pos < MAX_ANGLE; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
 }

Scaling Functions (code on Page 130)

The arduino provides an extremely handy function called map which makes the example below fairly trivial.

#include <Servo.h>
#define SERVO_PIN 9
#define SENSOR_PIN 0
#define MAX_SENSOR_READING 130#define MIN_SENSOR_READING 5
#define MIN_ANGLE 0
#define MAX_ANGLE 180
Servo myservo;  // create servo object to control a servo 
int pos = MIN_ANGLE;    // variable to store the servo position 
int sensor = MIN_ANGLE;    // variable to store the sensor reading 

void setup() 
{ 
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 
{ sensor=analogRead(SENSOR_PIN);
  if (sensor<MIN_SENSOR_READING) sensor=MIN_SENSOR_READING;
  if (sensor>MAX_SENSOR_READING) sensor=MAX_SENSOR_READING;
  myservo.write(map(MIN_SENSOR_READING,MAX_SENSOR_READING,MIN_ANGLE,MAX_ANGLE);
  delay(20);                       // waits 15ms for the servo to reach the position 

}

Leave a Reply

  • (will not be published)