Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
#include <SoR_Utils.h>#include <util/delay.h>#include <avr/interrupt.h>int motor1_1 = 0;int motor1_2 = 1;int motor2_1 = 2;int motor2_2 = 3; int sensor_left = 5;int sensor_right = 4;int background_left = 0;int background_right = 0;int reading_left = 0;int reading_right = 0;/* Go Forward */ void go_forward(){ PORT_ON(PORTD, motor1_1); //Port B pin 1 on PORT_OFF(PORTD, motor1_2); //Port B pin 2 off PORT_ON(PORTD, motor2_1); //Port B pin 3 on PORT_OFF(PORTD, motor2_2); //Port B pin 4 off}/* Turn Right*/void turn_right(){ PORT_OFF(PORTD, motor1_1); PORT_ON(PORTD, motor1_2); PORT_ON(PORTD, motor2_1); PORT_OFF(PORTD, motor2_2); }/* Auto Calibrate */void calibrate(){ background_left = a2dConvert8bit(sensor_left); //read value of space left of the line background_right = a2dConvert8bit(sensor_right); //read value of space right of the line}/* Turn Left */void turn_left(){ PORT_ON(PORTD, motor1_1); PORT_OFF(PORTD, motor1_2); PORT_OFF(PORTD, motor2_1); PORT_ON(PORTD, motor2_2);}int main(){ calibrate(); for (;;) { reading_left = a2dConvert8bit(sensor_left); //reading from left sensor reading_right = a2dConvert8bit(sensor_right); //reading from right sensor if (reading_left < background_left) { while(reading_left < background_left) { turn_left(); reading_left = a2dConvert8bit(sensor_left); } } else if (reading_right < background_right) { while(reading_right < background_right) { turn_right(); reading_right = a2dConvert8bit(sensor_right); } } else if (reading_right >= background_right) { go_forward(); } else if (reading_left >= background_left) { go_forward(); } }}
PORT_ON(PORTD, motor1_1); //Port B pin 1 onPORT_OFF(PORTD, motor1_2); //Port B pin 2 off
#include <util/delay.h>#include <SoR_Utils.h>int main(){ PORT_OFF(PORTD, 3); PORT_OFF(PORTD, 4); pwmInit56(); pwmOn5(); pwmOn6(); pwmSet6(255); pwmSet5(255); _delay_ms(5000); pwmOff56();}
Doesn't PORT_OFF set the pin low?
#include <util/delay.h>#include <SoR_Utils.h>int main(){ PORT_OFF(PORTD, 3); PORT_OFF(PORTD, 4); pwmInit56(); pwmOn5(); pwmOn6(); pwmSet6(255); pwmSet5(255); // Just keep looping with the PWM happening in the background while(true){ _delay_ms(5000); } pwmOff56();}
So when you say 'it doesnt work' - what else do you observe?
int main(){ while(1) { PORT_ON(PORTD, 1); PORT_OFF(PORTD, 2); }}
PORT_ON(PORTD, 1);PORT_OFF(PORTD, 2);while(1){//donothing}
Have you used a meter to check that your processor pins are the expected voltage - both at the output of the processor and at the input to the motor controller?
maybe only change the pins once and dont repeat over? No reason to put it in an infinite loop when it only needs to be done once
QuoteHave you used a meter to check that your processor pins are the expected voltage - both at the output of the processor and at the input to the motor controller?yep
PortD 2 is not low then for some stupid reason (checked with an LED). I know it's not a problem with my H-bridge, so it must be a problem with my Atmega168 (or my code).
DDRD |= _BV(PD1) | _BV(PD2);
Well, they are set as outputs in SoR_Utils.h. But i'll try it with your code.
It sorta works now (thanks chelmi!). The motor is extremely jerky and very slow.
You're problem now sounds like a PWM issue. try to simply connect the enable pin of your H-bridge to Vcc, you're motor should spin full speed (check that the max voltage of your motor is compatible with what you are supplying)