23 June 2013

Sunday 23rd June 2013 - Arduino projects

Over the last couple weeks I have become interested in Arduino projects. An Arduino at it's very basic is a simple input/output device which uses a very simply programming language. More information about the Arduino project can be found at http://www.arduino.cc/

The main advantage I have is that it appears that I can start small and get more complicated as I progress.

The Raspberry Pi I bought, even though it is a very versatile piece of kit, is just collecting dust. I haven't used it at all,even though Maison graciously programmed it for me for the XBMC. It still seems far too complicated for me to sit down and learn the programming. Just getting the software onto the SD card seemed quite convoluted.

I must admit at this point that I don't like computers as they always seem to go too slow and don't do what I want when I want. Yes, I can use them to communicate etc but other than that I don't know bupkiss.

Anyway back to the Arduino.

I initially bought a starter pack from SainSmart via Amazon. The link is here. There are other starter kits available which cost more/less or have less/more equipment in it.

Although it is possible to find programs (or sketches as they're known in Arduino circles) on the web I decided I'd try and be a bit more structured by using a '....for Dummies' book, as I've found them easy to use in the past.

I am working my way towards an automated garden water system including water towers, solar charged batteries and irrigation systems.

So far I have tried a few projects from the built in examples but generally try and augment them a little bit to test my understanding of the sketch.

Project #1 - LED output dependant on variable resister input.
Uses a variable resister (pot) to set a value which is read by the Arduino. Depending on the value set, the Arduino will drive 1 of three led's. For some reason the green led is not as obvious as the red/blue ones



Code after being augmented - '_20130622_Zippys_AnalogInOutSerial_3switch_led.ino'

c/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.

 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground

 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe

 modified 22nd June 2013 by Zippy : Added code for third LED.

 This example code is in the public domain.

 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin09 = 9; // Analog output pin that the LED is attached to
const int analogOutPin10 = 10; // Analog output pin that the LED is attached to
const int analogOutPin11 = 11; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin); // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); // scale the input range(0-1023) from the pot
 // to fit the output range (0-255) of the LED.
   if (outputValue <= 1) {
    analogWrite(analogOutPin09, 0); // with analogWrite you need to specify a number value between 0 and 255.
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 0);
  }
  if (outputValue > 1 && outputValue <;= 85) {
    analogWrite(analogOutPin09, 255);     
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 0);
  }
  if (outputValue >= 86 && outputValue <= 170) {
    analogWrite(analogOutPin09, 0);
    analogWrite(analogOutPin10, 255);
    analogWrite(analogOutPin11, 0);
  }
  if (outputValue >= 171 && outputValue <= 255) {
    analogWrite(analogOutPin09, 0);
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 255);
  }
  
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

  // wait 200 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(200);                     
}

¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
Project #2 - LED and buzzer output dependant on variable resister input.
Uses a variable resister (pot) to set a value which is read by the Arduino. Depending on the value set, the Arduino will drive 1 of three led's and a piezo buzzer.For some reason the green led is not as obvious as the red/blue ones



Code after being augmented - '_20130622_Zippys_AnalogInOutSerial_3switch_led_with_sound.ino'

/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.

 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground

 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe

modified 23rd June 2013 - To include 3 LED's and a piezo buzzer by Zippy
piezo buzzer positive connected to pin 8 on the Arduino. 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin09 = 9; // Analog output pin that the LED is attached to
const int analogOutPin10 = 10; // Analog output pin that the LED is attached to
const int analogOutPin11 = 11; // Analog output pin that the LED is attached to

int sensorValue = 0;        // set initial value read from the pot
int outputValue = 0;        // set initial value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); // scale the input range(0-1023) from the pot
 // to fit the output range (0-255) of the LED.
   if (outputValue <= 1) {
    analogWrite(analogOutPin09, 0);   // with analogWrite you need to specify a number value between 0 and 255.   
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 0);
    noTone(8); // cancel tone to pin 8
  }
  if (outputValue > 1 &&; outputValue <= 85) {
    analogWrite(analogOutPin09, 255);     
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 0);
    noTone(8); // cancel tone to pin 8
  }
  if (outputValue >= 86 && outputValue <= 170) {
    analogWrite(analogOutPin09, 0);
    analogWrite(analogOutPin10, 255);
    analogWrite(analogOutPin11, 0);
    tone(8,250); // tone(pin,freq[,optional duration]);
  }
  if (outputValue >= 171 && outputValue <= 255) {
    analogWrite(analogOutPin09, 0);
    analogWrite(analogOutPin10, 0);
    analogWrite(analogOutPin11, 255);
    tone(8,500); // tone(pin,freq[,optional duration]);
  }
  
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

  // wait 200 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(200);                     
}

¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
Project #3 - LED and buzzer output dependant on sonic range finder.
Uses a sonic range finder with the time delay being read by the Arduino. Depending on the value read, the Arduino will drive 1 of three led's and a piezo buzzer. Similar to two previous projects. It just uses a different input device.For some reason the green led is not as obvious as the red/blue ones


Code after being augmented - '_20130622_EchoSounder_3LED_with_sound.ino'

c/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 12 Trig to Arduino pin 13
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/

#define trigPin 13
#define echoPin 12
#define led1 9
#define led2 10
#define led3 11
// piezo buzzer positive attached to pin 6 on the Arduino

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance <= 5) {  // If the range is less than 5cm
    digitalWrite(led1,HIGH); // For digitalWrite the value has to be either HIGH or LOW.
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    Serial.println("RED");
    tone(6,600); // tone(pin,freq[,optional duration]);
  }
  if (distance >=5.5 && distance >=10) { // range is between 5.5 and 10cm
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
    Serial.println("BLUE");
    tone(6,200); // tone(pin[,optional duration]);
  }
  if (distance > 10 ){ // range is greater than 10cm
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,HIGH);
    Serial.println("GREEN");
    noTone(6); // cancel any tone sent to pin 6
  }    
  if (distance >= 5000 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(250);
}

¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
I have ordered some more components but will have to wait until I get back to FFA in July.

09 June 2013

Sunday 9th June 2013 - Bike ride to Oslo and back.

As is now normal for me I decided to go out on the bike at least one day during the weekend for an extended period. As I was recovering from a hangover yesterday brought about by Friday's BBQ it fell to today.

The initial plan was to just go to Sandvika and cycle around the Kalvøya Island and back again. Something a bit different than the usual scramble up and down muddy tracks in the hills.



As I came into the outskirts of Sandvika I thought it was going fairly well, the weather wasn't too hot nor too cold it seemed to be dry, so I decided to extend it all the way into Oslo.

Near the approach road to Kalvøya Island, by the Bærum Sports club, I noticed that there was a flea market under the E18 in the car park. I thought I had plenty of time so decided to give it a look. Nothing completely grabbed my fancy at the time. Nearly bought a hand-drill.

Getting around Sandvika and into Oslo was harder than it needed to be. The little brown signs showing the correct/easiest route were poorly positioned, hidden by overgrowing plants, or changed information.

At some junctions I couldn't see the sign until I had checked out each possibility with some of the signs being 50-100m down the correct road.

I had started following the signs which said 'Oslo E18' but these seemed to change to 'Oslo 1' around Sandvika without there being any notification of the change.

This not only slowed me down but also extended the route longer than it needed to be.



Had to stop a few times to verify the location, but thankfully I had the ViewRanger app on the phone helping me out, so I could always get back to the track if I strayed too far.

I got to Aker Brygge in Oslo and my knees were feeling the pain.



As I sat waiting to cool down a bit I did consider going back to Asker via the train. But obviously I didn't because the title sort of gives it away.

I did eventually pay quite a bit of money for an ice cream as it seemed the thing to do.

I wandered further down Aker Brygge where it looks like they're building some new swanky apartments and  took some pictures.










It was during this period that it started to show signs of rain.The sky wasn't too dark so I thought it was a passing shower.

On the way back to Asker, just as I was leaving Oslo, the passing showers got heavier and more frequent. I had to hide under a fly-over for a good 15mins at one point.

After that it seemed to hang off and it got hotter and stickier. On one of the climbs towards Sandvika I decided I'd stop and eat the orange I had in my pack, seeing as I don't normally like to have a large heavy snack when out cycling. I was really buggered when it wasn't actually there. Must be more careful about including snacks in future.

The majority of the ride back wasn't too bad.

It wasn't until the last hour when the climb back to Asker started. The height above sea level went from zero to about 580 feet in that time. It was a hard slog (for me anyhoo) in very low gear for the whole time. I was very glad to finally get back to the house.

Felt good to have actually done it though.

The data from ViewRanger is shown here.



The map of the track can be found here.
The