Monday 28 November 2011

CIRC 03 (tiny.cc)

Purpose:
To control the speed of a motor via two pushbuttons. Then adding extensions to the program such as a series of LED's to tell the speed of the motor, and deceleration over time.

Equipment:

  • Breadboard x1
  • Arduino x1
  • Buttons x2
  • Motor x 1
  • 10k ohm resistor x3
  • Diode x1
  • Transistor x1
  • Wire x10
References:

Program Details:
The building itself is not very complicated, as a picture is provided at [1]. Furthermore we have already finished a very similar circuit layout before [2]. The only exception being that there are now 6 more independent circuits, w of which are to get button input, and another 4 output to the LEDs.

In terms of programming, Part 1 was rather simple. What the program in Part 1 does is access a custom method called buttonCheck(); which does as its name implies. After checking which button is being press, and that two buttons are not being press at once, it goes through one of two if statements. Depending on which button is pressed, the program will either accelerate or decelerate the motor. The while loops are in place to insure the value being supplied to the motor isn't greater than 255 or less than 0, as those are the motors max and min values. 

Part 2 is exactly like Part 1, except there is now another step added to the end called ledCheck(). This custom method checks the value for power (current speed) and lights the corresponding LED. Each LED is on its own circuit. The final part adds just one more line. This line reduces the power of the LED by 25% every half second, and is meant to simulate deceleration. It happens at the end of each loop().

Tips:

Further Work:
You could make it so double tapping a button causes either maximum speed or brings the motor to a halt.

Program:

//PART 1

int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
    pinMode(motorPin, OUTPUT);
}
void loop()
{
  delay(100);
 
  int value = buttonCheck();
 
  if (value == 1)
  {
    while (power > 0)
    {
      power--;
    }
  }
 
  else if (value == 2)
  {
    while (power < 255)
    {
      power++;
    }
  }
  analogWrite(motorPin, power); 
}

int buttonCheck()
{
  int a;
 
  if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
  {
    a = 1;
  }
 
  else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
  {
    a = 2;
  }
  return a;
}

//PART 2

int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
    pinMode(motorPin, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}
void loop()
{
  delay(100);
 
  int value = buttonCheck();
 
  if (value == 1)
  {
    while (power > 0)
    {
      power--;
    }
  }
 
  else if (value == 2)
  {
    while (power < 255)
    {
      power++;
    }
  }
  analogWrite(motorPin, power);

  ledCheck();
}
int buttonCheck()
{
  int a;
 
  if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
  {
    a = 1;
  }
 
  else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
  {
    a = 2;
  }
  return a;
}
void ledCheck();
{
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
 
  if (power <= 64)
  {
    digitalWrite(10, HIGH);
  }
 
  else if ((power > 64) && (power <= 128))
  {
    digitalWrite(11, HIGH);
  }
 
  else if ((power > 128) && (power <= 192))
  {
    digitalWrite(12, HIGH);
  }
 
  else
  {
    digitalWrite(13, HIGH);
  }
}

//PART 3


int motorPin = 9;
int buttonA = 2;
int buttonB = 3;
int power = 0;
void setup()
{
    pinMode(motorPin, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}
void loop()
{
  delay(100);
 
  int value = buttonCheck();
 
  if (value == 1)
  {
    while (power > 0)
    {
      power--;
    }
  }
 
  else if (value == 2)
  {
    while (power < 255)
    {
      power++;
    }
  }
  analogWrite(motorPin, power);
 
  power -= 5;                   //Power decreases by 5(1/5 of 25 as delay = 1/5 0.5)

  ledCheck();
}
int buttonCheck()
{
  int a;
 
  if ((digitalRead(buttonA) == LOW) && (digitalRead(buttonB) == HIGH))
  {
    a = 1;
  }
 
  else if ((digitalRead(buttonA) == HIGH) && (digitalRead(buttonB) == LOW))
  {
    a = 2;
  }
  return a;
}
void ledCheck();
{
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
 
  if (power <= 64)
  {
    digitalWrite(10, HIGH);
  }
 
  else if ((power > 64) && (power <= 128))
  {
    digitalWrite(11, HIGH);
  }
 
  else if ((power > 128) && (power <= 192))
  {
    digitalWrite(12, HIGH);
  }
 
  else
  {
    digitalWrite(13, HIGH);
  }
}

Tuesday 15 November 2011

Tiny.cc/ArdiunoGuide - Circ - 07

//PART 1
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0, time = 500;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
 
    if ((buttonA == HIGH) && (buttonB == LOW))
    {
      evenLED();
    }
   
    else if ((buttonB == HIGH) && (buttonA == LOW))
    {
      oddLED();
    }
}

void evenLED()
{
  for(int i = 0; i < 4; i +=2)
  {
    digitalWrite(ledPin[i], HIGH);
    delay(time);
    digitalWrite(ledPin[i], LOW);
    delay(time);
  }
}

void oddLED()
{
  for(int i = 1; i < 4; i +=2)
  {
    digitalWrite(ledPin[i], HIGH);
    delay(time);
    digitalWrite(ledPin[i], LOW);
    delay(time);
  }
}
     
//PART 2
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0;
int time = 500;
int counter;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
  counter = 0;
 
  while (!((buttonA == HIGH) && (buttonB == HIGH)))     //Will not run while both buttons are pressed
  {
    if ((buttonA == HIGH) && buttonB == HIGH))
    {
      counter++;
     
      digitalWrite(ledPin[0], HIGH);
      delay(time);
      digitalWrite(ledPin[0], LOW);
      delay(time);
    }
   
    else if ((buttonB == HIGH) && (buttonA == LOW))
    {
      if (counter > 4)
      {
        allLED();
      }
     
      else if (counter == 0)
      {
        delay(1000);
      }
     
      else
      {
        counter--;                        //For approptiat index
        digitalWrite(ledPin[counter], HIGH);
        delay (time);
        digitalWrite(ledPin[counter], LOW);
        delay (time);
      }
    }
  }
}

//PART 3

int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0, time = 500;
void allLED()
{
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], HIGH);
  }
 
  delay(time);
 
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], LOW);
  }
 
  delay(time);
}
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0;
int time = 500;
int counter;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
  counter = 0;
 
  while (!((buttonA == HIGH) && (buttonB == HIGH)))     //Will not run while both buttons are pressed
  {
    if (buttonA == HIGH)
    {
      counter++;
     
      digitalWrite(ledPin[0], HIGH);
      delay(time);
      digitalWrite(ledPin[0], LOW);
      delay(time);
    }
   
    else if (buttonB == HIGH)
    {
      if (counter > 4)
      {
        allLED();
      }
     
      else if (counter == 0)
      {
        delay(1000);
      }
     
      else
      {
        counter--;                        //For approptiat index
        digitalWrite(ledPin[counter], HIGH);
        delay (time);
        digitalWrite(ledPin[counter], LOW);
        delay (time);
      }
    }
  }
}

void allLED()
{
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], HIGH);
  }
 
  delay(time);
 
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], LOW);
  }
 
  delay(time);
}

Sunday 6 November 2011

Top 40 Arduino Projects

This post was done in case the requirement was to do 4 different news blogs on top of our existing one. If that is not the case, please dismiss this post. Thank You.


Summary:
This page links to one of forty different Arduino Projects which we can try.

Photo:




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

Review:
Though the site itself is not Arduino focused, it does have a focus on creating and modifying pieces of software or hardware. So naturally they do have some degree of coverage on Arduino. The page itself is a central hub which links to forty different pages, each with an extensive coverage of an Arduino project. The links themselves contain information on what you need to build it, and how you build it. Most cover both the hardware and the software aspect.

It is known that modifying existing projects is much easier than creating your own. We can apply this principle in our class, as now students will be able to learn more faster my modifying these existing experiments. This will help strengthen our knowledge in both the hardware and software aspects associated with working with Arduino. One aspect of this site which I enjoyed was that I was able to see how Arduino was being applied. The shear variety of uses I saw here expanded my thinking of what I can do with Arduino.