-
Notifications
You must be signed in to change notification settings - Fork 1
/
Servo_and_moter_test.ino
113 lines (85 loc) · 2.78 KB
/
Servo_and_moter_test.ino
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
105
106
107
108
109
110
111
112
113
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 150
#define SERVOMAX 600
#define USMIN 600
#define USMAX 2400
#define SERVO_FREQ 60
// our servo # counter
uint8_t servonum = 2 ;
#define XP1MOTER1 2 // 6
#define XP2MOTER1 3 // 3
#define XP1MOTER2 4 // 6
#define XP2MOTER2 5 // 3
#define XP1MOTER3 6 // 6
#define XP2MOTER3 7 // 3
#define XP1MOTER4 8 // 6
#define XP2MOTER4 9 // 3
void setup() {
Serial.begin(9600);
Serial.println("Servo and moter test!");
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
pinMode(XP1MOTER1, OUTPUT);
pinMode(XP2MOTER1, OUTPUT);
pinMode(XP1MOTER2, OUTPUT);
pinMode(XP2MOTER2, OUTPUT);
pinMode(XP1MOTER3, OUTPUT);
pinMode(XP2MOTER3, OUTPUT);
pinMode(XP1MOTER4, OUTPUT);
pinMode(XP2MOTER4, OUTPUT);
delay(500);
}
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
int tempReading = analogRead(0);
double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK ); // Temp Kelvin
float tempC = tempK - 273.15; // Convert Kelvin to Celcius
float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
Serial.println("=============== Thermometer ===============");
Serial.print("Temperature in Celcius: ");
Serial.print(tempC);
Serial.println();
Serial.print("Temperature in Fahrenheit: ");
Serial.print(tempF);
Serial.println();
Serial.println("============= End Thermometer =============");
for (size_t i = 0; i < 100; i++)
{
analogWrite(XP1MOTER1, 255); // HI :)
analogWrite(XP2MOTER1, LOW);
analogWrite(XP1MOTER2, 255); // HI :)
analogWrite(XP2MOTER2, LOW);
analogWrite(XP1MOTER3, 255); // HI :)
analogWrite(XP2MOTER3, LOW);
analogWrite(XP1MOTER4, 255); // HI :)
analogWrite(XP2MOTER4, LOW);
delay(20);
}
for (size_t i = 0; i < 100; i++)
{
analogWrite(XP1MOTER1, LOW); // HI :)
analogWrite(XP2MOTER1, 255);
analogWrite(XP1MOTER2, LOW); // HI :)
analogWrite(XP2MOTER2, 255);
analogWrite(XP1MOTER3, LOW); // HI :)
analogWrite(XP2MOTER3, 255);
analogWrite(XP1MOTER4, LOW); // HI :)
analogWrite(XP2MOTER4, 255);
delay(20);
}
}