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.
I've bought a bunch of phototransistors and IR leds ...
/*Chip type : ATmega8Program type : ApplicationClock frequency : 8,000000 MHzMemory model : SmallExternal SRAM size : 0Data Stack size : 256 */#include <mega8.h>#include <delay.h>#define ADC_VREF_TYPE 0x40bit ADC_complete;int gADC_result;interrupt [ADC_INT] void adc_isr(void) {unsigned int adc_data;adc_data=ADCW;gADC_result=adc_data; ADC_complete=1;}int poll_sensor(void){ int delta;PORTB.0=1; delay_ms(1); ADCSRA|=0x40; while(ADC_complete!=1); delta=gADC_result;ADC_complete=0; PORTB.0=0; delay_ms(1);ADCSRA|=0x40;while(ADC_complete!=1);delta=gADC_result-delta;ADC_complete=0; return delta;}void main(void){int razn;PORTB=0x00;DDRB=0xFF;PORTC=0x00;DDRC=0x00;PORTD=0x00;DDRD=0xFF;TCCR0=0x00;TCNT0=0x00;TCCR1A=0x00;TCCR1B=0x00;TCNT1H=0x00;TCNT1L=0x00;ICR1H=0x00;ICR1L=0x00;OCR1AH=0x00;OCR1AL=0x00;OCR1BH=0x00;OCR1BL=0x00;ASSR=0x00;TCCR2=0x00;TCNT2=0x00;OCR2=0x00;MCUCR=0x00;TIMSK=0x00;ACSR=0x80;SFIOR=0x00;// ADC initialization// ADC Clock frequency: 1000,000 kHz// ADC Voltage Reference: AVCC pinADMUX=ADC_VREF_TYPE & 0xff;ADCSRA=0x8B;// Global enable interrupts#asm("sei")while (1) { razn=poll_sensor();// for exmple, turn different LEDs on portD depending of obstacle distance if(razn >10) PORTD.0=1; if(razn >20) PORTD.1=1; if(razn >30) PORTD.2=1; if(razn >40) PORTD.3=1; if(razn >50) PORTD.4=1; if(razn >60) PORTD.5=1; if(razn >70) PORTD.6=1; if(razn >80) PORTD.7=1; delay_ms(1); PORTD=0; };}