Wednesday, 12 February 2014

Bicycle flashlight #2 - Software

In the second phase of the bicycle flashlight project I'll describe the software loaded on the micro controller.
The program is writen and compiled in microPascal, and the machine code is loaded on the controller with AVRDude.
All the text small font size and dotted, is part of the code and part of one single program file in the sequence they appear in this article.

1) The first part of the program is a description, containing title, revision, author, date, etc.
  • // Project Bike_lamp, revision 01, Gaal Alexandru, 09.01.2014
  • // Bike flashlight project with 7 ligthing modes
  • // Mode 1: Police light
  • // Mode 2: Blinking red light
  • // Mode 3: Constant red light
  • // Mode 4: Blinking white light
  • // Mode 5: Constant white light
  • // Mode 6: Blinking blue light
  • // Mode 7: Constant blue light
All the text that is after the "//" characters are considered comments.




2) The second part of the program is initializing the variables, and setting the Input / Output ports of the micro controller.


  • program Bike_lamp;

  • var
  •    mode: integer;
  •    
  • begin
  • mode:=0; // mode selection counter

  • // defining outputs, ddrx.y:=1 means port pxy is an output
  • ddrc.0:=1; // red led 1 - refers to port pc0
  • ddrc.1:=1; // red led 2 - refers to port pc1
  • ddrc.2:=1; // red led 3 - refers to port pc2
  • ddrc.3:=1; // white led 1 - refers to port pc3
  • ddrc.4:=1; // white led 2 - refers to port pc4
  • ddrc.5:=1; // white led 3 - refers to port pc5
  • ddrd.0:=1; // blue led 1 - refers to port pd0
  • ddrd.1:=1; // blue led 2 - refers to port pd1
  • ddrd.2:=1; // blue led 3 - refers to port pd2

  • // defining inputs, ddrx.y:=0 means port pxy is an input
  • ddrb.0:=0; // switch - refers to port pb0
  • portb.0:=1; // "1" refers to internal pull up resistor



3) The third part of the program is an endless loop, that tracks the number of times the switch has been pressed, and runs an algorithm accordingly. The variable mode is tracking the number of bottom presses and every time it reaches seven, it resets and turns of all the leds.
  • repeat

  • if pinb.0=0 then  // This statement will change the mode
  •    begin
  •    mode:=mode+1;
  •    delay_ms(500); //wait for 0.5 seconds before going forward
  •    end;

  • //portx.y:= 0 will turn of  port pxy (will switch it to low state)
  • //portx.y:=1 will turn on port pxy (will switch it to high state)

  • if mode > 7 then // This statement will reset modes and will turn all leds off
  •    begin
  •    mode:=0;
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    end;

  • if mode = 1 then  // This mode will imitate police lights
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portd.0:=1;delay_ms(66);portd.0:=0;
  •    portd.1:=1;delay_ms(33);portd.1:=0;
  •    portd.2:=1;delay_ms(33);portd.2:=0;
  •    portc.5:=1;delay_ms(33);portc.5:=0;
  •    portc.4:=1;delay_ms(33);portc.4:=0;
  •    portc.3:=1;delay_ms(33);portc.3:=0;
  •    portc.2:=1;delay_ms(33);portc.2:=0;
  •    portc.1:=1;delay_ms(33);portc.1:=0;
  •    portc.0:=1;delay_ms(66);portc.0:=0; // Middle
  •    portc.1:=1;delay_ms(33);portc.1:=0;
  •    portc.2:=1;delay_ms(33);portc.2:=0;
  •    portc.3:=1;delay_ms(33);portc.3:=0;
  •    portc.4:=1;delay_ms(33);portc.4:=0;
  •    portc.5:=1;delay_ms(33);portc.5:=0;
  •    portd.2:=1;delay_ms(33);portd.2:=0;
  •    portd.1:=1;delay_ms(33);portd.1:=0;
  •     end;

  • if mode = 2 then  // This mode will blink red leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portc.0:=1; 
  •    portc.1:=1;
  •    portc.2:=1;
  •    delay_ms(150);
  •    portc.0:=0; portc.1:=0; portc.2:=0;
  •    delay_ms(150);
  •    end;

  • if mode = 3 then  // This mode will turn on red leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portc.0:=1;
  •    portc.1:=1;
  •    portc.2:=1;
  •    end;

  • if mode = 4 then  // This mode will blink white leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portc.3:=1;
  •    portc.4:=1;
  •    portc.5:=1;
  •    delay_ms(150);
  •    portc.3:=0; portc.4:=0; portc.5:=0;
  •    delay_ms(150);
  •    end;

  • if mode = 5 then  // This mode will turn on white leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portc.3:=1;
  •    portc.4:=1;
  •    portc.5:=1;
  •    end;

  • if mode = 6 then  // This mode will blink blue leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portd.0:=1;
  •    portd.1:=1;
  •    portd.2:=1;
  •    delay_ms(150);
  •    portd.0:=0; portd.1:=0; portd.2:=0;
  •    delay_ms(150);
  •    end;

  • if mode = 7 then  // This mode will turn on blue leds
  •    begin
  •    portd.0:=0; portd.1:=0; portd.2:=0; portc.0:=0; portc.1:=0; portc.2:=0; portc.3:=0; portc.4:=0; portc.5:=0;
  •    portd.0:=1;
  •    portd.1:=1;
  •    portd.2:=1;
  •    end;

  • until 1<>1; // condition that is never true, so the repeat loops last forever, or at least until power is cut off.

  • end.

Bellow you can see a video with the working prototype. the software is already loaded on the controller.
In the next article, I'll write about moving and soldering all the components from the breadboard to an actual board, connecting the battery, and finishing the electrical part of the project.

No comments:

Post a Comment