Weeks 4-6: Piezo Buzzer and Servo Motor w/ Sensors

Octi
Reactive Octi
Based on the pressure or flex applied to the embedded sensors in Octi’s arms, his eyes rotate to make him look either sad or mad.
#include <Servo.h>
Servo myservo;
int servo = 7;
int piezo = 8;
int pressure = A0;
int servo_pos = 0;
int current_pressure = 0;
int current_note = 200;
int current_spin = 1;
void setup() {
  // put your setup code here, to run once:
  pinMode(servo, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(pressure, INPUT);
  myservo.attach(servo);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  current_pressure = analogRead(pressure);
  Serial.print(current_pressure);
  Serial.print(” “);
  current_note = map(current_pressure, 0, 1024, 200, 2000);
  Serial.print(current_note);
  Serial.print(” “);
  current_spin = map(current_pressure, 0, 1024, 1, 20);
  Serial.println(current_spin);
  tone(piezo, current_note);
  myservo.write(servo_pos);
  servo_pos += 1*current_spin;
}

Making Things Move Assignment

Next class you will be presenting your moving projects. You are welcome to use any of the motors that I presented to you in class. Remember that if you need to switch the rotation of a motor you can use an H-bridge. These ICs are also useful for controlling Stepper Motors.  We have some of these chips in stock in the components cabinet of the lab.

Servo

If you are using the 180 micro servo motors that I handed out in class, please refer to the Servo Library here: http://arduino.cc/en/reference/servo

Here is a very simple example tgat uses 2 for loops to sweep the servo back and forth from 0-180 degrees and viceversa: http://arduino.cc/en/Tutorial/Sweep

Stepper

Eric Hoffman has a great example for connecting the H-bridge to the Stepper to be controlled by the Arduino here: http://teaching.ericforman.com/stepper-motor/

H-bridge

Here is the data sheet of the sn754410 H-bridge IC for your reference: http://www.ti.com/lit/ds/symlink/sn754410.pdf

Here is a breadboarded tutorial with a fritzing example of how to combine the H-Bridge with your Arduino micro-controller: http://quarkstream.wordpress.com/2010/01/07/arduino-8-the-h-bridge/

 

LOL SHIELDS FOR NEXT WEEK

Make sure that you purchase your LOL shields for next week. I recommend that you use Adafruit, because they are local, as well as have a huge variety of colors: http://www.adafruit.com/products/274

 

hw3 :: delirious home :: step 1 :: motion sensor

Dali Atomicus, photo by Philippe Halsman (1948)

If our  everyday artifacts and objects were to become capable of sensing and perceiving what they are detecting, so how can we make sure the response is shaping the networks of our environments the way we want?

So I am imagining a delirious home where the internet of things is out of control… something like this :

Prototype :

As a starting point I am working with a motion sensor that I will place at the entrance door of the apartment to detect people, pets… passing by my door… the sensor will send data to the internet when motion is detected. I  will work with the data and affect another object in the home. I am using the Arduino Yun to connect to the internet and send a data stream on sparkfun.

The motion sensor is now working and lighting up pin13 on the Yun.

However, the data doesn’t seem to want to go online. So I’m still working on that.

Here’s the code

Continue reading

homework3 : distance

I use a range sensor to calculate the distance like a bat through the theory of per centimeter need 29 milliseconds for sound .

 

 

 

 

 

 

const int pingPin = 7;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

Serial.begin(9600);

lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);

}

void loop()
{

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
int distance = pulseIn(pingPin, HIGH);
distance= distance/58;
delay(200);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Distance is”);
lcd.setCursor(0,1);
lcd.print(distance);
lcd.print(“cm”);
}

The Talking Tissue Box

So far it’s not working quite the way I want it to yet. Had problems getting Processing to read my Serial messages properly when I coded in Arduino, so in frustration, I used Firmata and controlled Arduino through Processing.

Except my talking box still isn’t working the way I want to yet. I think I bent the sensor too many times…. Seemed to work a bit better after I switched sensor.

Firmata is included with Arduino’s examples, so I’m not going to include it here. Here’s the Processing code that is still buggy:

Continue reading

hw week 5

I created a led lit cigarette box that is controlled by a tilt sensor which I attached to the top of the box which turns the led light on when the top of the box is tilted open. The sensor was a bit buggy and led flickered on and off quite a bit.

int SensorPin = 2;
int LEDPin = 3;

int LEDstate = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 50;

void setup()
{
pinMode(SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
pinMode(LEDPin, OUTPUT);
}

void loop()
{
int switchstate;

reading = digitalRead(SensorPin);

if (reading != previous) {

time = millis();
}

if ((millis() – time) > debounce) {

switchstate = reading;

if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(LEDPin, LEDstate);

previous = reading;
}

hw5.2 hw5

 

 

homework for week 4: Little Piano

This is a little piano with 3 keys — red stands for C, green stands for E, and blue stands for G. Control the knock strength to change the key.

2014-09-26 08.45.04-1 拷貝

 

HW2_piano_bb

int sensorPin = 0;
int ledPinC = 2;
int ledPinE = 3;
int ledPinG = 4;
int threshold = 100;
int keyboard;

int speaker = 12;
int sensorValue = 0;

void setup() {
Serial.begin(9600);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinE, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(speaker, OUTPUT);
}

void loop() {
int val = analogRead(sensorPin);

if (val >= threshold) {
sensorValue = map(val, 180, 400, 100, 500);

if(sensorValue >= 100 && sensorValue < 200) {
keyboard = 131;
tone(speaker, keyboard);
digitalWrite(ledPinC, HIGH);
delay(100);
digitalWrite(ledPinC, LOW);
noTone(speaker);
}
else if(sensorValue >= 200 && sensorValue < 300) {
keyboard = 330;
tone(speaker, keyboard);
digitalWrite(ledPinE, HIGH);
delay(100);
digitalWrite(ledPinE, LOW);
noTone(speaker);
}
else if(sensorValue >= 300 && sensorValue < 400) {
keyboard = 784;
tone(speaker, keyboard);
digitalWrite(ledPinG, HIGH);
delay(100);
digitalWrite(ledPinG, LOW);
noTone(speaker);
}
Serial.println(keyboard);
}
}

Synthesizer

 

I made a a granular synthesizer. I combined a few tutorials I found online for different granular synths using code called “auduino” by peter knight.

http://www.instructables.com/id/The-Arduino-Synthesizer/

http://www.instructables.com/id/How-to-build-an-Arduino-synth/

https://code.google.com/p/tinkerit/wiki/Auduino

 

the synth has pots for pitch 1 and 2 and pots for decay 1 and decay 2. the 5th pot is  for repetition frequency. with the click of a button, the synth can use a pressure sensor in place of pot 5 to control the repetition frequency.

there is also a button that cycles through 6 different modes. basically changing the scale (or sometimes no scale) that is heard when the repetition frequency changes.

 

here is the code I used-

// Auduino, the Lo-Fi granular synthesiser
//
// by Peter Knight, Tinker.it http://tinker.it
//
// Help: http://code.google.com/p/tinkerit/wiki/Auduino
// More help: http://groups.google.com/group/auduino
//
// Analog in 0: Grain 1 pitch
// Analog in 1: Grain 2 decay
// Analog in 2: Grain 1 decay
// Analog in 3: Grain 2 pitch
// Analog in 4: Grain repetition frequency
//
// Digital 3: Audio out (Digital 11 on ATmega8)
//
// Changelog:
// 19 Nov 2008: Added support for ATmega8 boards
// 21 Mar 2009: Added support for ATmega328 boards
// 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
// 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
// 11 Mar 2012: edit code to fit corresponding Fritzing diagram.

/*
10/07/10 Prodical contributed:
– additional mapping modes – diatonic major and minor and pentatonic major and minor
– switchable between modes by a single button which cycles through each and differentiates by an LED blinking as per the mode number
– a light dependent resistor (LDR) – calibrated in the first five seconds after switching on – replacing the main (grain repetition) frequency pot on a switch (using an external interrupt) with an LED indicator when it’s active but which also dims according on the LDR value
more details at http://blog.lewissykes.info
*/

/*
Calibration

Demonstrates one techinque for calibrating sensor input. The
sensor readings during the first five seconds of the sketch
execution define the minimum and maximum of expected values
attached to the sensor pin.

The sensor minumum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen for anything
lower, saving it as the new minumum. Likewise, you set the
maximum low and listen for anything higher as the new maximum.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
* LED attached from digital pin 9 to ground

created 29 Oct 2008
By David A Mellis
Modified 17 Jun 2009
By Tom Igoe

http://arduino.cc/en/Tutorial/Calibration

This example code is in the public domain.

*/

// AUDUINO code STARTS
#include <avr/io.h>
#include <avr/interrupt.h>

uint16_t syncPhaseAcc;
uint16_t syncPhaseInc;
uint16_t grainPhaseAcc;
uint16_t grainPhaseInc;
uint16_t grainAmp;
uint8_t grainDecay;
uint16_t grain2PhaseAcc;
uint16_t grain2PhaseInc;
uint16_t grain2Amp;
uint8_t grain2Decay;

// Map Analogue channels
#define SYNC_CONTROL (4)
#define GRAIN_FREQ_CONTROL (3)
#define GRAIN_DECAY_CONTROL (2)
#define GRAIN2_FREQ_CONTROL (1)
#define GRAIN2_DECAY_CONTROL (0)

// Changing these will also requires rewriting audioOn()
#if defined(__AVR_ATmega8__)
//
// On old ATmega8 boards.
// Output is on pin 11
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_PIN 11
#define PWM_VALUE OCR2
#define PWM_INTERRUPT TIMER2_OVF_vect
#elif defined(__AVR_ATmega1280__)
//
// On the Arduino Mega
// Output is on pin 3
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 7
#define PWM_PIN 3 //3
#define PWM_VALUE OCR3C
#define PWM_INTERRUPT TIMER3_OVF_vect
#else
//
// For modern ATmega168 and ATmega328 boards
// Output is on pin 3
//
#define PWM_PIN 3 //3
#define PWM_VALUE OCR2B
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_INTERRUPT TIMER2_OVF_vect
#endif
// AUDUINO code ENDS
// BUTTON, SWITCH, LDR & LEDs – START
// Button
#define BUTTON_PIN (6) // the number of the pushbutton pin
int buttonValue; // variable for reading the button status
int buttonState; // variable to hold the button state
int mapMode = 0; // What scale/mapping mode is in use?

// LDR
//#define LDRSWITCH (4)
// switch replaced by external interrupt
volatile int LDRswitchState = LOW;
#define LDR_PIN (5)
#define LDRLED_PIN (9)
//Callibration variables
int LDRValue = 0; // the sensor value
int LDRMin = 1023; // minimum sensor value
int LDRMax = 0; // maximum sensor value

// PWM_VALUE LED
#define FRQLED_PIN (11) // not as consistent as pin 13

// mapmode LED
#define mapModeLED_PIN (10) // the number of the LED pin
int mapModeLEDState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
int BlinkRate = 5; // no of blinks per second i.e. fps – empirically tested as just slow enough to count
int BlinkCount = 0; // variable to store no of blinks
int BlinkLoopLength = 14; // err… blink loop length

// BUTTON, SWITCH, LDR & LEDs – ENDS
// MAPPINGS – START
// Smooth logarithmic mapping
//
uint16_t antilogTable[] = {
64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
};
uint16_t mapPhaseInc(uint16_t input) {
return (antilogTable[input & 0x3f]) >> (input >> 6);
}

// Stepped chromatic mapping
//
uint16_t midiTable[] = {
0,17,18,19,20,22,23,24,26,27,29,31,32,34,36,38,41,43,46,48,51,54,58,61,65,69,73,
77,82,86,92,97,103,109,115,122,129,137,145,154,163,173,183,194,206,218,231,
244,259,274,291,308,326,346,366,388,411,435,461,489,518,549,581,616,652,691,
732,776,822,871,923,978,1036,1097,1163,1232,1305,1383,1465,1552,1644,1742,
1845,1955,2071,2195,2325,2463,2610,2765,2930,3104,3288,3484,3691,3910,4143,
4389,4650,4927,5220,5530,5859,6207,6577,6968,7382,7821,8286,8779,9301,9854,
10440,11060,11718,12415,13153,13935,14764,15642,16572,17557,18601,19708,20879,
22121,23436,24830,26306,27871
};
uint16_t mapMidi(uint16_t input) {
return (midiTable[(1023-input) >> 3]);
}

//// Stepped Pentatonic mapping
//
uint16_t pentatonicTable[54] = {
0,19,22,26,29,32,38,43,51,58,65,77,86,103,115,129,154,173,206,231,259,308,346,
411,461,518,616,691,822,923,1036,1232,1383,1644,1845,2071,2463,2765,3288,
3691,4143,4927,5530,6577,7382,8286,9854,11060,13153,14764,16572,19708,22121,26306
};

uint16_t mapPentatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (pentatonicTable[value]);
}

// Lewis added – I’ve got an Excel spreadsheet with these workings out on my blog…
// Stepped major Diatonic mapping
//
uint16_t majordiatonicTable[76] = {
0,17,19,22,23,26,29,32,34,38,43,46,51,58,65,69,77,86,92,103,115,129,137,154,173,183,206,231,259,274,308,346,366,
411,461,518,549,616,691,732,822,923,1036,1097,1232,1383,1465,1644,1845,2071,2195,2463,2765,2930,3288,
3691,4143,4389,4927,5530,5859,6577,7382,8286,8779,9854,11060,11718,13153,14764,16572,17557,19708,22121,23436,26306
};

uint16_t mapmajorDiatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (majordiatonicTable[value]);
}

// Stepped minor Diatonic mapping
//
uint16_t minordiatonicTable[76] = {
0,17,19,20,23,26,27,31,34,38,41,46,51,54,61,69,77,82,92,103,109,122,137,154,163,183,206,218,244,274,308,326,366,
411,435,489,549,616,652,732,822,871,978,1097,1232,1305,1465,1644,1742,1955,2195,2463,2610,2930,3288,
3484,3910,4389,4927,5220,5859,6577,6968,7821,8779,9854,10440,11718,13153,13935,15642,17557,19708,20879,23436,26306
};

uint16_t mapminorDiatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (minordiatonicTable[value]);
}

// Stepped major Pentatonic mapping
//
uint16_t majorpentatonicTable[55] = {
0,17,19,22,26,29,34,38,43,51,58,69,77,86,103,115,137,154,173,206,231,274,308,346,
411,461,549,616,691,822,923,1097,1232,1383,1644,1845,2195,2463,2765,3288,
3691,4389,4927,5530,6577,7382,8779,9854,11060,13153,14764,17557,19708,22121,26306
};

uint16_t mapmajorPentatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (majorpentatonicTable[value]);
}

// Stepped minor Pentatonic mapping
//
uint16_t minorpentatonicTable[55] = {
0,17,20,23,26,31,34,41,46,51,61,69,82,92,103,122,137,163,183,206,244,274,326,366,
411,489,549,652,732,822,978,1097,1305,1465,1644,1955,2195,2610,2930,3288,
3910,4389,5220,5859,6577,7821,8779,10440,11718,13153,15642,17557,20879,23436,26306
};

uint16_t mapminorPentatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (pentatonicTable[value]);
}
// MAPPINGS – END
void audioOn() {
#if defined(__AVR_ATmega8__)
// ATmega8 has different registers
TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
TCCR3A = _BV(COM3C1) | _BV(WGM30);
TCCR3B = _BV(CS30);
TIMSK3 = _BV(TOIE3);
#else
// Set up PWM to 31.25kHz, phase accurate
TCCR2A = _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
TIMSK2 = _BV(TOIE2);
#endif
}
void setup() {

pinMode(PWM_PIN,OUTPUT);
audioOn();
pinMode(LDRLED_PIN,OUTPUT);
pinMode(FRQLED_PIN,OUTPUT);
pinMode(mapModeLED_PIN,OUTPUT);
pinMode(BUTTON_PIN,INPUT);
attachInterrupt(0, LDRswitched, CHANGE);
}

// // Calibration
//Serial.begin(9600);
// turn on LED to signal the start of the calibration period:
// pinMode(9, OUTPUT);
// digitalWrite(9, HIGH);
// // calibrate during the first five seconds
// while (millis() < 5000) {
// LDRValue = analogRead(LDR_PIN);
// // record the maximum sensor value
// if (LDRValue > LDRMax) {
// LDRMax = LDRValue;
// }
// // record the minimum sensor value
// if (LDRValue < LDRMin) {
// LDRMin = LDRValue;
// }
// }
// // signal the end of the calibration period
// digitalWrite(9, LOW);
//// // END calibration code
////
////}
void LDRswitched(){
LDRswitchState = !LDRswitchState;
}
void loop() {
// The loop is pretty simple – it just updates the parameters for the oscillators.
//
// Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
// They will cause clicks and poops in the audio.

// Smooth frequency mapping
//syncPhaseInc = mapPhaseInc(analogRead(SYNC_CONTROL)) / 4;

// Stepped mapping to MIDI notes: C, Db, D, Eb, E, F…
//syncPhaseInc = mapMidi(analogRead(SYNC_CONTROL));

// // Stepped pentatonic mapping: D, E, G, A, B
// syncPhaseInc = mapPentatonic(analogRead(SYNC_CONTROL));

// could add switch here to choose between midiIn() and the twisty-pot pitch control

//BEGIN Calibration
// read the sensor:
LDRValue = analogRead(LDR_PIN);
// apply the calibration to the sensor reading
LDRValue = map(LDRValue, LDRMin, LDRMax, 0, 1023);
// in case the sensor value is outside the range seen during calibration
LDRValue = constrain(LDRValue, 0, 1023);
// // fade the LED using the calibrated value: (this moved below)
// analogWrite(LDRLED_PIN, LDRValue);
// Serial.println(LDRValue);
//END Calibration
LDRValue=analogRead(LDR_PIN);
//
if (LDRValue==HIGH){
LDRswitched(); }
//

// button presses cycle through scales/mapping modes
buttonValue = digitalRead(BUTTON_PIN); // read input value and store it in val
if (buttonValue != buttonState) { // the button state has changed!
if (buttonValue == 0) { // check if the button is pressed
if (mapMode == 0) { // if set to smooth logarithmic mapping
mapMode = 1; // switch to stepped chromatic mapping
}
else {
if (mapMode == 1) { // if stepped chromatic mapping
mapMode = 2; // switch to stepped major Diatonic mapping
}
else {
if (mapMode == 2) { // if stepped major Diatonic mapping
mapMode = 3; // switch to stepped minor Diatonic mapping
}
else {
if (mapMode == 3) { // if stepped minor Diatonic mapping
mapMode = 4; // switch to stepped major Pentatonic mapping
}
else {
if (mapMode == 4) { // if stepped major Pentatonic mapping
mapMode = 5; // switch to stepped minor Pentatonic mapping
}
else {
if (mapMode == 5) { // if stepped major Pentatonic mapping
mapMode = 0; // switch back to smooth logarithmic mapping
}
}
}
}
}
}
}
buttonState = buttonValue; // save the new state in our variable
}

// mapMode LED indicator – blinks number of scale/mapping mode on a fixed ‘frame’ cycle
if (millis() – previousMillis > 1000/BlinkRate)
{
BlinkCount = BlinkCount + 1;
// save the last time you blinked the LED
previousMillis = millis();
digitalWrite(mapModeLED_PIN, LOW);
if (BlinkCount <= (mapMode+1)*2)
{
//if the LED is off turn it on and vice-versa:
if (mapModeLEDState == LOW)
{
mapModeLEDState = HIGH;
}
else{
mapModeLEDState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(mapModeLED_PIN, mapModeLEDState);
}
// resets Blink loop after 14 blinks
else if (BlinkCount >= BlinkLoopLength)
{
BlinkCount = 0;
}
//debug
//Serial.println(BlinkCount);
}

//Map modes to select with button. LED blinks as per mode currently in use
//selects which array to pull data from to get SyncPhaseInc
//1. Smooth logarithmic mapping
if (mapMode == 0) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapPhaseInc(LDRValue) / 4;
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapPhaseInc(analogRead(SYNC_CONTROL)) / 4;
digitalWrite(LDRLED_PIN, LOW);
}
}
//2. Stepped chromatic mapping to MIDI notes: C,C#,D,Eb,F,F#,G,Ab,A,Bb,B
if (mapMode == 1) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapMidi(1023-LDRValue);
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapMidi(analogRead(SYNC_CONTROL));
digitalWrite(LDRLED_PIN, LOW);
}
}
//3. Stepped major Diatonic mapping: C,D,E,F,G,A,B
if (mapMode == 2) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapmajorDiatonic(LDRValue);
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapmajorDiatonic(analogRead(SYNC_CONTROL));
digitalWrite(LDRLED_PIN, LOW);
}
}
//4. Stepped minor Diatonic mapping: C,D,Eb,F,G,Ab,Bb
if (mapMode == 3) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapminorDiatonic(LDRValue);
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapminorDiatonic(analogRead(SYNC_CONTROL));
digitalWrite(LDRLED_PIN, LOW);
}
}
//5. Stepped major Pentatonic mapping
if (mapMode == 4) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapmajorPentatonic(LDRValue);
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapmajorPentatonic(analogRead(SYNC_CONTROL));
digitalWrite(LDRLED_PIN, LOW);
}
}
//6. Stepped major Diatonic mapping
if (mapMode == 5) {
if (LDRswitchState == HIGH) {
syncPhaseInc = mapminorPentatonic(LDRValue);
analogWrite(LDRLED_PIN, LDRValue);
}
else
{
syncPhaseInc = mapminorPentatonic(analogRead(SYNC_CONTROL));
digitalWrite(LDRLED_PIN, LOW);
}
}

//input from pots
grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;

digitalWrite(FRQLED_PIN, PWM_VALUE);

}
SIGNAL(PWM_INTERRUPT)
{
uint8_t value;
uint16_t output;

syncPhaseAcc += syncPhaseInc;
if (syncPhaseAcc < syncPhaseInc) {
// Time to start the next grain
grainPhaseAcc = 0;
grainAmp = 0x7fff;
grain2PhaseAcc = 0;
grain2Amp = 0x7fff;
LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
}

// Increment the phase of the grain oscillators
grainPhaseAcc += grainPhaseInc;
grain2PhaseAcc += grain2PhaseInc;

// Convert phase into a triangle wave
value = (grainPhaseAcc >> 7) & 0xff;
if (grainPhaseAcc & 0x8000) value = ~value;
// Multiply by current grain amplitude to get sample
output = value * (grainAmp >> 8);

// Repeat for second grain
value = (grain2PhaseAcc >> 7) & 0xff;
if (grain2PhaseAcc & 0x8000) value = ~value;
output += value * (grain2Amp >> 8);

// Make the grain amplitudes decay by a factor every sample (exponential decay)
grainAmp -= (grainAmp >> 8) * grainDecay;
grain2Amp -= (grain2Amp >> 8) * grain2Decay;

// Scale output to the available range, clipping if necessary
output >>= 9;
if (output > 255) output = 255;

// Output to PWM (this is faster than using analogWrite)
PWM_VALUE = output;
}

 

LUOBIN_HOMEWORK

 

This device is using Arduino, a photocell, an accelerometer, and a LCD. The goal is to help people to take more stable shots of photo/video by notification. In a darker environment, the helper will tell user to hold steady until it’s stable enough to take a clear photo. The helper will automatically turns off when there’s enough light.

The photocell senses the lightness of the environment. A line code is checking the value, if the value is low enough, it will check the realtime reading from accelerometer and give out suggestions.

Test without enclosure…………………………………

breadboard…………………………………..

wire_bbschematic…………………………………………….

 

 

code………………………………………………….

/******************************************************************************
*
* peterobbin
*
* Courtesy To:
* SFE_MMA8452Q Library Basic Example Sketch
* Jim Lindblom @ SparkFun Electronics
* Original Creation Date: June 3, 2014
* https://github.com/sparkfun/MMA8452_Accelerometer
*
* This sketch uses the SFE_MMA8452Q library to initialize the
* accelerometer, and stream values from it.
******************************************************************************/
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library

// Begin using the library by creating an instance of the MMA8452Q
MMA8452Q accel;

float lastasValue;
float asValue;
float asThreshold = 0.015;
float lightThreshold = 220;
int photocellPin = A0;
int pcValue;
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// The setup function simply starts serial and initializes the
// accelerometer.
void setup()
{
Serial.begin(9600);
Serial.println(“MMA8452Q Test Code!”);

accel.init();

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print(“HOLD STEADY”);
}
void loop()
{

pcValue = analogRead(photocellPin);
Serial.println(pcValue);

// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();

// accel.read() will update two sets of variables.
// * int’s x, y, and z will store the signed 12-bit values
// read out of the accelerometer.
// * floats cx, cy, and cz will store the calculated
// acceleration from those 12-bit values. These variables
// are in units of g’s.
if (pcValue < lightThreshold){
printCalculatedAccels();
lcd.setCursor(13, 1);
lcd.print(” ON”);
}
else{
lcd.setCursor(0, 0);
lcd.print(“STABILIZE HELPER”);
lcd.setCursor(13, 1);
lcd.print(“OFF”);
}

printOrientation();
Serial.println();
}
}

void printCalculatedAccels()
{
lastasValue = asValue;
asValue = accel.cz;
float a = asValue – lastasValue;
float compareValue = abs(a);
Serial.print(compareValue, 3);
Serial.print(“\t”);
Serial.print(accel.cx, 3);
Serial.print(“\t”);
Serial.print(accel.cy, 3);
Serial.print(“\t”);
Serial.print(accel.cz, 3);
Serial.print(“\t”);
if(compareValue > asThreshold){
lcd.setCursor(0, 0);
lcd.print(“HOLD STEADY “);
}
else{
lcd.setCursor(0, 0);
lcd.print(“OKAY TO SHOOT “);
}

lcd.setCursor(0, 1);
lcd.print(compareValue);
}

 

 

week 4 – No, he doesn’t want to be touched

Hi everyone.

for this homework I made a toy that doesn’t want to be touched. This is the early stage of a more complex idea where a toy can give you remote information from your house.

Since to do that require more time and knowledge, for now I connected a Sharp IR sensor to detect proximity connected with an LED and a speaker. at certain distance the puppet start complaining.

 

I found my father playing with him this morning in the kitchen.

10626532_10203603875071056_2931513896486994226_n(1)

 

foto 4

foto 1

// Uses a PIR sensor to detect movement, a speaker and an LED

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}

void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(0);
if (pirState == LOW) {
// we have just turned on
Serial.println(“Motion detected!”);
// We only want to print on the output change, not state
pirState = HIGH;

Serial.println(val);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println(“Motion ended!”);
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}