Skip to content

Commit 9bb0fd2

Browse files
authoredJul 29, 2022
Add files via upload
1 parent 525c9e4 commit 9bb0fd2

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
 

‎buoy_with_motor/buoy_with_motor.ino

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
///Motor Pins
2+
#define outputA 13
3+
#define outputB 12
4+
5+
//Encoder
6+
#define encoder 0
7+
#define encoderRegLength 5
8+
int encoderRegister[encoderRegLength]; // <----------------------Shift register
9+
int encoderRegSum = 0; // <-------------/
10+
int encoderVal = 5; // <----------depth of buoy in rotations
11+
int dir = 1; //direction of motor (positive means going into water)
12+
13+
#define stopper 22
14+
#define stopperRegLength 10
15+
int stopperRegister[stopperRegLength]; // <----------------------Shift register
16+
int stopperRegSum = 0; // <-------------/
17+
int stopperActive = 0; //<----------will be 1 when buoy is at top
18+
19+
int state = 2;
20+
int maxDepth = 5; //33
21+
#define deploy 14
22+
23+
void t() {
24+
Serial.print("\t");
25+
}
26+
27+
void up() {
28+
digitalWrite(outputA, HIGH);
29+
digitalWrite(outputB, LOW);
30+
Serial.println("UP_____________");
31+
}
32+
33+
void down() {
34+
digitalWrite(outputB, HIGH);
35+
digitalWrite(outputA, LOW);
36+
Serial.println("DOWN_______________");
37+
}
38+
39+
void stopMotor() {
40+
digitalWrite(outputA, LOW);
41+
digitalWrite(outputB, LOW);
42+
Serial.println("STOP_______________");
43+
44+
}
45+
46+
47+
void setup() {
48+
// put your setup code here, to run once:
49+
Serial.begin(9600);
50+
Serial.print("Serial is now up");
51+
//motor
52+
pinMode(outputA, OUTPUT);
53+
pinMode(outputB, OUTPUT);
54+
55+
pinMode(encoder, INPUT_PULLUP);
56+
pinMode(stopper, INPUT_PULLUP);
57+
58+
pinMode(deploy, INPUT_PULLUP);
59+
60+
}
61+
62+
void loop() {
63+
//Telling the motors to stop
64+
65+
66+
// // put your main code here, to run repeatedly:
67+
// Serial.print(digitalRead(13));
68+
// Serial.print('\t');
69+
// Serial.print(digitalRead(encoderPin));
70+
// Serial.print('\t');
71+
// Serial.print(digitalRead(button));
72+
// Serial.print('\t');
73+
// Serial.println(digitalRead(button1));
74+
// Serial.print('\t');
75+
// Serial.println(digitalRead(12));
76+
//
77+
//
78+
79+
80+
//---------------------------------------------------------------------------------------------encoder shift register
81+
encoderRegSum = 0;
82+
for ( int index = 0; index < encoderRegLength - 1; index++) {
83+
encoderRegister[index] = encoderRegister[index + 1];
84+
encoderRegSum += encoderRegister[index];
85+
}
86+
encoderRegister[encoderRegLength - 1] = digitalRead(encoder);
87+
88+
if ( encoderRegSum == 1 && encoderRegister[0] == HIGH) { // falling edge (from high to low) (default is high ie. pullup)
89+
encoderVal += dir;
90+
}
91+
//-----------------------------------------------------------------------------------------------
92+
93+
94+
95+
96+
97+
98+
//---------------------------------------------------------------------------------------------stopper shift register
99+
stopperRegSum = 0;
100+
for ( int index = 0; index < stopperRegLength - 1; index++) {
101+
stopperRegister[index] = stopperRegister[index + 1];
102+
stopperRegSum += stopperRegister[index];
103+
}
104+
stopperRegister[stopperRegLength - 1] = digitalRead(stopper);
105+
106+
if ( stopperRegSum < 3) { // Magnet detected filter noise smaller than 9 cycles
107+
stopperActive = 1;
108+
}
109+
else {
110+
stopperActive = 0;
111+
}
112+
//-----------------------------------------------------------------------------------------------
113+
114+
115+
116+
if (stopperActive == 1) encoderVal = 0;
117+
118+
//wait for signal to go down
119+
if ( state == 0) {
120+
if (digitalRead(deploy) == LOW) {
121+
state = 1;
122+
}
123+
stopMotor();
124+
}
125+
126+
//go down until reach bottom
127+
if (state == 1) {
128+
dir = 1; //depth larger ie. down
129+
if ( encoderVal >= maxDepth ) {
130+
state = 2;
131+
}
132+
down();
133+
}
134+
135+
//go up until reach top
136+
if ( state == 2 ) {
137+
dir = -1; //depth smaller ie. up
138+
if (encoderVal < 1) {
139+
state = 0;
140+
}
141+
up();
142+
}
143+
144+
145+
146+
147+
148+
Serial.print(state); t();
149+
Serial.print(encoderVal); t();
150+
Serial.print(stopperActive); t();
151+
Serial.print(digitalRead(deploy)); t();
152+
// Serial.print(stopperRegSum); t();
153+
// Serial.print(stopperRegister[0]); t();
154+
// Serial.print(digitalRead(stopper)); t();
155+
156+
Serial.println();
157+
158+
159+
160+
161+
}

0 commit comments

Comments
 (0)
Please sign in to comment.