diychen mini shredder motor automatic control module
Detect the working current of the motor. When the current is too large, the motor will automatically reverse
Components and supplies
1
Arduino Nano
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
diychen_minishredder
cpp
Used to prevent DC motors from stalling
1int inPin = A0; 2int RelayPin1 = 4; 3int RelayPin2 = 5; 4int MotorPin = 9; 5int buttonPin1 = 7; 6int buttonLedPin1 = 6; 7int buttonPin2 = 12; 8 9void setup() { 10 // put your setup code here, to run once: 11 Serial.begin(9600); 12 analogReference(INTERNAL); //1.1V internal reference 13 pinMode(RelayPin1, OUTPUT); 14 pinMode(RelayPin2, OUTPUT); 15 pinMode(MotorPin, OUTPUT); 16 pinMode(buttonPin1, INPUT); 17 pinMode(buttonLedPin1, OUTPUT); 18 pinMode(buttonPin2, INPUT); 19} 20int MotorState = 0; 21int MotorSpeed = 0; 22int overload = 0; 23int sensorValue = 0; 24int Motordirection = 0; 25int noload = 0; 26void loop() { 27 if (overload == 0) { 28 if (digitalRead(buttonPin2) == HIGH) { 29 if (Motordirection == 0) { 30 MotorSpeed = 0; 31 noload = 0; 32 analogWrite(MotorPin, MotorSpeed); 33 delay(1000); 34 } 35 Motordirection = 1; 36 } else { 37 if (Motordirection == 1) { 38 MotorSpeed = 0; 39 noload = 0; 40 analogWrite(MotorPin, MotorSpeed); 41 delay(1000); 42 } 43 Motordirection = 0; 44 } 45 } 46 47 if (Motordirection == 0) { 48 49 digitalWrite(RelayPin1, LOW); 50 digitalWrite(RelayPin2, LOW); 51 } else { 52 digitalWrite(RelayPin1, HIGH); 53 digitalWrite(RelayPin2, HIGH); 54 noload = 0; 55 } 56 57 58 Serial.print("direction"); 59 Serial.print(Motordirection); 60 61 62 63 if (digitalRead(buttonPin1) == HIGH) { 64 65 noload = 0; 66 if (MotorState == 0) { 67 MotorState = 1; 68 digitalWrite(buttonLedPin1, HIGH); 69 } else if (MotorState == 1) { 70 MotorState = 0; 71 digitalWrite(buttonLedPin1, LOW); 72 } 73 } 74 Serial.print("button"); 75 Serial.print(MotorState); 76 77 78 if (MotorState == 1) { 79 80 if (MotorSpeed < 255) { 81 for (int i = 0; i < 256; i++) { 82 MotorSpeed = i; 83 analogWrite(MotorPin, MotorSpeed); 84 Serial.print("Speed"); 85 Serial.print(MotorSpeed); 86 } 87 } 88 89 90 91 } else { 92 MotorSpeed = 0; 93 94 analogWrite(MotorPin, MotorSpeed); 95 Serial.print("stop"); 96 } 97 Serial.print("Speed"); 98 Serial.print(MotorSpeed); 99 100 101 sensorValue = 0; 102 for (int i = 0; i < 100; i++) { 103 sensorValue = sensorValue + analogRead(inPin); 104 delay(5); 105 } 106 Serial.print("current"); 107 Serial.print(sensorValue); 108 109 110 if (sensorValue > 1600) { //This value determines the current size that triggers ccw rotation 111 MotorSpeed = 0; 112 analogWrite(MotorPin, MotorSpeed); 113 if (Motordirection == 1) { 114 Motordirection = 0; 115 } else { 116 Motordirection = 1; 117 } 118 119 overload = 1; 120 } else if (sensorValue < 110) { //When the current is lower than this value, it will automatically stop counting. 121 noload = noload + 1; 122 if (noload > 100) { //This value determines the time to automatically stop the motor. 123 MotorState = 0; 124 digitalWrite(buttonLedPin1, LOW); 125 } 126 Serial.print("noload"); 127 Serial.print(noload); 128 } else { 129 noload = 0; 130 } 131 132 if (overload != 0) { 133 overload = overload + 1; 134 Serial.print("overload"); 135 Serial.print(overload); 136 137 if (overload > 7) //This value determines the ccw rotation duration 138 { 139 MotorSpeed = 0; 140 analogWrite(MotorPin, MotorSpeed); 141 142 if (Motordirection == 1) { 143 Motordirection = 0; 144 } else { 145 Motordirection = 1; 146 } 147 148 overload = 0; 149 } 150 } 151 152 153 Serial.println(""); 154}
Comments
Only logged in users can leave comments