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