Saturday, December 17, 2011

Lab Expansion - Circuit 3

Purpose: To take elements from circuit 7 to modify and enhance circuit 3


Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC -03 Breadboard Sheet x 1
  • CIRC -07 Breadboard Sheet x 1
  • Pushbutton x 2
  • Toy Motor x 1
  • 10k ohm resistor x 3
  • Diode x 1
  • Transistor x 1
  • 3 pin header x 1
  • Wire x 10

References:
http://milindarduino.blogspot.com/2011/10/circuit-03-spin-motor-spin.html
http://milindarduino.blogspot.com/2011/10/circuit-07-push-buttons.html

Program Details:
The circuit is setup very differently than circuit 3. Two pushbuttons are added for part 1 and four led's for part 2. The pushbuttons are used to control the speed of the motor. In part 1, one button is used to increase the speed of the motor and the other button is used to decrease the speed of the motor. No real new programming concepts are introduced in this program except for a new data type, boolean. A boolean has only two values, true and false. A boolean is used in this line of code boolean value = buttonCheck();. So this means that the return type of the buttonCheck method must also be a boolean for this to work and that the value is either true or false. This is best used for when there are only two options as utilized later in the program.

Part 2 is very similar to Part 1 except for led's are introduced into the circuit to act as indicators for the speed of the motor. Each led represents an increment of 25% of the motor's speed. In terms of the program, a new method called void ledCheck() is created to turn the led's on and off which is accessed by the line ledCheck();. This method simply turns on an led based on the speed of the motor, so if the motor is between 25% - 50% led 2 will turn on, if the motor is between 50-75% led 3 will turn on, etc.

Part 3 is quite different than part 1 and part 2 in terms of programming; the circuit looks the exact same though. In part 3 button A is pressed a number of times (up to 4) and each press increases the motor's speed to 25% times the number of presses. After that, the speed is decreased by 25% every 0.5 seconds until it reaches 0%. In terms of programming, first the counter is calculated, then when button B is pressed the method timesPushed is executed. In timesPushed, the variable power is given a value, then confirmed to be withing the range and then decreased in increments of 64 (25%) every 0.5 seconds until it reaches 0. Then the counter is reset after which the process is repeated.  


Time to complete:
10 mins to assemble
50 mins to program


Results:
The first time we ran it, there were many problems but after we fixed a few wires and some parts of the program everything ran perfectly fine.


Photo:


Tips:
  • Make sure your program logic works
  • Make sure the push buttons, the transistor and the diode are setup the right way
  • Make sure the motor is properly connected with the breadboard

Program Modifications:
There were major modifications from circuit 3. Two push buttons were added, four more LEDS were used, and the programs are very different as can be seen below.


Program(s):


(Part 1)

//Sets buttons to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int power = 0;

void setup()
{
    pinMode(motorPin, OUTPUT);  // assigns motorPin as an output
}

void loop()
{
  delay(20);
 
  boolean value = buttonCheck();     // Goes to buttonCheck method
 
  if (value == true)                           // if value is true then...
  {
    while (power > 0)                       // when power(speed of motor) is greater than 0...
    {
      power--;                                  // decrease the speed until it is less than 0
    }
  }
 
  else if (value == false)      // if value is false then...
  {
    while (power < 255)     // when power(speed of motor) is less than 255..
    {
      power++;                  // increase the speed until it is greater than 255
    }
  }
 
  analogWrite(motorPin, power);  // sends the speed to the arduino 
}// end loop


// Checks which button is pressed
boolean buttonCheck()
{
  boolean a;
 
  // if button A is pressed and button B isn't pressed then return a = true
  if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
  {
    a = true;
  }
 
  // if button A isn't pressed and button B is pressed then return a = false
  else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
  {
    a = false;
  }
 
  return a;  // the entire method returns a value to be read by the variable
}// end buttonCheck










(Part 2)

//Sets variables to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int ledPins[] = {10,11,12,13};
int power = 0;

void setup()
{
    pinMode(motorPin, OUTPUT);   // assigns motorPin as an output
    for(int i = 0; i < 4; i++)
    {
      pinMode(ledPins[i], OUTPUT); // sets leds on pins 10 - 13 as outputs
    }
}

void loop()
{
  delay(20);
 
  boolean value = buttonCheck();     // Reads the value returned by buttonCheck method
 
  if (value == true)                           // if value is true then...
  {
    while (power > 0)                       // when power(speed of motor) is greater than 0...
    {
      power--;                                  // decrease the speed until it is less than 0
    }
  }
 
  else if (value == false)      // if value is false then...
  {
    while (power < 255)     // when power(speed of motor) is less than 255..
    {
      power++;                  // increase the speed until it is greater than 255
    }
  }
 
  analogWrite(motorPin, power);  // sends the speed to the arduino 
 
  ledCheck();                              // Goes to ledCheck Method
}// end loop


// Checks which button is pressed
boolean buttonCheck()
{
  boolean a;
 
  // if button A is pressed and button B isn't pressed then return a = true
  if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
  {
    a = true;
  }
 
  // if button A isn't pressed and button B is pressed then return a = false
  else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
  {
    a = false;
  }
 
  return a;  // the entire method returns a value to be read by the variable
}// end buttonCheck


//Checks if an led should light up
void ledCheck()
{
  // sets all led's off
  for(int i = 0; i < 4; i++)
  {
     digitalWrite(ledPins[i], LOW);
  }
 
  // led connected to pin 10 turns on if motor is at 25% or less speed
  if (power <= 64)
  {
    digitalWrite(10, HIGH);
  }
 
  // led connected to pin 11 turns on if motor is between 25% and 50% speed
  else if ((power > 64) && (power <= 128))
  {
    digitalWrite(11, HIGH);
  }
 
  // led connected to pin 12 turns on if motor is between 50% and 75% speed
  else if ((power > 128) && (power <= 192))
  {
    digitalWrite(12, HIGH);
  }
 
  // led connected to pin 13 turns on if motor is greater than 75% speed
  else
  {
    digitalWrite(13, HIGH);
  }
}// end ledCheck










(Part 3)

//Sets buttons to pins
int motorPin = 9;
int buttonA = 2, buttonB = 3;
int ledPins[] = {10,11,12,13};
int power = 0, counter = 0;


void setup()
{
    pinMode(motorPin, OUTPUT);        // assigns motorPin as an output
    for(int i = 0; i < 4; i++)
    {
      pinMode(ledPins[i], OUTPUT);      // sets leds on pins 10 - 13 as outputs
    }
}




void loop()
{
  delay(50);
 
  if(digitalRead(buttonA) == LOW)    // if buttonA is pressed...
  {
     counter++;                                   // counter goes up by 1
  }
  else if(digitalRead(buttonB) == LOW)  // if buttonB is pressed...
  {
    timesPushed();                                   // go to timesPushed method         
  }


  ledCheck();                    // Goes to ledCheck Method  
}// end loop




// Method used to output speed to motor depending on the counter
void timesPushed()
{
  power = 64 * counter;   // Power increased in increments of 25%
 
  // Makes sure the speed stays  between 0 and 255
  if(power > 255)
  {
    power = 255;
  }
  else if(power < 0)
  {
    power = 0;
  }
 
  delay(500);
 
  // Power/Speed goes down by 25% every 0.5 seconds until it reaches 0
  while(power >= 0 && power <= 255)
  {
    power -= 64;
    delay(500);
  }
 
  //Makes sure the speed stays between 0 and 255
  if(power > 255)
  {
    power = 255;
  }
  else if(power < 0)
  {
    power = 0;
  }
 
  analogWrite(motorPin, power);  // sends the speed to the arduino
  counter = 0;                              // counter is reset to 0 to restart loop
}// end timesPushed




//Checks which leds should light up
void ledCheck()
{
  // sets all led's off
  for(int i = 0; i < 4; i++)
  {
     digitalWrite(ledPins[i], LOW);
  }
 
  // led connected to pin 10 turns on if motor is at 25% or less speed
  if (power <= 64)
  {
    digitalWrite(10, HIGH);
  }
 
  // led connected to pin 11 turns on if motor is between 25% and 50% speed
  else if ((power > 64) && (power <= 128))
  {
    digitalWrite(11, HIGH);
  }
 
  // led connected to pin 12 turns on if motor is between 50% and 75% speed
  else if ((power > 128) && (power <= 192))
  {
    digitalWrite(12, HIGH);
  }
 
  // led connected to pin 13 turns on if motor is greater than 75% speed
  else
  {
    digitalWrite(13, HIGH);
  }
}// end ledCheck

Friday, December 9, 2011

Circuit 13 - Flex Sensor

Purpose: To control a mini servo motor using a flex sensor potentiometer

Equipment: 
  • Arduino x 1 
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC - 13 Breadboard Sheet x 1
  • Flex Sensor x 1
  • Mini Servo x 1
  • 10k ohm resistor x 1
  • 3 pin header x 1
  • Wire x 8

References:

Program Details: 
In this circuit, another new piece of hardware is introduced, the flex sensor. The flex sensor uses carbon on a strip of plastic to sense resistance based on the bending of the strip. There is a higher level of resistance as the sensor is bent more; the range is from approximately 10k - 35k ohms. The arduino will sense the resistance provided by the flex sensor and translate it to a numerical value which will be used to control the position of the servo.

The programming sector of this exercise is quite simple. First the entire servo library is imported which allows us to use pre-made methods right away without the tedious coding. Then an object of type Servo is created along with a few variables. Then a connection between the serial monitor and arduino is created, allowing the user to see the resistance value. The resistance value is read from the flex sensor and is scaled as a numerical value. The numerical value is finally sent to the servo motor and the servo motor spins accordingly. 

Time to complete: 10 mins to assemble
                                5 mins to code

Results: The circuit and program both ran perfectly the first time although it was hard to place the servo onto the breadboard/pin header. 

Photo: 

Tips: 
  • Make sure the servo wires match up with ground, power and signal properly (black with ground, red with power, and white with signal)
  • Make sure the circuit involved with the flex sensor is proper (connect to ANALOG PIN 0, add resistor)

Further Work: I will calibrate the sensor's range to make it turn better.

Program Modifications:
The program is very similar to the one in the link below:

Program:
// imports the entire Servo library
#include <Servo.h> 

// an object "myservo" created of type Servo
Servo myservo;  

int potpin = 0;  // analog pin used to connect the potentiometer
int val;             // reads value from the potentiometer

void setup() 
  Serial.begin(9600);  // creates a connection between the serial monitor and arduino
  myservo.attach(9);  //  the servo object is controlled through pin 9 

void loop() 
  val = analogRead(potpin);              // the value of the potentiometer is read and stored 
  Serial.println(val);                           // the value of the potentiometer is displayed in the serial monitor
  val = map(val, 50, 300, 0, 179);     // the scale is changed to be compatible with the servo 
  myservo.write(val);                        // sets the servo position according to the scaled value 
  delay(15);                           

Circuit 14 - Soft Potentiometer

Purpose: To control the colour of an RGB led by using the touch sensor of a soft Potentiometer.

Equipment:

  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC - 14 Breadboard Sheet
  • RGB LED x 1
  • Soft Potentiometer x 1
  • 330 Ohm Resistor x 3
  • Wire x 9

References:
In this circuit, a soft potentiometer is introduced. It is similar to the potentiometer in the past except this potentiometer senses touch and not torque. The soft potentiometer senses resistance between 100 to 10k ohms depending on where pressure is applied. In this circuit it will be used to change the colour of an RGB LED. 

The programming sector is quite easy as well. No new concepts are introduced so all this is previous knowledge. First all pins and set and declared. Then the value of the soft pot is read and used to calculate the values of each the red, green and blue pigments. The red, green and blue pigments are then constrained, meaning that any value above or below the range would be viewed as maximums of the range, no higher or lower. The green is constrained so that it is always between red and blue. After the constraining, the program writes (to the RGB LED) to display its colour using the calculated amounts of colour. 

Time to Complete: 10 mins to assemble
                                10 mins to code

Results: The first time I ran this program, only the red led could be seen and the colour of the RGB LED would only become dim red to bright red as pressure was applied to the potentiometer, barely any blue or green could be seen. Then analogWrite(RED_LED_PIN, redValue); was deleted from the program and the circuit was re-tested. This time the colours changed from blue - green as pressure was applied so the purpose of this exercise was complete. There was no need to put the red back in as it overpowered the other two led's. 

Photo:

Video: 


Tips:
  • Place RGB leads correctly, the correct side can be adjusted by looking at the flat side
  • Make sure to connect the lead from the potentiometer to ANALOG PIN 0 and not DIGITAL PIN 0.
  • Remove or Reduce the output light of the red LED as it may overpower the others. 

Further Work: I will try and use HSB colour codes rather than RGB colour codes in order to have a cleaner fade from red to violet when using the soft potentiometer. 

Program Modifications: 
This program is almost the same as the one in the link below:


Program:
// declares the pin each lead is connected to
const int RED_LED_PIN = 9;        
const int GREEN_LED_PIN = 10; 
const int BLUE_LED_PIN = 11;   

void setup() // no setup needed
{
}

void loop()
{

  int sensorValue = analogRead(0);   //reads the value of the soft potentiometer
  
  //calculates the red value using the sensor value (255-0 over the range 0-512)
  int redValue = constrain(map(sensorValue, 0, 512, 255, 0),0,255); 
  
  //calculates the green value (0-255 over 0-512 & 255-0 over 512-1023)
  int greenValue = constrain(map(sensorValue, 0, 512, 0, 255),0,255)-constrain(map(sensorValue, 512, 1023, 0, 255),0,255);  
  
  //calculates the blue value 0-255 over 512-1023
  int blueValue = constrain(map(sensorValue, 512, 1023, 0, 255),0,255); 

  //uses the calculated values to display a colour using the RGB
  analogWrite(RED_LED_PIN, redValue);
  analogWrite(GREEN_LED_PIN, greenValue);
  analogWrite(BLUE_LED_PIN, blueValue);

Circuit 12 - RGB LEDs

Purpose: To use an RGB led to make a rainbow of colours

Equipment:

  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC - 12 Breadboard Sheet x 1
  • RGB LED x 1
  • 330 Ohm Resistor x 3 
  • Wire x 6

References:


Program Details:
In this circuit a new piece of hardware is introduces, an RGB led. The RGB led is basically three led's put together, a red, a green and a blue stuck inside a small package. And to create different colours, it uses a combination of all 3 LED's. This is done because red, green and blue are the primary colours of light and all different shades of light can be created by mixing those three colours. 

The programming aspect is quite simple in this exercise; no new concepts are introduced. First, all three LED's are declared and initialized. Then, by using a for loop, the program cycles colours (in increments of 5) between red and green, then between green and blue and finally between blue and red. Each increment/decrement has a 100 millisecond delay between it. 


Time to Complete: 5 mins to assemble
                                10 mins to code

Results: The RGB LED worked perfectly the first time. 

Photo: 



Video: 

Tips:
  • Place RGB leads correctly, the correct side can be adjusted by looking at the flat side
  • Both the program and the setup is very easy 

Further Work: I will change the colours more rapidly or make different colours than the ones that were created through this experiment.

Program Modifications: 
This program is almost the same as the one in the link below:

Program:
// declares which pins are connected to which leads
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;

// stores intensity level of the leds
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

int time = 100; // time between each interval of colour change

void setup()    // no setup
{
}

void loop() 
{
  // Changes the colour from red to green in increments of 5 every 100 milliseconds
  for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
        redIntensity = 255-greenIntensity;
        analogWrite(GREEN_LED_PIN, greenIntensity);
        analogWrite(RED_LED_PIN, redIntensity);
        delay(time);
  }

  // Changes the colour from green to blue in increments of 5 every 100 milliseconds
  for (blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5) {
        greenIntensity = 255-blueIntensity;
        analogWrite(BLUE_LED_PIN, blueIntensity);
        analogWrite(GREEN_LED_PIN, greenIntensity);
        delay(time);
  }
  
  // Changes the colour from blue to red in increments of 5 every 100 milliseconds
  for (redIntensity = 0; redIntensity <= 255; redIntensity+=5) {
        blueIntensity = 255-redIntensity;
        analogWrite(RED_LED_PIN, redIntensity);
        analogWrite(BLUE_LED_PIN, blueIntensity);
        delay(time);
  }
}// end loop

Monday, November 21, 2011

Lab Expansion - Circuit 7

Purpose: To modify circuit 7 to do more advanced functions


Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-07 Breadboard Sheet x 1
  • Pushbutton x 2
  • 330 ohm resistor x 4
  • 10k ohm resistor x 2
  • LED x 4
  • Wire x 10

References: A more advanced circuit than the one below
http://milindarduino.blogspot.com/2011/10/circuit-07-push-buttons.html


Program details:
The circuit setup is almost the same as circuit 7; there are 4 led's rather than just 1 led. The programming sector  however is quite different. In Part 1 no new programming concepts were introduced.

In Part 2, a new concept is introduced; a method. A method contains a group of programming that has a function and may be used multiple times. Methods are created to make the programming neater and to save some space and time if it is used multiple times because a programmer won't have to type the same code again. A method does not activate as part of the code until it is "called." It is called by putting the name of the method then brackets and the parameters inside the brackets. In  the code, timesPushed(); is used to call the method void timesPushed(). Since this method has no parameters, no parameters are used when calling it. But if it did have parameters, the same type of parameter must be used to call it. So for example, if the method was void timesPushed(int num), the code to call it would be timesPushed(int a); or timesPushed(7);.

Part 3 is almost the same as Part 2, however there was one new concept introduced, multiple conditions in if statements. Inside an if statement, multiple conditions can be given by adding "&&" or "||". "&&" means that both or all conditions must be met for the program to run what is inside the if statement. "||" means that any one of the conditions must be met for the program to run what is inside the if statement.
   
Time to complete: 10 mins to assemble
                                45 mins to program


Results:
The first time we ran it, only two of the led's were blinking. We replaced the two that weren't blinking but then they were all on when the buttons weren't even pressed.We changed up some parts of the code and then it worked. For part 2, our buttons weren't registering anything but that was because we had used HIGH instead of LOW. Once that was fixed the circuit ran perfectly. Part 3 was very easy as only two lines of code were changed so it worked perfectly the first time.

Videos:
Part 1

Part 2


Part 3


Tips:
  • When putting the resistors in for the push buttons, make sure to connect the resistors to the positive flow of electricity.
  • Check your code for logic errors as that is the most common problem
  • Try to make your code as concise as possible but make it still work

Program Modifications:
A lot of modifications were made from circuit 7. In part 1, led's light up according to which button is pressed. If the first button is pressed led's connected to pin 9 and 11 light up whereas if the second button is pressed, led's connected to pin 10 and 12 light up. In part 2 the first push button is used to control which led lights up. The second push button is used to activate that led. So if the first button was pressed once and then the second button was pressed led connected to pin 9 would light up, if the first button was pressed twice, then the led connected to pin 10 would light up etc.... If the first button was pressed  more than 4 times, all the led's would light up. Part 3 is the same as part 2 except that both buttons control which led light up and then the second button is used to register that count.


Program(s):
(part 1)

int led [] = {9,10,11,12};           // set led pins
int buttonA = 2, buttonB = 3;     // set pushButton pins

void setup()
{    
  for(int i = 0; i < 4; i++)             // intializes all leds as outputs
  {
    pinMode(led[i], OUTPUT);  
  }
  pinMode(buttonA, INPUT);    // intializes buttons to inputs
  pinMode(buttonB, INPUT);
}

void loop()
{
  if(digitalRead(buttonA) == LOW)    // if buttonA is pressed
  {
    digitalWrite(led[0], HIGH);           // led at pins 9 and 11 will blink
    digitalWrite(led[2], HIGH);
    delay(500);
    digitalWrite(led[0], LOW);
    digitalWrite(led[2], LOW);
  }

  else if(digitalRead(buttonB) == LOW)    // if buttonB is pressed
  {
    digitalWrite(led[1], HIGH);                  // led at pins 10 and 12 will blink
    digitalWrite(led[3], HIGH);
    delay(500);
    digitalWrite(led[1], LOW);
    digitalWrite(led[3], LOW);
  }
}




(part 2)


int led [] = {9,10,11,12};           // set led pins
int buttonA = 2, buttonB = 3;     // set pushButton pins
int counter = 0;                          // counter used to store button presses

void setup()
{  
  for(int i = 0; i < 4; i++)             // initializes all leds as outputs
  {
    pinMode(led[i], OUTPUT);
  }
  pinMode(buttonA, INPUT);    // intializes buttons to inputs
  pinMode(buttonB, INPUT);
}

void loop()
{
  if(digitalRead(buttonA) == LOW)    // if buttonA is pressed...
  {
     counter++;                                   // counter goes up by 1
     digitalWrite(led[0], HIGH);          // led at pin 9 blinks
     delay(500);
     digitalWrite(led[0], LOW);  
  }
  else if(digitalRead(buttonB) == LOW)  // if buttonB is pressed...
  {
    timesPushed();                                   // go to timesPushed method         
  }
}

void timesPushed()
{
  if(counter == 1)                     // if the counter is at 1...
  {
    digitalWrite(led[0], HIGH); // led at pin 9 blinks
    delay(500);
    digitalWrite(led[0], LOW);
  }
  else if(counter == 2)              // if the counter is at 2...
  {
    digitalWrite(led[1], HIGH); // led at pin 10 blinks
    delay(500);
    digitalWrite(led[1], LOW);
  }
  else if(counter == 3)               // if the counter is at 3...
  {
    digitalWrite(led[2], HIGH);  // led at pin 11 blinks
    delay(500);
    digitalWrite(led[2], LOW);
  }
  else if(counter == 4)               // if the counter is at 4...
  {
    digitalWrite(led[3], HIGH);  // led at pin 12 blinks
    delay(500);
    digitalWrite(led[3], LOW);
  }
  else if(counter > 4)                 // if the counter is above 4...
  {
    for(int i = 0; i < 4; i++)
    {
      digitalWrite(led[i], HIGH);// All four led's blink
    }
    delay(500);
    for(int i = 3; i >= 0; i--)
    {
      digitalWrite(led[i], LOW);
    }  
  }
  else                         // if the counter is less than 1
  {
    delay(500);          // there is a half-second delay 
  }
  counter = 0;           // counter is reset to 0 to restart loop
}





(part 3)

int led [] = {9,10,11,12};           // set led pins
int buttonA = 2, buttonB = 3;     // set pushButton pins
int counter = 0;                          // counter used to store button presses

void setup()
{  
  for(int i = 0; i < 4; i++)             // initializes all leds as outputs
  {
    pinMode(led[i], OUTPUT);
  }
  pinMode(buttonA, INPUT);    // intializes buttons to inputs
  pinMode(buttonB, INPUT);
}

void loop()
{
  if(digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW)  
  // if buttonA and buttonB are pressed... 
  {
     counter++;                                   // counter goes up by 1
     digitalWrite(led[0], HIGH);          // led at pin 9 blinks
     delay(500);
     digitalWrite(led[0], LOW);  
  }
  else if(digitalRead(buttonB) == LOW  && digitalRead(buttonA) == HIGH)  // if only buttonB is pressed...
  {
    timesPushed();                                   // go to timesPushed method      
  }
}

void timesPushed()
{
  if(counter == 1)                     // if the counter is at 1...
  {
    digitalWrite(led[0], HIGH); // led at pin 9 blinks
    delay(500);
    digitalWrite(led[0], LOW);
  }
  else if(counter == 2)              // if the counter is at 2...
  {
    digitalWrite(led[1], HIGH); // led at pin 10 blinks
    delay(500);
    digitalWrite(led[1], LOW);
  }
  else if(counter == 3)               // if the counter is at 3...
  {
    digitalWrite(led[2], HIGH);  // led at pin 11 blinks
    delay(500);
    digitalWrite(led[2], LOW);
  }
  else if(counter == 4)               // if the counter is at 4...
  {
    digitalWrite(led[3], HIGH);  // led at pin 12 blinks
    delay(500);
    digitalWrite(led[3], LOW);
  }
  else if(counter > 4)                 // if the counter is above 4...
  {
    for(int i = 0; i < 4; i++)
    {
      digitalWrite(led[i], HIGH);// All four led's blink
    }
    delay(500);
    for(int i = 3; i >= 0; i--)
    {
      digitalWrite(led[i], LOW);
    }  
  }
  else                         // if the counter is less than 1
  {
    delay(500);          // there is a half-second delay 
  }
  counter = 0;           // counter is reset to 0 to restart loop
}

Wednesday, November 2, 2011

Non-Lab Entry: Nothing But Reboots

Summary: A great website for medium level arduino projects

Screenshot: 




Site Link: 
http://nothingbutreboots.com/projects/arduino/top-10


Review:
This website has quite a few projects based on arduino, similar to the hack n mod website. However, this website is great for people who have just finished the starter exercises from the starter kit and want to do something a little more complex. This website provides projects that are medium level rather than extremely hard projects.

A picture/video is provided of how the arduino should function and look like. Some of these projects are very interesting and quite easy to pull off. And the code is even provided for you. This website seems like a great place to get projects from to use what you have learned from the starter arduino kit to make simple useful circuits.

Saturday, October 29, 2011

Non-Lab Entry: Official Arduino Website

Summary: A great website for projects and project ideas


Screenshot: 






Site link: 
http://www.arduino.cc/playground/Projects/Ideas

Review:
Not only does this website provide a range of great projects for arduino, but it also provides information on circuits, schematics, arduino parts, arduino products and even tutorials. This website is really good for learning about specific arduino parts such as shift registers or transistors. Through the main site, you may also download the arduino IDE for free as well as buy arduino starter kits and specific parts.

This is very useful for learning more about arduino. We can also order parts from here and start to build bigger projects in school such as wall-avoiding robots or drink-serving robots. There are tutorials to follow and things to do on this website.

Non-Lab Entry: Sparkfun

Summary: This website is great for buying and learning about arduino parts and products

Screenshot:




Site link: http://www.sparkfun.com/

Review:
Sparkfun provides a lot of equipment for arduino including kits and advanced items as well. Its main focus is products but this website has tutorials as well. Like with the products, there are beginner tutorials as well as much more advanced tutorials.

Also, it is very easy to navigate and very user friendly. There are product reviews as well as product suggestions and recommendations. Another great thing is that there are product details which explain what each and every part does. There are so many available products that one could spend an entire day just viewing and reading details about them.


Non-Lab Entry: Simon Game

Summary: This website explains how to make a Simon Says game using arduino.

Screenshot:

Site Link: http://www.codeproject.com/KB/system/arduino_simon.aspx

Review:  This specific entry talks about how to make a Simon Says game using arduino. It gives you the code to do it and explains what each part of the code does, its very useful and easy to use, especially for beginners.

 It also provides a schematic of the arduino as well as the program files so if you have the necessary materials, you can make the Simon game and upload the code to make it work right away. It is also very simple and could teach a few things about  programming. I would like to make a simon says game because it doesn't seem too complicated.

Non-Lab Entry: Hack N Mod

Summary: Hacknmod is a great website for lots of really cool, advanced arduino projects.

Site Screenshot:





Site link: http://hacknmod.com/hack/top-40-arduino-projects-of-the-web/


Review:
This website has a lot of projects based on arduino. The specific link provided above talks about 40 really cool projects that this site has to offer. The projects vary from making a wall-avoiding robot, to a gameboy to a UAV spy plane.

However, the projects are quite advanced and should only be attempted after we gain a better understanding of arduino's capabilities. I find the projects on this website to be really amazing, I have watched a few videos on the final results of these projects and most of them turn out really well.



Sunday, October 23, 2011

Circuit 11 - Larger Loads

Purpose:  To use a relay to switch currents between two LEDs

Equipment: 
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-11 Breadboard Sheet x 1
  • Diode (1N4001) x 1
  • Transistor (P2N2222AG) x 1
  • Relay (SPDT) x 1
  • 10k Ohm Resistor x 1
  • 330 Ohm resistor x 2
  • LED x 2
  • Wire x  13

References:
Program Details:
This circuit is much more complicated than what we've used in the previous exercises. In this circuit a relay is introduced. A relay has a mechanical switch inside it which, when energized, trips and switches the current. In this circuit, we will use the switch to consecutively turn two LEDs on and off.

There is a diode in this circuit which is not really necessary but it is helpful. The purpose of it is to prevent energy loss as it makes sure that the electricity is flowing in one direction only. A transistor is used to switch the current into the relay. Without the transistor, we cannot control the Relay as it is connected to Pin2.

The programming in this is very simple, it is nearly the same as the very first program. There should be no difficulties in the programming part of this exercise. 


Time to Complete: 15 mins to assemble
                                 5 mins to code

Results: The first time we uploaded the program, nothing happened. This was because the transistor wasn't placed properly, so we flipped it around but only one light came on and it wasn't even blinking. We tried various things like switching LEDs, wires, resistors and even trying another group's Relay. We decided to rebuild it except this time we forget to add the diode. However when we ran it this time, the circuit worked. When we put the diode though, it stopped working. This was because we placed the diode in the wrong way so it didn't let any of the current pass through.

Picture:


Video: 


Tips: 
  • Place the transistor correctly, the flat side should be facing the relay and make sure it is not a temperature sensor.
  • Place the diode correctly, if it doesn't work then remove it from the circuit, it is not necessary
  • There are a lot of wires to connect, make sure you connect all of them

Further Work: I will change the delay time at a decreasing rate to make the LEDs blink faster and faster and then increase the delay to make them blink slower and slower. This will be similar to whats shown in the video. 

Program Modifications: The program is very similar to the one in the link below. The only change is the delay time which was switched from 1000 milliseconds to 500 milliseconds.

Program:

int ledPin =  2;                         // Relay is connected to pin2

void setup()  
{                
  pinMode(ledPin, OUTPUT); // ledPin is set as an output     
}

void loop()                     
{
  digitalWrite(ledPin, HIGH);    // The Led turns on
  delay(500);                           // Wait for 500 milliseconds
  digitalWrite(ledPin, LOW);   // The Led turns off
  delay(1000);                        // Wait for 500 milliseconds
}  

Circuit-10 - Temperature

Purpose: To get the arduino to display the room temperature

Equipment:

  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-10 Breadboard Sheet x 1
  • TMP36 Temperature Sensor x 1
  • Wire x 5

References:
Program Details:
Once again, a new piece of hardware is introduced in this circuit. It is the temperature sensor, and it looks exactly like the transistor. To tell them apart you must read the label on the head. If it says TMP36, then its a temperature sensor, otherwise, its a transistor. The temperature sensor requires 3 connections. One is connected to power (in this case 5v), one connected to ground and one connected to an analog pin for input. What the temperature sensor does is sense the temperature in the room and convert it into electricity and then sends it to the computer through the analog pin. Then using the program, we convert the electricity into numbers and use the program to display it.

Many new things are introduced in terms of programming. First up, a pre-made class named Serial. The purpose of Serial is to create a connection between the computer and arduino. It is also used to display the temperature inputted by the arduino through the line Serial.println();. Serial.begin(); is used to determine the speed at which the information is begin transferred back and forth. The other new concept introduced in this program is a method which has a data type. The method float getVoltage(int pin);, returns a value at the end of the method of type float. Float is a number with decimals between -3.40 and 3.40. The returned value is then used by the line of code that called the method, so now float temperature is equal to the value returned by the method.
    
Time to Complete: 5 mins to assemble
                                10 mins to code

Results: The circuit and the program both ran perfectly the first time. However, what the temperature sensor was displaying was wrong or out of range at first. After 30 seconds or so, it started becoming close to room temperature (around 18 - 25 degrees Celsius).

Pictures: 





Tips: 
  • Make sure you are using TMP36 temperature sensor and not the Transistor, they both look the same
  • If the temperature sensor is displaying temperatures above 35 degrees Celsius, then disconnect the wire from the computer, it may be over-heating.
  • Don't forget to click on Serial Monitor in the arduino program menu after uploading the program to display the values.  


Further Work: I will convert the temperature from Celsius to Fahrenheit by using simple math. I will also test what happens when I change the serial speed and what the optimal serial speed for the arduino is.

Program Modifications: The program is very similar to the one in the link below. However we did change one thing. We changed the delay speed to make the program display the temperature faster. Rather than displaying it every second, we made it display the temperature every half second or every quarter second.

Program:
 int temperaturePin = 0;    // The temperature senosor is connected to Analog Pin 0

void setup()
{
  Serial.begin(9600);        // A serial connection between the arduino and computer is made
}
 
void loop()                    
{
 float temperature = getVoltage(temperaturePin);  // Stores the voltage from temperaturePin into variable temperature 
 temperature = (temperature - .5) * 100;              // Converts from 10 mv per degree to degrees Celsius
 Serial.println(temperature);                                  // Prints the results in the serial monitor
 delay(500);                                                         // Delays 500 milliseconds
}

float getVoltage(int pin)                            // Returns the voltage from the analog pin 
{
 return (analogRead(pin) * .004882814); // Converting from a 0 to 1024 digital range
}

Thursday, October 20, 2011

Circuit-09 - Light Resistors

Purpose: To test how photo resistors responds to different amounts of light.

Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-09 Breadboard Sheet x 1
  • Photo Resistor x 1
  • 330 Ohm Resistor x 1
  • 10k Ohm Resistor x 1
  • LED x 1
  • Wire x 6

References:

Program Details: In this exercise, a Photo resistor is introduced. A photo resistor senses light and returns a voltage value. A photo resistor must be connected to a resistor, an analog pin, as well as to ground but it only has 2 pins. So how can this be done? Well the resistor and the analog pin are both connected to one lead and the ground is connected to the other lead.

Yet again, the programming is very simple. Only two new method are introduced, both which are very easy to understand. First map(lightLevel, 0, 900, 0, 255); what it does is assign a new range to lightLevel. Instead of it going from 0 - 900, it now goes from 0 - 255. Next is constrain(lightLevel, 0, 255); which makes sure that the range of lightLevel is between 0 - 255. The rest of the code is very simple as is the exercise. 

Time to Complete: 10 mins to assemble
                                10 mins to code

Results: The LED turned on and the circuit worked. However, when there is a change in the lighting in the room, the LED only grows dimmer or brighter very slightly. Even when the light was turned off, the LED grew slightly brighter. I think this is because the photo resistor is not the greatest so there was nothing wrong with the circuit or the program.

Photos: 

Circuit 9  with light on
Circuit 9 with light off
Tips:
  • Try controlling 2 or more LEDs rather than just one because this circuit is easy
  • Don't forget to attach a resistor connected to your photo resistor as shown in the schematic 


Further Work:
I will control the LED to do the opposite, so when there is a greater amount of light, the LED will grow brighter and when there is a lower amount of light, the LED will grow dimmer. I will also try to control a small servo using the photo resistor.

Program Modifications: The program is the exact same one from the link below
http://ardx.org/src/circ/CIRC09-code.txt

Program:
int lightPin = 0;   // photoresistor pin is connected to analog pin 0 
int ledPin = 9;    // LED pin is connected to pin 9

void setup()
{
  pinMode(ledPin, OUTPUT);    //sets the led pin as an output
}

void loop()
{
 int lightLevel = analogRead(lightPin);             // The light level from the lightPin is stored in lightLevel
 lightLevel = map(lightLevel, 0, 900, 0, 255); // Changes the range (from 0-900) to 0 - 255 (which is colour) 
 lightLevel = constrain(lightLevel, 0, 255);      // Makes sure that the value is between from 0 - 255
 analogWrite(ledPin, lightLevel);                    // Writes the value into ledPin from lightLevel 
 }// ends loop

Wednesday, October 19, 2011

Circuit-08 - Potentiometers

Purpose: To change the LED frequency by turning the potentiometer accordingly

Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC- 08 Breadboard Sheet x 1
  • Potentiometer (10k Ohm) x 1
  • 330 Ohm Resistor x 1
  • LED x 1
  • Wire x 6

References:
Program Details:
In this circuit, we will be twisting a potentiometer to make an LED blink at different time intervals. The only new hardware introduced in this circuit is the potentiometer. What a potentiometer does is it reads some value between 0 and the amount of volts it is connected to, (in this case 5), and generates a value between those volts depending on the angle that the potentiometer is at.

In the program, we will be using the value generated by the potentiometer to cause a delay in the blinking time of the LED. This is done by the line sensorValue = analogRead(sensorPin). The analogRead method reads the position at which the potentiometer is at and then stores it into sensorValue. As we turn the potentiometer farther and farther away from 0 degrees, the delay between the LED going on and off  will increase. And as we turn the potentiometer closer to 0 degrees, the delay between the LED going on and off will decrease. This is shown by the last four lines of code in the program.

Both the program and the circuit are quite easy to understand and create.    


Time to Complete: 5 mins to assemble 
                                10 mins to program

Results: The circuit and program both ran perfectly the first time. As we twisted the potentiometer, the frequency at which the LED was blinking either increased or decreased depending on the way and angle we turn the potentiometer to.

Picture:


Video:





Tips:

  • Since this circuit is easy, use the schematic to create the circuit rather than using the breadboard. Later, double check if you did it right. This will help you understand schematics.

Further Work: I will try to control multiple LEDs by using only one potentiometer. If that is successful, then I will try to control a servo using the potentiometer. 

Program Modifications: This program is the exact same as in the link below.


Program:

int sensorPin = 0;       // sets input pin for the potentiometer
int ledPin = 13;          // sets input pin for the LED to pin13
int sensorValue = 0;  // stores values from the potentiometer

void setup()
{
  pinMode(ledPin, OUTPUT);  // intializes ledPin as an output
}

void loop() 
{
  sensorValue = analogRead(sensorPin);  // Values of sensorPin is read and stored into sensorValue 
  digitalWrite(ledPin, HIGH);                   // Turns on LED
  delay(sensorValue);                              // Delays LED for sensorValue milliseconds       
  digitalWrite(ledPin, LOW);                   // Turns off LED
  delay(sensorValue);                              // Delays LED for sensorValue milliseconds
}// end loop

Tuesday, October 18, 2011

Circuit-07 - Push Buttons

Purpose: To use a push button to complete a circuit.

Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-07 Breadboard Sheet x 1
  • Pushbutton x 2
  • 330 Ohm resistor x 1
  • 10k Ohm resistor x 2
  • LED x 1
  • Wire x 7

References: 
Program Details: 
Both the assembly of the circuit and the programming of the circuit are very simple in this exercise. In this circuit, we are basically controlling the LED staying on with the push button. When the push button is held down, the LED stays on, and when it is not pressed down, the LED stays off. This circuit should be easily built by following the schematic or the breadboard.

There is one new method introduced in this exercise, digitalRead().  What this method does is it reads the state of variable in the parameters and gives the output to another variable. In this example, buttonState = digitalRead(buttonPin); what this does is it reads the state (either HIGH or LOW) of buttonPin and assigns that read state as a value to the variable buttonState. Now in the program, according to the value of buttonState, the LED is either turned on or off.  

Time to complete: 5 mins to assemble
                               10 mins to program

Results: The first time we ran it, our push button did nothing. This was because we hadn't connected it to Pin2. The first button worked after that, but the second button still didn't do anything but this was because there was nothing about the second button in the program. The second button is used to turn off the LED in the second part of this circuit (if you decide to do it). 

Photo: 


Tips:
  • If the push button doesn't work, try flipping it around in 90 degree intervals
  • If the push button still doesn't work, try switching it with another one, the first one may have been nonfunctional
  • Don't forget to connect your resistors and wires 

Further Work: I will write the program to make the other button turn off the LED as well as make the LED's fade when they turn off and on.

Program Modifications:
The program is the same as the program in the link below:

Program:

// constants remain the same throughout the program
const int buttonPin = 2;      // pushbutton pin number
const int ledPin =  13;        // LED pin number

int buttonState = 0;           // buttonState 0 means it is currently doing nothing

void setup() 
{
  pinMode(ledPin, OUTPUT);     // initializes ledPin as an output   
  pinMode(buttonPin, INPUT);    // intializes buttonPin as an output    
}

void loop()
{
  buttonState = digitalRead(buttonPin);  // reads the state (HIGH or LOW) of buttonPin
  if (buttonState == HIGH)                   // if state is HIGH (meaning button is pressed) ...
  {     
    digitalWrite(ledPin, HIGH);             // turn on LED
  } 
  else                                                 // otherwise (meaning button is not pressed) ...
  {
    digitalWrite(ledPin, LOW);           // turn off LED
  }
}// end of loop




Sunday, October 16, 2011

Circuit-06 - Piezo Elements

Purpose: To play a short song using the Piezo Element

Equipment: 
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-06 Breadboard Sheet x 1
  • Piezo Element x 1
  • Wire x 4

References:

Program Details:
The building of the circuit in this exercise very very simple. The only new thing is the introduction of the Piezo Element. The Piezo element has 2 leads and is in a circular shape, the positive lead must be connected to arduino an arduino pin, and the negative lead to the ground. The positive lead will have a plus sign on top of it.

Once again, there are a lot of new concepts introduced in the programming. First, an array, char notes[], is used to hold a bunch of the same type of data in one simple line, instead of creating a lot of different variables. Secondly, methods with parameters are introduced, playNote(char note, int duration). When a method has parameters, it must be called by using the same type of data as indicated in the parameters. For example, this line of code, playTone(tones[i], duration), calls the method playNote using a character (tones[i]) and an integer (duration). This method cannot be called by simply typing playNote(). Thirdly, when a method is called, the program goes to the method and progresses through it before continuing on with the next line of code.

Even by understand all the new concepts , this program is still quite hard to understand so the comments given should be looked at to fully understand the code. 

Time to Complete: 2 mins to assemble
                                15 mins to program

Results: Our Piezo element ran perfectly the first time we did this. It played the first verse of Twinkle Twinkle Little Star.

Photo:


Video:

Tips:
  • Make sure you understand your code, there are a lot of loops and many new concepts
  • If your Piezo element is not playing, try switching its two leads around 

Further Work: I will try to make another short song using the Piezo Element, such as Mary Had a Little Lamb or Happy Birthday. I will also attach an LED to display the beat of the song. 


Program Modifications:
This program is the exact same one from the link below:

Program:

int speakerPin = 9;                        // sets the input pin to pin9
int length = 15;                              // the number of notes
char notes[] = "ccggaagffeeddc ";  // the notes in order
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; // the beat of the notes
int tempo = 300;                          // the speed of the melody


void playTone(int tone, int duration)    // method playTone used to play a tone
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) //note is only played as long as it is less than its duration
  {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }// ends for loop
}// ends playTone


void playNote(char note, int duration)   // method playNote used to play a note
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };        // Notes in the song
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };  // Tone of each note   
  // plays the tone corresponding to the note name
  for (int i = 0; i < 8; i++)   // playes all 8 notes
  {
    if (names[i] == note)       // corresponds the tone with the note
    {
      playTone(tones[i], duration); // refers to playTone method giving it the parameters
    }
  }// ends for loop
}// ends playNote


void setup()                                     // intializes once when program runs
{
  pinMode(speakerPin, OUTPUT); // sets speakerpin as an output
}


void loop()                                    // loop continues as long as there is power
{
  for (int i = 0; i < length; i++)        // plays 15 notes
  {
    if (notes[i] == ' ')                      // if the character is a space..
    {
      delay(beats[i] * tempo);         // then there is a rest
    }
    else                                         // otherwise...
    {
      playNote(notes[i], beats[i] * tempo);   // play the note according to the beat and tempo
    }
    delay(tempo / 2); // creates a slight pause
  }// ends for loop
}// ends the loop