Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
#define F_CPU 8000000 // AVR clock frequency in Hz, used by util/delay.h#include <avr/io.h>#include <inttypes.h>#include <util/delay.h>#define output_low(port,pin) port &= ~(1<<pin)#define output_high(port,pin) port |= (1<<pin)int speed = 255;int main() { DDRD &= ~(1 << PD4); // Left bump switch PORTD |= _BV(PD4); // Enable internal pull up DDRD &= ~(1 << PD3); // Right bump switch PORTD |= _BV(PD3); // Enable internal pull up //Motor 1 DDRB |= (1<<PB2); // make PB2 an output DDRB |= (1<<PB0); // make PB0 an output //Motor 2 DDRB |= (1<<PB3); // make OC1A an output DDRD |= (1<<PD6); // make PD6 an output // init timers as fast PWM TCCR0A = (1 << WGM00) | (1 << WGM01); TCCR1A = (1 << WGM10) | (1 << WGM12); // set prescaler to 1 TCCR0B |= (1 << CS00); TCCR1B |= (1 << CS00); // set outputs to PWM TCCR0A |= (1 << COM0A1); TCCR1A |= (1 << COM1A1); TCCR1A |= (1 << COM1B1); int Left_Bump(); int Right_Bump(); Status(5); // Flash status LED while(1) { Forward(); if (Left_Bump()) { Reverse(); _delay_ms(1000); Right(); _delay_ms(1000); Forward(); } if (Right_Bump()) { Reverse(); _delay_ms(1000); Left(); _delay_ms(1000); Forward(); } } }int Right_Bump(){ if (bit_is_clear(PIND, PD4)) { _delay_ms(25); if (bit_is_clear(PIND, PD4)) return 1; } return 0;}int Left_Bump(){ if (bit_is_clear(PIND, PD3)) { _delay_ms(25); if (bit_is_clear(PIND, PD3)) return 1; } return 0;}int Stop(){ OCR0A = 0; //Lowest 190 OCR1A = 0;}int Left(){ OCR0A = speed; OCR1A = speed; output_high(PORTB, PB0); output_low(PORTD, PD6); }int Right(){ OCR0A = speed; OCR1A = speed; output_high(PORTB, PB0); output_low(PORTD, PD6);}int Forward(){ OCR0A = speed; OCR1A = speed; output_low(PORTB, PB0); output_low(PORTD, PD6); }int Reverse(){ OCR0A = speed; OCR1A = speed; output_high(PORTB, PB0); output_high(PORTD, PD6); }int Status(int flashes){ int i; for (i = 0; i < flashes; i++ ) { output_high(PORTB, PB1); _delay_ms(500); output_low(PORTB, PB1); _delay_ms(500); } }