-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stirring_system.ino
66 lines (61 loc) · 1.4 KB
/
Stirring_system.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
float highlevel ;
float lowlevel;
unsigned long RPM;
int Userinput = 1000;
float Val = 70;
float RPMaverage = 0;
float RPMtotal = 0;
int times=0;
void setup() {
pinMode(6, INPUT);
pinMode(5,OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long duration;
highlevel = pulseIn(6, HIGH);
lowlevel = pulseIn(6, LOW);
duration = highlevel+lowlevel;
//measure RPM from duration
RPM=30000000/duration;
if( duration == 0 ) {RPM = 0;}
Serial.print("the RPM is\n");
Serial.print("\t");
Serial.print(String(RPM) + "\n");
unsigned long ll=Userinput-20;
unsigned long ul=Userinput+20;
//output and control motor speed
if(RPM<ll) {
Val = Val+0.1;
if(Val>255){ Val = 255;}
analogWrite(5,Val);
Serial.print("The value of PWM 1 is ");
Serial.println(Val);
Serial.print("The value of RPM is ");
Serial.println(RPM);
delay(100);
//times = times+1;
}
else if(RPM>=ll&&RPM<=ul){
// turn the pin on:
Val = Val;
analogWrite(5,Val);
Serial.print("The value of PWM 2 is ");
Serial.println(Val);
Serial.print("The value of RPM is ");
Serial.println(RPM);
delay(100);
//times = times+1;
}
else if(RPM>ul){
Val = Val-0.1;
if(Val<0){ Val = 0;}
analogWrite(5,Val);
Serial.print("The value of PWM 3 is ");
Serial.println(Val);
Serial.print("The value of RPM is ");
Serial.println(RPM);
delay(100);
//times = times+1;
}
}