Sunday 20 October 2013

Dim & Dimmer


When I started to make this project I only had a few components at my disposal, two Potentiometer's, a single LED and some miscellaneous resistors. At first I used the example sketch AnalogReadSerial to print the value of a Potentiometer to a serial monitoring program on my PC. After seeing the Fade sketch that allows a LED to fade in and out. I got ideas from both pieces of code and then managed to write a compact and simple sketch allowing the brightness of an LED to be controlled by a Potentiometer.

// If any of the images are too small click once to enlarge.
// Sorry about the video quality, it was compressed to lower resolution after being uploaded.

Code:

Circuit Diagram:
Demonstration:

Saturday 12 October 2013

Arduino ISP(In System Programming)


Using arduino isp you can program a AVR. Avr is the heart of arduino, the arduino uno has a ATmega328p( the "p" stands for  pico power, less power and more effiency.) If you go ahead and open the arduino isp form file-->examples-->arduino isp.
If you scroll down you can see the pin configuration on your arduino and the Avr( note that your AVR pin configuration can differ). 

First you have to power the AVR using 5V and GND. the you have to connect the actual communication pins. MOSI( MASTER OUT SLAVE IN), MISO( MASTER IN SLAVE OUT), SCK(THE CLOCK) AND THE RESET. and you have to put 10uf capacitor to diasable auto reset.  Then Google "winavr" and install it.




Here is the AVR led blink. open and write this into programmers notepad. THIS PICTURE IS NOT MINE!!!










/***********************************************************************
make sure to attach a LED to + to PB0 and - to GND.
written and created by randipa. project 1.2

*************************************************************************/
#define F_CPU=8000000; // setting the cpu clock make sure to divide the crystal oscilator by 8;
#include <util/delay.h> // including the delay header
#include <avr/io.h> // including iostream to avr


int main(){
DDRB = 0xff; // data direction port b. using hex. 0xff= 0b11111111 setting all of it to output.
PORTB = 0; // setting port b to 0V.


  While(1){

      PORTB = 0;
      _delay_ms(100) // delay 100ms
      PORTB = 0xff;
      _delay_ms(100)  //if your are too lazy type PORTB^=0xff; _delay_ms(100);  this will xor which will invert.

 }
 retun 0;
}

save this in the c directory.
Goto CMD and "cd .." press enter "cd .." press enter again. And type "avrdude -c avrisp -p m328p -U flash:w:ledblink.hex"
(without any quotes) make sure use -p to your avr part.

Bootloader?

You might be wandering if you can program avr and arduino in one language. yes You can avr and arduino has a different language because of the bootloader. bootloader is a firmware(you can only write.) Bootloader is the piece of code which runs when the power was supplied. the bootloader recognise the code and do everything else. if you want to do this search google " bootloader avr arduino" Only do this if you know what you are doing(I tried this and bricked my ATmega8)

13107@csc.school.nz
11127@csc.school.nz

Friday 11 October 2013

Let there be ... Lights? (From Freetronics Project Guide Book)

Here is a sketch that makes a light "scan" along a row of LEDs, very similar to the Kitt scanner in the show Knight Rider or the red Cyclon eye in Battlestar Galaticica.

Here is the sketch:


It is basically the beginner's sketch "Blink", but with  8 LEDs.


// Project II, Controlling 8 LEDs

int ledCount = 8;
int ledPins [] = { 6, 7, 8, 9,  10,  11, 12, 13};
int ledDelay = 100
;

// the setup routine runs once when you press reset:
void setup() {                
 for (int thisLed = 0; thisLed < ledCount; thisLed++) {
   pinMode (ledPins[thisLed], OUTPUT);
 }
}

   
   void loop () {
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      digitalWrite (ledPins [thisLed], HIGH) ;
      delay(ledDelay);
      digitalWrite (ledPins [thisLed], LOW) ;
    }
    for (int thisLed = ledCount-1; thisLed > 0; thisLed--)  {
      digitalWrite (ledPins[thisLed], HIGH) ;
      delay(ledDelay);
      digitalWrite (ledPins [thisLed], LOW) ;
      }
    }

In this code, instead of writing the instructions for each LED, we use a 'For' loop, which runs pinMode( ) eight times but with a different value each time.  

The Next part tells the Arduino when to stop the 'For' loop.  This will keep repeating until the conditions are not met, so for this example until the value of 'ledPin' is greater than the value of 'ledCount', so in this case 'ledCount' is 8 so it will keep going up until until ledPin reaches 8.  

The last part is an action that the loop will do each time it runs.  The '++' is a shorthand or quicker way to take a variable (in this case thisLed) and add one to it each time.  


Once the first loop finishes, (in this case thisLed reaches the same value ledCount), the second loop begins.  The second loop is almost identical to the first, but it goes backwards, it starts at the value of 'thisLed' and each time it runs through, it uses the '--' action to decrease the the value of the position by one each time.  

When it reaches the start of the list, the second loop finishes and the program jumps back to the first loop to do it all again.   


Thanks for Reading,

Logan Cox

12044@csc.school.nz or
loggie374@gmail.com  

Tuesday 8 October 2013

Flow Charts to help work out your sketch...

We have unlocked LucidChart on CSC Google Apps Domain. This means that you can now create a LucidChart diagram as an option through Google Drive. This is possible through your school Google Apps account.
Flow charts are an excellent way to iron out the different stages that you would like in your program or "Sketch" in Arduino. Flow charting your sketch also helps you to communicate the purpose of your sketch to other people.
Here is an example of a very simple flow chart, created using LucidChart...

Friday 4 October 2013

Program Structure - Functions

When writing code there are almost always several ways to do things, but one thing that most programmers try to achieve is to get their code compact and to use as little space as they can.
This code is available on our File Cabinet page, or through a direct link here...

All of the comments to explain the code are in the sketch and follow these signs //
This mean that the Arduino does not read anything after the two symbols:
// The arduino recognises this as a comment.

It is very simple to set up...
This diagram was produced using Fritzing. A link to a download for Fritzing is available on our File Cabinet page. Note that the positive power rail at the bottom is not needed, but it is good practise to include it in case your project develops and needs use of it.

Here  is the sketch...
___________________________________________________
/*
This Sketch shows two useful concepts in writing sketches for Arduino.
Firstly it demonstrates how to use FUNCTIONS and then call them in the sketch.
It also demonstrates how to use a WHILE loop, using a count to add to a
predefined variable
 */

// Give Pin 13 a name...
int led = 13;
int count = 0;

// the setup routine runs once only, when you press reset:
void setup() {              
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  Reset();        // Call the "Reset" function declared at the bottom of this sketch
  SlowBLINK();    // Call the "SlowBLINK" function declared at the bottom of this sketch
  Reset();
  MediumBLINK();  // Call the "MediumBLINK" function declared at the bottom of this sketch
  Reset();
  FastBLINK();    // Call the "FastBLINK" function declared at the bottom of this sketch

}

void SlowBLINK()    // Funtion named "SlowBlink"
{
while(count < 5)             // Repeat whilst the value of "count" is less than 5
  {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
count++;                     // add "1" to the value of "count"
  }
}
void MediumBLINK()    // Funtion named "MediumBlink"
{
while(count < 10)            // Repeat whilst the value of "count" is less than 10
  {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                // wait for 500ms
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                // wait for 500ms
count++;                     // add "1" to the value of "count"
  }
}
void FastBLINK()    // Funtion named "FastBlink"
{
while(count < 50)            // Repeat whilst the value of "count" is less than 10
  {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                // wait for 100ms
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                // wait for 100ms
count++;                     // add "1" to the value of "count"
  }
}
void Reset()        // Funtion named "Reset"
  {
  count = 0;                 // Reset the value of the variable "count" to "0"
                             // this needs to be done to make the "while" functions work.
  }



Thursday 3 October 2013

A New Look for Arduino "CC"

It's good to see some of you posting on our blog, great links from Brodie, and some good work by Janitha and Randipa on controlling an LED using their Arduino.

Whilst looking at some of Brodie's links, I noticed that the Arduino Creative Cloud Web Site has a new look to it. It's well worth a look...
http://www.arduino.cc

Don't forget that you all have (Or can request) permissions to be able to save links to our FILE CABINET on our CSC Arduino Development Group Web Site. We can then keep all manner of files and links that you submit together in one place.