-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMagnetic_Flow_Meter.ino
95 lines (85 loc) · 2.23 KB
/
Magnetic_Flow_Meter.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
//Grady Hillhouse 2016
//L298N Pins
const int IN1 = 8;
const int IN2 = 9;
const int IN3 = 10;
const int IN4 = 11;
//Biphasic DC Pulse Electromagnet Voltage Signal Properties
const int freq = 1; //Hertz
const double duty_cycle = 0.5;
const boolean reverse_mag = false; //Set to true if the two electromagnets are not running in the same direction (easier just to switch the wires on the H-bridge)
const double signal_delay = 0.5 / freq * duty_cycle * 1000.0; //Duration in milliseconds that the voltage is high or low
const double off_delay = 0.5 / freq * (1.0 - duty_cycle) * 1000.0; //Duration in milliseconds that the voltage is zero
//Other variables
unsigned long previous_millis = 0; //Milliseconds
boolean previous_direction = false;
boolean currently_off = true;
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
turn_off_magnets(); //Just in case
}
void loop()
{
unsigned long current_millis = millis();
if (currently_off)
{
if (current_millis - previous_millis >= off_delay)
{
previous_millis = current_millis;
previous_direction = !previous_direction;
activate_magnets(previous_direction);
currently_off = false;
}
}
else
{
if (current_millis - previous_millis >= signal_delay)
{
previous_millis = current_millis;
turn_off_magnets();
currently_off = true;
}
}
}
void activate_magnets(boolean positive)
{
if (positive && !reverse_mag)
{
digitalWrite(IN2, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN1, HIGH);
digitalWrite(IN3, HIGH);
}
else if (positive && reverse_mag)
{
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN1, HIGH);
digitalWrite(IN4, HIGH);
}
else if (!positive && !reverse_mag)
{
digitalWrite(IN1, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);
}
else if (!positive && reverse_mag)
{
digitalWrite(IN1, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
}
}
void turn_off_magnets()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}