Skip to content

Instantly share code, notes, and snippets.

@cpjobling
Last active November 27, 2020 14:09
Show Gist options
  • Save cpjobling/3793729b96ce062a69cc57df936c89c9 to your computer and use it in GitHub Desktop.
Save cpjobling/3793729b96ce062a69cc57df936c89c9 to your computer and use it in GitHub Desktop.
Micro Mouse motor simulation programme for Arduino Nano. Written by Dr Timothy Davies for EG-151 Microcontrollers, (c) Swansea University 2020.
// the setup function runs once when you press reset or power the board
void setup()
{
Serial.begin(9600);
Serial.println();
Serial.println("Line sensor test programme");
}
// the loop function runs over and over again forever
void loop()
{
Serial.println();
Serial.print(" Left sensor = "); Serial.print(analogRead(A7));
Serial.print(" Right sensor = "); Serial.print(analogRead(A6));
delay(500);
}
// the setup function runs once when you press reset or power the board
void setup() {
#define TBL 11 //Actually Port B3
#define TBR 8 //Actually Port B0
pinMode(TBL, INPUT_PULLUP);
pinMode(TBR, INPUT_PULLUP);
DDRD = 0b11110000; //Use top four bits for motor control
PORTD = 0b00000000; //Machine is stopped
}
#define drive PORTD
#define STOP 0b00000000
#define FWD 0b01010000
#define REV 0b10100000
#define CW 0b01100000
#define ACW 0b10010000
#define touched 0
// the loop function runs over and over again forever
void loop() {
drive = FWD;
if (digitalRead(TBR) == touched) {
revleft();
}
if (digitalRead(TBL) == touched) {
revright();
}
}
void Stop() {
drive = STOP;
}
void Forward() {
drive = FWD;
}
void Reverse() {
drive = REV;
}
void Clockwise() {
drive = CW;
}
void Anticlockwise() {
drive = ACW;
}
void revleft() {
Stop();
Reverse();
delay(2000);
Stop();
Anticlockwise();
delay(1000);
Stop();
}
void revright() {
Stop();
Reverse();
delay (2000);
Stop();
Clockwise();
delay(1000);
Stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment