Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
project/project.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (122 sloc)
3.71 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project | |
# my mbot project | |
#that my simple code of mbot project which avoids obsticles | |
{ include <Arduino.h>; | |
include <Wire.h>; | |
include <SoftwareSerial.h>; | |
include <MeAuriga.h>; | |
MeUltrasonicSensor ultrasonic_10(10); | |
MeEncoderOnBoard Encoder_1(SLOT1); | |
MeEncoderOnBoard Encoder_2(SLOT2); | |
void isr_process_encoder1(void) | |
{ | |
if(digitalRead(Encoder_1.getPortB()) == 0){ | |
Encoder_1.pulsePosMinus(); | |
}else{ | |
Encoder_1.pulsePosPlus(); | |
} | |
} | |
void isr_process_encoder2(void) | |
{ | |
if(digitalRead(Encoder_2.getPortB()) == 0){ | |
Encoder_2.pulsePosMinus(); | |
}else{ | |
Encoder_2.pulsePosPlus(); | |
} | |
} | |
void move(int direction, int speed) | |
{ | |
int leftSpeed = 0; | |
int rightSpeed = 0; | |
if(direction == 1){ | |
leftSpeed = -speed; | |
rightSpeed = speed; | |
}else if(direction == 2){ | |
leftSpeed = speed; | |
rightSpeed = -speed; | |
}else if(direction == 3){ | |
leftSpeed = -speed; | |
rightSpeed = -speed; | |
}else if(direction == 4){ | |
leftSpeed = speed; | |
rightSpeed = speed; | |
} | |
Encoder_1.setTarPWM(leftSpeed); | |
Encoder_2.setTarPWM(rightSpeed); | |
} | |
MeRGBLed rgbled_0(0, 12); | |
MeGyro gyro_0(0, 0x69); | |
void _loop(){ | |
Encoder_1.loop(); | |
Encoder_2.loop(); | |
gyro_0.update(); | |
} | |
void _delay(float seconds){ | |
long endTime = millis() + seconds * 1000; | |
while(millis() < endTime)_loop(); | |
} | |
void setup(){ | |
TCCR1A = _BV(WGM10); | |
TCCR1B = _BV(CS11) | _BV(WGM12); | |
TCCR2A = _BV(WGM21) | _BV(WGM20); | |
TCCR2B = _BV(CS21); | |
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING); | |
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING); | |
rgbled_0.setpin(44); | |
gyro_0.begin(); | |
} | |
void loop(){ | |
if(ultrasonic_10.distanceCm() > 30){ | |
move(1,50/100.0*255); | |
rgbled_0.setColor(1,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(2,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(3,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(4,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(5,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(6,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(7,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(8,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(9,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(10,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(11,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
_delay(0.01); | |
rgbled_0.setColor(12,random(1, 255 +1),random(1, 255 +1),random(1, 255 +1)); | |
rgbled_0.show(); | |
}else{ | |
move(2,50/100.0*255); | |
rgbled_0.setColor(0,#ff0000); | |
rgbled_0.show(); | |
move(3,50/100.0*255); | |
_delay(1); | |
move(3,0); | |
} | |
if(gyro_0.getAngle(1) > 20){ | |
Encoder_1.setTarPWM(0); | |
Encoder_2.setTarPWM(0); | |
rgbled_0.setColor(0,#ff0000); | |
rgbled_0.show(); | |
} | |
_loop(); | |
} | |
} |