-
Notifications
You must be signed in to change notification settings - Fork 2
/
hardware_arduino.cpp
106 lines (82 loc) · 1.99 KB
/
hardware_arduino.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <Arduino.h>
#include "hardware_arduino.h"
ArduinoInterface::ArduinoInterface() {
this->setupPins();
this->stopMotors();
};
void ArduinoInterface::setupPins() {
// LED Pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Motor Pins
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(ampSDNpin, OUTPUT); //for turning on/off amp
digitalWrite(ampSDNpin, LOW); //turn amp off at startup
}
/************* LED COLOR CONTROL FUNCTIONS ************/
void ArduinoInterface::writeToLED(byte red, byte green, byte blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
/**************DC MOTOR CONTROL FUNCTIONS***************/
void ArduinoInterface::moveMotor(int motor, int motorSpeed, int motorDirection)
{
//Move specific motor at speed and direction
//motor: 1 for B, 0 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(motorDirection == 1)
{
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 0)
{
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, motorSpeed);
}
else
{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, motorSpeed);
}
}
void ArduinoInterface::stopMotors()
{
//enable standby
digitalWrite(STBY, LOW);
}
int ArduinoInterface::readBattery()
{
return analogRead(A0);
}
void ArduinoInterface::playTone(unsigned int freq) {
tone(tonePin, freq);
}
void ArduinoInterface::toneOff() {
noTone(tonePin);
}
bool ArduinoInterface::ampIsOn() {
return digitalRead(ampSDNpin) == HIGH;
}
void ArduinoInterface::setAmpPower(bool on) {
if (on) {
digitalWrite(ampSDNpin, HIGH);
} else {
digitalWrite(ampSDNpin, LOW);
}
}