tiistai 29. tammikuuta 2013

Accelerometer Memsic MX2125


This time i got Memsic accelerometer. The task was to find out what this sensor is and what it does.
The first thing what I did was that I opened arduino.cc and searched that sensor. There was a clear picture how to connect the sensor and in the development environment we found under sensors the basic code for this sensor. Link to the sensor:
http://arduino.cc/en/Tutorial/Memsic2125?from=Tutorial.AccelerometerMemsic2125
Then we just opened device monitor to see which axis where which and then added two leds which start to blink slower when the angle is growing. Picture of the setup:


The actual code, we added if-else statement that if the angle is growing the led should blink slower. Few videos here about the testing:



The code:

// these constants won't change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  // initialize the pins connected to the accelerometer
  // as inputs:
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
}

void loop() {
  // variables to read the pulse widths:
  int pulseX, pulseY;
  // variables to contain the resulting accelerations
  int accelerationX, accelerationY;

  // read pulse from x- and y-axes:
  pulseX = pulseIn(xPin,HIGH);
  pulseY = pulseIn(yPin,HIGH);

  // convert the pulse width into acceleration
  // accelerationX and accelerationY are in milli-g's:
  // earth's gravity is 1000 milli-g's, or 1g.
  accelerationX = ((pulseX / 10) - 500) * 8;
  accelerationY = ((pulseY / 10) - 500) * 8;

if (accelerationX > 100 && accelerationX < 200){
  digitalWrite(4, HIGH);
  delay(50);
  digitalWrite(4, LOW);
 delay(50);
}
else if (accelerationX > 200 && accelerationX < 300)
{
  digitalWrite(4, HIGH);
  delay(200);
  digitalWrite(4, LOW);
    delay(200);
}

else if (accelerationX > 300 && accelerationX < 400)
{
  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);
   delay(500);
}
else if (accelerationX > 400 && accelerationX < 500)
{
  digitalWrite(4, HIGH);
  delay(600);
  digitalWrite(4, LOW);
   delay(600);
}

else {
  digitalWrite(4, LOW);
}

//--------------------------------------------------------------------------------

if (accelerationY > 100 && accelerationY < 200){
  digitalWrite(5, HIGH);
  delay(50);
  digitalWrite(5, LOW);
 delay(50);
}
else if (accelerationY > 200 && accelerationY < 300)
{
  digitalWrite(5, HIGH);
  delay(200);
  digitalWrite(5, LOW);
    delay(200);
}

else if (accelerationY > 300 && accelerationY < 400)
{
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(5, LOW);
   delay(500);
}
else if (accelerationY > 400 && accelerationY < 500)
{
  digitalWrite(5, HIGH);
  delay(600);
  digitalWrite(5, LOW);
   delay(600);
}

else {
  digitalWrite(5, LOW);
}

  // print the acceleration
  Serial.print(accelerationX);
  // print a tab character:
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();

  delay(100);

}

perjantai 25. tammikuuta 2013

Arduino ping sensor


Me and Anders got a PING ultrasonic sensor. Our task was to find out what it does and how. Here is the sensor:


It sends ultrasonic sound from the other tube and then the other one is microphone which caches the sound. Then the actual code calculates how long it took for the sound to travel to the obstacle and back. Here is some basic code which sets the led in socket 13 on IF the distance is bigger than 10. If it's smaller than ten the led will turn off.

Connect PING signal to pin 7, GND to GND and power to +5v

#define fP 7 //Pin the ping sensor is connected to
void setup()
{
  pinMode(13, OUTPUT);     
}
void loop()
{
  if (ping() > 10) // set distance
  {
    digitalWrite(13, HIGH);   // set the LED on
  }
  else
  {
    digitalWrite(13, LOW);    // set the LED off
  }
}
int ping() //Get CM to obstacle in front of the sensor
{
  long duration;
  pinMode(fP, OUTPUT);
  digitalWrite(fP, LOW);
  delayMicroseconds(2);
  digitalWrite(fP, HIGH);
  delayMicroseconds(15);
  digitalWrite(fP, LOW);
  delayMicroseconds(20);
  pinMode(fP, INPUT);
  duration = pulseIn(fP, HIGH);
  return duration / 29.0 / 2.0;
}


Then we just added more leds and added distances when the led's should be active:



I forgot to shoot my own video, but my friend took one and here is how it works.



//#define fP 7 //Pin the ping sensor is connected to
const int pingPin = 7; //set ping to slot 7

void setup()
{
  pinMode(4, OUTPUT); //
  pinMode(5, OUTPUT); //set led to slot 4,5,6
  pinMode(6, OUTPUT); //
}

void loop()
{
  if (ping() > 2) //if ping distance is bigger than #
{
  digitalWrite(4, HIGH); //set the LED on
}
 else if(ping() > 6)
{
  digitalWrite(5, HIGH);
}
  else if(ping() > 10)
{
  digitalWrite(6, HIGH);
}
else
{
  digitalWrite(4, LOW); //
  digitalWrite(5, LOW); //set the LED off
  digitalWrite(6, LOW); //
}
}
int ping() //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(15);
digitalWrite(pingPin, LOW);
delayMicroseconds(20);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return duration / 29.0 / 2.0;
}

maanantai 21. tammikuuta 2013

First week of prototype course

First task was to burn xubuntu live cd. I burned other cd's as well.



First lesson of prototype course was very impressive, all other courses are boring or I feel somehow stupid doing other courses. This was the first course this year when I felt I'm in the right place. Maybe later I will attend other Tero's courses if I have time.

The idea of the course is to build your own prototype and show it to others. Prototype is some kind of demoversion. Here are the "brains" called arduino in the picture.


First program or machine:

I installed Lubuntu in dualboot and then opened command prompt:

sudo apt-get update  (here i updated the software database)
sudo apt-get install arduino (this code installs the arduino environment)
newgrp dialout (adds dialout group which is needed in arduino)
arduino (starts the actual programming thing)

At first i downloaded Arduino "Hello World!" program which blinks led light in socket 13 with 1 second delay. It worked and so I was sure the developing environment worked. Then I installed three leds on the testing board and programmed them to blink in order, first led blinks 3 times, second 2 times and the last one of course once. It worked very well ! Here is a video how the system works:


And here is the actual code:

void setup() {            
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
}
void green(){
  digitalWrite(3, HIGH);
  delay(150);
  digitalWrite(3, LOW);
  delay(150);
}
void yellow(){
  digitalWrite(4, HIGH);
  delay(50);
  digitalWrite(4, LOW);
  delay(50);
}
void red(){
  digitalWrite(2, HIGH);
  delay(100);
  digitalWrite(2, LOW);
  delay(100);
}
void loop() {
  for (int i = 0; i < 3; i++){
  red();
  }
 for (int i = 0; i < 2; i++){
  green();
  }
  yellow();
}

Some screenshot:


Last task was to invent a system with uses some sensor. I couldn't think anything so i just developed basic alarm system. We could use a ultrasonic sensor or infrared if there is a movement in the house or some place and then send email or pictures with webcam to internet to see what is currently happening in the house.

How to setup the arduino environment:


$ setxkbmap fi
$ sudo apt-get update
$ sudo apt-get install arduino
$ sudo adduser xubuntu dialout
$ newgrp dialout
$ arduino