| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lab 4 - Bobby G

Page history last edited by Bobby Gonzales 13 years ago

Part A: Resistance Varying Sensors

 

Potentiometer

 

int sensorPin = A0;    // select the input pin for the potentiometer

int ledPin = 6;      // select the pin for the LED

int sensorValue = 0;  // variable to store the value coming from the sensor

 

void setup() {

  pinMode(ledPin, OUTPUT);   

  Serial.begin(2400);

}

 

void loop() {

  //divide sensor by 4 (0-255)

  Serial.println(sensorValue>>2, DEC);

  sensorValue = analogRead(sensorPin);  

  analogWrite(ledPin, sensorValue>>2);  

}

 

Serial Monitor

a. the potentiometer reads give values from 0 to 1023

b. this is 10 bit resolution log2(1024) = 10

 

Flex Sensor

a. resistance

     Flat: 9kΩ

     Bent: 23kΩ

b. voltages

     Flat: 5V * (22)/(9+22) = 3.5V

     Bent: 5V * (22)/(23+22) = 2.4V

c. sensor rading range

     We have to map the values we get so that they fit the 8bit PWM (analogWrite) value range.

 

d. code

int sensorPin = A7;    // select the input pin for the potentiometer

int ledPin = 6;      // select the pin for the LED

long sensorValue = 0;  // variable to store the value coming from the sensor

long sensorModValue = 0;

 

void setup() {

  pinMode(ledPin, OUTPUT);   

  Serial.begin(2400);

}

 

void loop() {

  sensorValue = analogRead(sensorPin);  

  sensorModValue = ((sensorValue-400)*255)/370;

  Serial.println(sensorModValue, DEC);

  analogWrite(ledPin, sensorModValue);  

}

 

Force Sensitive Resistor

a. resistances ???/

b. code

int sensorPinL = A6;    // select the input pin for the left pressure sensor

int sensorPinR = A7;    // select the input pin for the right pressure sensor

int ledPinL = 5;

int ledPinR = 6;

long sensorValueL = 0;

long sensorValueR = 0;

short diff = 0;

int zero = 5;

 

void setup() {

  pinMode(ledPinL, OUTPUT);

  pinMode(ledPinR, OUTPUT);   

  Serial.begin(2400);

}

 

void loop() {

  sensorValueL = analogRead(sensorPinL);

  sensorValueR = analogRead(sensorPinR);

 

  // max difference between readings is 1023. scale down to 127 add 127.

  if(sensorValueL > sensorValueR) {

    diff = ((sensorValueL - sensorValueR)/8) + 127;

    analogWrite(ledPinL, diff);

    analogWrite(ledPinR, zero);

  } else if(sensorValueL < sensorValueR) {

    diff = ((sensorValueR - sensorValueL)/8) + 127;

    analogWrite(ledPinL, zero);

    analogWrite(ledPinR, diff);

  } else {

    diff = 0;

    analogWrite(ledPinL, zero);

    analogWrite(ledPinR, zero);

  }

 

  ///////////////////////////////////////

  Serial.println(sensorValueL, DEC);

  Serial.println(sensorValueR, DEC);

  Serial.println(diff, DEC);

  Serial.println("----");

}

 

Data Logger

 

Arduino's are sick!!! I LOVE CODING ON THESE :D

...now that I've got that out of the way...

 

the atmega328 has a 512byte EEPROM

 

here's fun use of EEPROM. Storing a state variable. This is my VU Meter code, it works on any input, calibrates for 5 seconds, and then uses that range of values to light up a bar of 8 LEDs. There are 8 response curves to handle inputs that may need to be adjusted for sensitivity to non-linearity. State 1 is exponential (for sound, this means the quiet parts still light up a good amount of the bar). State 5 is linear and State 8 is logarithmic (the input has to be loud to light up the first half or so). The pot that can be seen in the video is used to choose the update rate of the display. For audio applications, a 1uF reservoir capacitor between ground and the input signal is used.

 

// Bobby Gonzales

// VU Meter Code

// REV 2011-04-28 02:01AM

////////////////////////////

 

#include <EEPROM.h>

 

const int sensorPin = A0;

const int potPin = A1;

int potread = 0;

 

int sensorValue = 0;         // the sensor value

int sensorMin = 1023;        // minimum sensor value

int sensorMax = 0;           // maximum sensor value

 

//sensor response curve (post-map)

 

int thresh[8][8] = {

  {5,7,12,22,38,62,104,176}, //exponential

  {6,11,22,39,61,89,129,189},

  {10,20,37,60,86,116,153,202},

  {17,35,58,84,112,142,175,213},

  {28,56,84,112,140,168,196,224}, //linear

  {42,82,115,144,170,194,215,234},

  {59,114,152,179,201,219,233,243},

  {80,152,194,218,234,244,249,251}, //log

};

 

int buttonPin = 12;

int curveChangeButtonPin = 11;

int buttonState = 0;

 

int curveChangeState = 0;             // the current reading from the input pin

int lastButtonState = LOW;   // the previous reading from the input pin

long lastDebounceTime = 0;

long debounceDelay = 20;

 

void setup() {

  pinMode(13, OUTPUT);

  digitalWrite(13, HIGH);

  while (millis() < 5000) {

    sensorValue = analogRead(sensorPin);

    if (sensorValue > sensorMax) sensorMax = sensorValue;

    if (sensorValue < sensorMin) sensorMin = sensorValue;

  }

  digitalWrite(13, LOW);

 

  for(int i=2; i<=9; i++)  pinMode(i, OUTPUT);

  pinMode(curveChangeButtonPin, INPUT);

  pinMode(buttonPin, INPUT);

  if(EEPROM.read(0) >=0 && EEPROM.read(0) <= 7) curveChangeState = EEPROM.read(0);

}

 

void loop() {

 

  potread = analogRead(potPin) >> 3;

  sensorValue = analogRead(sensorPin);

  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  sensorValue = constrain(sensorValue, 0, 255);

 

  int reading = digitalRead(curveChangeButtonPin);

  if (reading != lastButtonState) lastDebounceTime = millis(); 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if(curveChangeState < 7) curveChangeState ++;

    else curveChangeState = 0;

    EEPROM.write(0, curveChangeState);

    for(int i=2;i<=9;i++) digitalWrite(i, HIGH); //blank all leds

    delay(100);

 

    for(int i=2;i<=(curveChangeState+2);i++) digitalWrite(i, LOW); //display state graphically

    delay(200);

    for(int i=2;i<=9;i++) digitalWrite(i, HIGH); //blank all leds

    delay(100);

 

    for(int j=0;j<=7;j++) {

      digitalWrite(j+2, LOW);

      if(j > 0) delay(3*(thresh[curveChangeState][j]-thresh[curveChangeState][j-1]));

      else delay(3*thresh[curveChangeState][j]);

    }

    for(int i=2;i<=9;i++) digitalWrite(i, HIGH); //blank all leds

  }

 

  buttonState = digitalRead(buttonPin);

  drawLEDbar(sensorValue, !-buttonState, potread);

}

 

void drawLEDbar(int sensorValue, boolean drawModeDot, int del) {

  if(drawModeDot == false) {

    for(int j=8; j>=0; j--) {

      if ((sensorValue >= thresh[curveChangeState][j-1]) || j==0) {

        for (int i=2; i<j+2; i++) digitalWrite(i, LOW);

        for (int i=j+2; i<10; i++) digitalWrite(i, HIGH);

        delay(del);

        break;

      }

    }

  } else {

    for(int j=8; j>=0; j--) {

      if (sensorValue >= thresh[curveChangeState][j-1]) {

        for (int i=j+2; i<10; i++) digitalWrite(i, HIGH);

        digitalWrite(j+1, LOW);

        for (int i=2; i<j+1; i++) digitalWrite(i, HIGH);

        delay(del);

        break;

      }

    }

  }  

}

 

YouTube plugin error

Comments (0)

You don't have permission to comment on this page.