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?
Mbot-Ranger-sumo/RangerCode.txt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
136 lines (125 sloc)
4.02 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
#include <Arduino.h>; | |
#include <Wire.h>; | |
#include <SoftwareSerial.h>; | |
#include <MeAuriga.h>; | |
float distance = 0; | |
MeRGBLed rgbled_0(0, 12); | |
MeLineFollower linefollower_9(9); | |
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); | |
} | |
MeBuzzer buzzer; | |
MeUltrasonicSensor ultrasonic_10(10); | |
void _loop(){ | |
Encoder_1.loop(); | |
Encoder_2.loop(); | |
} | |
void _delay(float seconds){ | |
long endTime = millis() + seconds * 1000; | |
while(millis() < endTime)_loop(); | |
} | |
void setup(){ | |
rgbled_0.setpin(44); | |
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); | |
buzzer.setpin(45); | |
} | |
void loop(){ | |
rgbled_0.setColor(3,#3bff00); | |
rgbled_0.show(); | |
if(1?(3==0?linefollower_9.readSensors()==0:(linefollower_9.readSensors() & 3)==3):(3==0?linefollower_9.readSensors()==3:(linefollower_9.readSensors() & 3)==0)){ | |
move(1,100/100.0*255); | |
buzzer.tone(65, 0.25>0?0.25*1000:0); | |
delay(20); | |
} | |
if(0?(2==0?linefollower_9.readSensors()==0:(linefollower_9.readSensors() & 2)==2):(2==0?linefollower_9.readSensors()==3:(linefollower_9.readSensors() & 2)==0)){ | |
rgbled_0.setColor(4,#0400ff); | |
rgbled_0.show(); | |
Encoder_1.setTarPWM(0); | |
Encoder_2.setTarPWM(0); | |
move(4,100/100.0*255); | |
_delay(1.5); | |
move(4,0); | |
buzzer.tone(110, 0.25>0?0.25*1000:0); | |
delay(20); | |
} | |
if(0?(1==0?linefollower_9.readSensors()==0:(linefollower_9.readSensors() & 1)==1):(1==0?linefollower_9.readSensors()==3:(linefollower_9.readSensors() & 1)==0)){ | |
rgbled_0.setColor(4,#ff006e); | |
rgbled_0.show(); | |
Encoder_1.setTarPWM(0); | |
Encoder_2.setTarPWM(0); | |
move(3,100/100.0*255); | |
_delay(1.5); | |
move(3,0); | |
buzzer.tone(196, 0.25>0?0.25*1000:0); | |
delay(20); | |
if(1?(0==0?linefollower_9.readSensors()==0:(linefollower_9.readSensors() & 0)==0):(0==0?linefollower_9.readSensors()==3:(linefollower_9.readSensors() & 0)==0)){ | |
rgbled_0.setColor(6,#ffc300); | |
rgbled_0.show(); | |
Encoder_1.setTarPWM(0); | |
Encoder_2.setTarPWM(0); | |
move(2,100/100.0*255); | |
_delay(1.5); | |
move(2,0); | |
move(4,100/100.0*255); | |
_delay(1.5); | |
move(4,0); | |
buzzer.tone(4699, 0.25>0?0.25*1000:0); | |
delay(20); | |
} | |
distance = ultrasonic_10.distanceCm(); | |
if(ultrasonic_10.distanceCm() < 15){ | |
rgbled_0.setColor(0,255,0,0); | |
rgbled_0.show(); | |
buzzer.tone(123, 0.75>0?0.75*1000:0); | |
delay(20); | |
move(1,100/100.0*255); | |
_delay(1); | |
move(1,0); | |
rgbled_0.setColor(0,0,0,0); | |
rgbled_0.show(); | |
Encoder_1.setTarPWM(0); | |
Encoder_2.setTarPWM(0); | |
} | |
} | |
_loop(); | |
} |