-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (94 loc) · 2.14 KB
/
main.cpp
File metadata and controls
125 lines (94 loc) · 2.14 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
#include<TimerOne.h>
//Hi masami was here
const int dir_a = 12; // pin motor A direction
const int pwn_a = 3; // pin motor A output
const int dir_b = 13; // pin motor b direction
const int pwn_b = 11; // pin motor B output
const int s = 14; // pin for switch
const int IRpin[4] = {1, 2, 3, 4}; //pin for IR sensor
const int d[4] = {10, 9 , 8, 3}; // pin for 7-segment display
int i;
int pv[4]; // value read from IR sensor
boolean sw = 0;
void show_status(int c); // show 0 1 2 3, depends on current movement
void pc( int a[]);
int tcounter = 0; // timer counter
// function ti countes time.
void ti(){
tcounter++;
}
void setup(){
Serial.begin(9600);
pinMode(dir_a, OUTPUT);
pinMode(pwn_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(pwn_b, OUTPUT);
pinMode(d[0], OUTPUT);
pinMode(d[1], OUTPUT);
pinMode(d[2], OUTPUT);
pinMode(d[3], OUTPUT);
pinMode(s, INPUT_PULLUP);
Timer1.initialize(1000);
Timer1.attachInterrupt(ti);
digitalWrite(dir_a, 1);
analogWrite(pwn_a, 255);
digitalWrite(dir_b, 1);
analogWrite(pwn_b, 255);
while(sw == 0)
{
sw = !digitalRead(s);
for(i = 0; i < 4; i++){
pv[i] = analogRead(IRpin[i]);
}
}
}
void loop(){
if(tcounter > 10) // read value every 10 ms
{
tcounter = 0;
// read IR sensor value and put into pv[4]
for(i = 0; i < 4; i++){
pv[i] = analogRead(IRpin[i]);
}
pc(pv); // function pc controls the movement of 2 motors based on array pv[4]
}
for(i = 0; i < 4; i++ ){
Serial.println(pv[i]);
}
Serial.println(" ");
}
void show_status( int c ){
int b[4] = {0, 0, 0, 0};
switch(c){
case 0:
b[0] = 0;
b[1] = 0;
b[2] = 0;
b[3] = 0;
break;
case 1:
b[0] = 1;
b[1] = 0;
b[2] = 0;
b[3] = 0;
break;
case 2:
b[0] = 0;
b[1] = 1;
b[2] = 0;
b[3] = 0;
break;
case 3:
b[0] = 1;
b[1] = 1;
b[2] = 0;
b[3] = 0;
break;
}
for(i = 0; i < 4; i++){
digitalWrite(d[i], b[i]);
}
}
// pc controls motor speed based on array input.
void pc(int a[]){
}