Tuesday, February 26, 2013

Servos!

In this exercise we were learning how to control servos. We used one servo as is and another that had been hacked to disable it's internal potentiometer. We used a button to switch between them and an off state. Leds show which is enabled.




Here is the code used:


#include <Servo.h>

Servo constant;
Servo angle;

const int ledOff = 6;
const int ledServoCon = 10;
const int ledServoAngle = 3;
const int buttonPin = 2;
const int potPin = A0;
int potValue = 0;
int buttonState = 0;
int buttonPressed = 0;
int ledCount = 0;
int dir = 0;

void setup() {
 
  Serial.begin(9600);
  constant.attach(11);//pins 9 and 10 don't work with servos
  angle.attach(5);
  pinMode(ledOff,OUTPUT);
  pinMode(ledServoCon, OUTPUT);
  pinMode(ledServoAngle, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

void loop() {
 
   buttonPressed = digitalRead(buttonPin);
   buttonPressed = map(buttonPressed,0,1,1,0);//we want a 1 when it's pressed, so we used map to reverse the inputs
   if(buttonPressed==1){
     buttonState ++;//every time we push the button we increase the button state by 1
     delay(500);//make sure we don't push the button to many times by keeping our finger there too long
   }
   if(buttonState>=3){
     buttonState = 0;
   }
 
   if(buttonState==0){
     digitalWrite(ledServoCon,LOW);
     digitalWrite(ledServoAngle,LOW);
     constant.detach();
     angle.detach();
   
     if(ledCount<=0){
       dir = 2;//double time
     }
     else if(ledCount>=255){
       dir = -2;
     }
     ledCount += dir;
     analogWrite(ledOff,ledCount);//right now this variable will keep the led brightness next time this is activated.
     Serial.println(ledCount);
   }
   else if(buttonState==1){
     digitalWrite(ledOff, LOW);
     digitalWrite(ledServoCon, HIGH);
     digitalWrite(ledServoAngle, LOW);
     constant.attach(11);//because we detach this in our other loops we have to reattach it here.
     constant.write(1);
     angle.detach();
   
   }
   else if(buttonState==2){
     digitalWrite(ledOff, LOW);
     digitalWrite(ledServoCon, LOW);
     digitalWrite(ledServoAngle, HIGH);
     constant.detach();
     angle.attach(5);
   
     potValue = analogRead(potPin); //get value from potentiometer
     Serial.println(potValue);
 //    delay(500);
     potValue = map(potValue,0,1023,0,179);
     angle.write(potValue);
     potValue = map(potValue, 0,179,0,255);
     analogWrite(ledServoAngle, potValue);
     Serial.println(potValue);
   
   }
   
   Serial.print(buttonPressed);
   Serial.print("   ");
   Serial.println(buttonState);
 
   constant.write(179);
 
}

Friday, February 15, 2013

Potentiometer with Alternating LEDs

This was a difficult program, and I still didn't get it all the way...

This video explains what I was trying to do:


Above are two different versions of the code I was trying. In the COM3 window I was showing what happens after you do it a couple of times. Where there should be a zero, there is a two. Midway I reset it, and you can see it return to the state it should be. Here is the code from the one on the left:


const int potPin = A0;
const int led = 3;
const int led2 = 5;
int potValue = 0;
int newValue = 0;
int value2 = 0;

void setup() {
 
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(9600);
 
}

void loop() {
 
  potValue = analogRead(potPin);
  potValue = map(potValue,0,1023,0,1020);
 
  if(potValue <= 510){
    if(potValue < 255){
      newValue = map(potValue,0,255,0,255);
    }
    else{
    newValue = map(potValue,256,510,255,0);
    value2 = 0;
    }
  }
  else if(potValue >=511){
    if(potValue <= 765){
      value2 = map(potValue,511,765,0,255);
    }
    else{
      value2 = map(potValue,766,1020,255,0);
      newValue = 0;
    }
  }
 
  analogWrite(led, newValue);
  analogWrite(led2, value2);
 
  Serial.print(potValue);
  Serial.print("   ");
  Serial.print(newValue);
  Serial.print("   ");
  Serial.println(value2);
  delay(50);
}

Potentiometer Fun! *woot*

We learned how to take input from a sensor such as a light sensor and feed back a range of values. I used these values to control the brightness of an led. 
Here I used the same programming code and pretty much the same breadboard setup to rig a pressure sensor to a light. 

The great thing is that it doesn't even take a lot of code to run:
const int potPin = A0;
const int led = 3;
int potValue = 0;
int newValue = 0;

void setup() {
  
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  
}

void loop() {
  
  potValue = analogRead(potPin);
  
  newValue = map(potValue,0,1023,0,255);
  
  analogWrite(led, newValue);
  
  Serial.println(potValue); 
  
}

Wednesday, February 13, 2013

Twinkle Twinkle - Using Button Input

For this project we were learning how to take input from a button and write a program that uses this input to make something happen. I chose to allow a user to play the song "Twinkle Twinkle Little Star" by pressing a single button repeatedly. The breadboard setup is simple. I rigged 6 lights parallel through a single resistor and out to ground. The button has a constant charge being output to it which allows the pin to read when it has been grounded by the button being pressed.

The code is below. It's long, but because I did a long song, this was unavoidable. It is mostly repetition.


const int buttonPin = 2;
const int led1 = 3;
const int led2 = 4;
const int led3 = 5;
const int led4 = 6;
const int led5 = 7;
const int led6 = 8;
int musicMarker = 0;
int note = 0;
int buttonDelay = 50;
int lightDelay = 100;

void setup(){
 
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
 
}

void loop(){
 
  if(digitalRead(buttonPin)==0){
    musicMarker ++;
    delay(buttonDelay);
    }
  if(musicMarker == 1){
    note = 1;
  }else if(musicMarker == 2){
    note = 1;
  }else if(musicMarker == 3){
    note = 5;
  }else if(musicMarker == 4){
    note = 5;
  }else if(musicMarker == 5){
    note = 6;
  }else if(musicMarker == 6){
    note = 6;
  }else if(musicMarker == 7){
    note = 5;
  }else if(musicMarker == 8){
    note = 4;
  }else if(musicMarker == 9){
    note = 4;
  }else if(musicMarker == 10){
    note = 3;
  }else if(musicMarker == 11){
    note = 3;
  }else if(musicMarker == 12){
    note = 2;
  }else if(musicMarker == 13){
    note = 2;
  }else if(musicMarker == 14){
    note = 1;
  }else if(musicMarker == 15){
    note = 5;
  }else if(musicMarker == 16){
    note = 5;
  }else if(musicMarker == 17){
    note = 4;
  }else if(musicMarker == 18){
    note = 4;
  }else if(musicMarker == 19){
    note = 3;
  }else if(musicMarker == 20){
    note = 3;
  }else if(musicMarker == 21){
    note = 2;
  }else if(musicMarker == 22){
    note = 5;
  }else if(musicMarker == 23){
    note = 5;
  }else if(musicMarker == 24){
    note = 4;
  }else if(musicMarker == 25){
    note = 4;
  }else if(musicMarker == 26){
    note = 3;
  }else if(musicMarker == 27){
    note = 3;
  }else if(musicMarker == 28){
    note = 2;
  }else if(musicMarker == 29){
    note = 1;
  }else if(musicMarker == 30){
    note = 1;
  }else if(musicMarker == 31){
    note = 5;
  }else if(musicMarker == 32){
    note = 5;
  }else if(musicMarker == 33){
    note = 6;
  }else if(musicMarker == 34){
    note = 6;
  }else if(musicMarker == 35){
    note = 5;
  }else if(musicMarker == 36){
    note = 4;
  }else if(musicMarker == 37){
    note = 4;
  }else if(musicMarker == 38){
    note = 3;
  }else if(musicMarker == 39){
    note = 3;
  }else if(musicMarker == 40){
    note = 2;
  }else if(musicMarker == 41){
    note = 2;
  }else if(musicMarker == 42){
    note = 1;
  }else{
    note = 0;
    musicMarker = 0;
  }
   
  if(note == 1){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    delay(lightDelay);
    digitalWrite(led1, LOW);
  }else if(note == 2){
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    delay(lightDelay);
    digitalWrite(led2, LOW);
  }else if(note == 3){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    delay(lightDelay);
    digitalWrite(led3, LOW);
  }else if(note == 4){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    delay(lightDelay);
    digitalWrite(led4, LOW);
  }else if(note == 5){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, HIGH);
    digitalWrite(led6, LOW);
    delay(lightDelay);
    digitalWrite(led5, LOW);
  }else if(note == 6){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, HIGH);
    delay(lightDelay);
    digitalWrite(led6, LOW);
  }else{
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
  }
 
}

Monday, February 11, 2013

Pin Modes - Carol of the Bells

We learned how to use the pins to allow us to turn things on and off with programming instead of physical switches. I programmed some lights to blink to the pattern of the Carol of the Bells.

Here is the code I used. It's a bit long, so there's probably a more concise way to write it, but it works.


//melody notes:
int b = 4;
int c = 3;
int d = 2;
//bass notes:
int lb = 8;
int lc = 7;
int ld = 6;
int le = 5;

void setup() {
 
  Serial.begin(9600);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(lb, OUTPUT);
  pinMode(lc, OUTPUT);
  pinMode(ld, OUTPUT);
  pinMode(le, OUTPUT);
   
}

void loop() {
 
  for(int i = 0; i<5; i++){
  digitalWrite(d, HIGH);
  delay(400);
  digitalWrite(d, LOW);
  digitalWrite(c, HIGH);
  delay(200);
  digitalWrite(c,LOW);
  digitalWrite(d, HIGH);
  delay(200);
  digitalWrite(d,LOW);
  digitalWrite(b,HIGH);
  delay(400);
  digitalWrite(b,LOW);
  }
 digitalWrite(le,HIGH);
 digitalWrite(d, HIGH);
  delay(400);
  digitalWrite(d, LOW);
  digitalWrite(c, HIGH);
  delay(200);
  digitalWrite(c,LOW);
  digitalWrite(d, HIGH);
  delay(200);
  digitalWrite(d,LOW);
  digitalWrite(b,HIGH);
  delay(400);
  digitalWrite(b,LOW);
  digitalWrite(le,LOW);

 digitalWrite(ld,HIGH);
 digitalWrite(d, HIGH);
  delay(400);
  digitalWrite(d, LOW);
  digitalWrite(c, HIGH);
  delay(200);
  digitalWrite(c,LOW);
  digitalWrite(d, HIGH);
  delay(200);
  digitalWrite(d,LOW);
  digitalWrite(b,HIGH);
  delay(400);
  digitalWrite(b,LOW);
  digitalWrite(ld,LOW);
 
  digitalWrite(lc,HIGH);
  digitalWrite(d, HIGH);
  delay(400);
  digitalWrite(d, LOW);
  digitalWrite(c, HIGH);
  delay(200);
  digitalWrite(c,LOW);
  digitalWrite(d, HIGH);
  delay(200);
  digitalWrite(d,LOW);
  digitalWrite(b,HIGH);
  delay(400);
  digitalWrite(b,LOW);
  digitalWrite(lc,LOW);
 
  digitalWrite(lb,HIGH);
  digitalWrite(d, HIGH);
  delay(400);
  digitalWrite(d, LOW);
  digitalWrite(c, HIGH);
  delay(200);
  digitalWrite(c,LOW);
  digitalWrite(d, HIGH);
  delay(200);
  digitalWrite(d,LOW);
  digitalWrite(b,HIGH);
  delay(400);
  digitalWrite(b,LOW);
  digitalWrite(lb,LOW);

}

Thursday, February 7, 2013

Introduction to Arduino Programming


Here is a program which upon start-up displays "I'm working". Then after 1.5 seconds counts by fives to thirty at which point the counter is set to 0 and it starts again:

int data;

void setup() {
 
  Serial.begin(9600);
  Serial.println("I'm working");
  delay(1500);
  data = 0;
}

void loop() {

 data +=5;
 Serial.print("Count by fives  ");
 Serial.println(data);
 delay(500);

 if(data==30){
  data=0;
 }
}

Here is an example of it working:
 

Friday, February 1, 2013

Dimmer Switch


 Some introductions in electrical wiring: we rigged a dimmer switch.
See it in action.



Off

Turned a little
ON
with a button added
Turned on with both a button and switch!