Useless Machine……part three

Posted on December 12, 2012 | Category :All, Micro, Projects | No Comments on Useless Machine……part three

< Useless Machine Part two

Code for the useless machine:  download source file

You can download the file above or you can copy and paste from the file below.


//##########################################################################
// # Useless Machine                                                        #
// #                                                                        #
// # 14 July 2012                                                           #
// # Coded for the AvrTiny85                                                #
// ##########################################################################

#include &lt;avr/io.h&gt;                            //#
#include &lt;util/delay.h&gt;                        //#

#define PWR_PIN PB3                            //#
#define SERVO PB0                            //#
#define pwr_on PORTB |= (1&lt;&lt;PWR_PIN)        //# Pwr on
#define pwr_off PORTB &amp;= ~(1&lt;&lt;PWR_PIN)        //# Pwr off
#define SERVO_LIMIT_L 247                    //#
#define SERVO_LIMIT_U 253                    //#
#define L_DELAY 5

inline void setPWM0A(uint8_t num) { OCR0A = num; }    //#

// # Initialise PWM #########################################################
void initPWM0(){
TCCR0A |= ((1 &lt;&lt; COM0A1) | (1 &lt;&lt; COM0B1) | (1 &lt;&lt; WGM01) | (1 &lt;&lt; WGM00));
TCCR0B |= ((1 &lt;&lt; CS02));       //# This turns the the 8 bit counter 0 on and
//# sets the pre-scaler to 256 (305 Hz)
}

// # Setup I/O ##############################################################
void setup() {
DDRB |= (1 &lt;&lt; PWR_PIN);                    //#    Set LED_B to output
DDRB |= (1 &lt;&lt; SERVO);                     //# Set OC0B to output
initPWM0();                                //# Setup pwm
}

// # Main Loop ##############################################################
int main( void ){
uint8_t sweep=0;                        //# Set anticlockwise sweep
uint8_t complete=0;                        //# Set NOT complete
uint8_t pwmASetting = SERVO_LIMIT_U;    //# Set current position marker to max clockwise
uint8_t pwmBSetting;                    //# Used for position calculation

setup();

pwr_on;                                    //# Pwr relay on

while(!complete) {                        // # If servo move is not complete
if (sweep==0) {                            // # Start anti-clockwise sweep
if ( pwmASetting == SERVO_LIMIT_L )    {//# Is servo at max anticlockwise
sweep=1;                        //#     If max change direction
_delay_ms(140);                    //#     Pause for 100 ms
}
else {                                //#        If not at max anticlockwise
pwmASetting = pwmASetting - 1;    //#        move 1 tick anticlockwise
pwmBSetting = 255 - (pwmASetting); //#    Claculate servo setting
setPWM0A(pwmBSetting);            //#        set servo
_delay_ms(L_DELAY);                //#        Pause for 10 ms
}
}
else {                                    //# Start clockwise sweep
if ( pwmASetting == SERVO_LIMIT_U )    {//# Is servo at max clockwise
sweep=0;                        //#        change direction
_delay_ms(140);                    //#        delay 1 second
complete=1;                        //#        set completion flag
}
else {                                //#        if not at max clockwise
pwmASetting += 1;                //#        move 1 tick clockwise
pwmBSetting = 255 - (pwmASetting); //#    Calculate servo setting
setPWM0A(pwmBSetting);            //#        set servo
_delay_ms(L_DELAY);                //#        Pause for 10 ms
}
}
}
pwr_off;                                //# Pwr relay off
while(1) {                                //# Catcher loop
}                                    //# Do nothing when finished servo moves (Power is turned off by now anyway....
}

Comments 0

Leave a Reply

Your email address will not be published. Required fields are marked *

*