diff options
Diffstat (limited to 'Arduino')
-rw-r--r-- | Arduino/ardu/ardu.ino | 33 | ||||
-rw-r--r-- | Arduino/garbage_sorting_machine/garbage_sorting_machine.ino | 191 | ||||
-rw-r--r-- | Arduino/garbage_sorting_machine_good/garbage_sorting_machine_good.ino | 187 | ||||
-rw-r--r-- | Arduino/libraries/readme.txt | 1 | ||||
-rw-r--r-- | Arduino/stepper/stepper.ino | 115 |
5 files changed, 527 insertions, 0 deletions
diff --git a/Arduino/ardu/ardu.ino b/Arduino/ardu/ardu.ino new file mode 100644 index 0000000..65e6f52 --- /dev/null +++ b/Arduino/ardu/ardu.ino @@ -0,0 +1,33 @@ +#include <Servo.h> +int relayInput = 27; int moistSensor = A10; Servo servo1; Servo servo2; +enum Material { + dry = 0, wet = 1, metal = 2, none = 4, +}; +enum Material material = dry; int timer = -1; int wetTick = 0; int +wetTickThreshold = 7; void setup() { + pinMode(relayInput, INPUT); digitalWrite(relayInput, HIGH); + pinMode(moistSensor, INPUT); servo1.attach(8); servo2.attach(9); + servo1.write(180); servo2.write(90); Serial.begin(9600); +} +void loop() { int metalState = digitalRead(relayInput); int moistState = + analogRead(moistSensor); if (timer >= 0) { + timer--; + } + if (timer == -1) { if (metalState == LOW) { servo1.write(135); + servo2.write(90); material = metal; timer = 120; + } else { + servo1.write(180); if (moistState <= 800) { wetTick++; if (wetTick + >= wetTickThreshold) { + servo2.write(70); material = wet; timer = 100; + } + } else { + wetTick = 0; servo2.write(90); material = dry; + } + } + } + delay(100); Serial.print("Moisture: "); Serial.print(moistState); + Serial.print("; Metal: "); Serial.print(metalState); Serial.print("; + Material: "); Serial.print(material); Serial.print("; Timer: "); + Serial.print(timer); Serial.print("; wetTick: "); + Serial.println(wetTick); +} diff --git a/Arduino/garbage_sorting_machine/garbage_sorting_machine.ino b/Arduino/garbage_sorting_machine/garbage_sorting_machine.ino new file mode 100644 index 0000000..901c64e --- /dev/null +++ b/Arduino/garbage_sorting_machine/garbage_sorting_machine.ino @@ -0,0 +1,191 @@ +// SERVO 44, 45 +// DISTANCE 25 +// METAL 27 +// CONVEYOR 4 +// MOISTURE A10 + +#include <Servo.h> + +bool isNotIdle = false; + +// Input +int distance = 25; +int metalSensor = 27; +int moisture = A10; + +// Output +Servo servo1, servo2; +int conveyor = 7; + +// Program +enum Material { + dry = 0, wet = 1, metal = 2, none = 3, unknown = 4, +}; +Material material = none; +Material prevMat; +int wetThreshold = 900; + +int tickDuration = 100; // Every clock ticks in ms +int conveyorTimer; +int servoTimer; +int wetThresholdCounter; + +// Duration for timer to reset. 1 : 100 ms (1 : 0,1 s) +int conveyorDuration = 200; +int servoDuration = 70; +int wetDuration = 7; + +// Counting +int total; +int metalPassed; +int wetPassed; +int dryPassed; + +// Servo positions +int servo1Init = 40; +int servo1On = 5; +int servo2Init = 60; +int servo2On = 10; + +// Turning off conveyor after servoTime == -1 +bool justTurnedOff = true; + +void setup() { + pinMode(metalSensor, INPUT_PULLUP); digitalWrite(metal, HIGH); + pinMode(moisture, INPUT); + pinMode(distance, INPUT); + + pinMode(conveyor, OUTPUT); + servo1.attach(44); servo2.attach(45); + servo1.write(servo1Init); servo2.write(servo2Init); + + Serial.begin(9600); +} + +void loop() { + int distanceState = !digitalRead(distance); + int metalState = !digitalRead(metalSensor); + int moistureState = analogRead(moisture); + + // Checking Logic + if (distanceState) { + conveyorTimer = conveyorDuration; + } + + if (metalState) { + servoTimer = servoDuration; + material = metal; + } + + if (moistureState <= wetThreshold && material != wet) { + wetThresholdCounter++; + if (wetThresholdCounter >= wetDuration) { + servoTimer = servoDuration; + material = wet; + } + //servoTimer = servoDuration; + //material = wet; + } + + // Timer & Logics + if (conveyorTimer >= 0) { + conveyorTimer--; + if (conveyorTimer == conveyorDuration - 1) { + justTurnedOff = false; + } + if (conveyorTimer == 0) { + if (material == none || material == unknown || prevMat == none) { + material = dry; + } + } + } else { + material = none; + } + + if (servoTimer >= 0) { + servoTimer--; + if (servoTimer == servoDuration) { + servo1.write(servo1Init); + servo2.write(servo2Init); + } + if (material == metal) { + servo1.write(servo1On); + } else if (material == wet) { + servo2.write(servo2On); + } + } else { + servo1.write(servo1Init); + servo2.write(servo2Init); + if (!justTurnedOff && material != none) { + conveyorTimer = -1; + wetThresholdCounter = 0; + justTurnedOff = true; + } + } + + digitalWrite(conveyor, conveyorTimer >= 0); + + // Counting + if (prevMat != material) { + if (material == metal) { + metalPassed++; + } else if (material == wet) { + wetPassed++; + } else if (material == dry) { + dryPassed++; + } + + total = metalPassed + wetPassed + dryPassed; + + prevMat = material; + } + + // On or off? + if (conveyorTimer >= 0) { + isNotIdle = true; + } else { + isNotIdle = false; + } + + // Debugging + + Serial.print("{\"running\":"); + Serial.print(isNotIdle); + Serial.print(",\"material\":"); + Serial.print(material); + Serial.print(",\"countTotal\":"); + Serial.print(total); + Serial.print(",\"countMetal\":"); + Serial.print(metalPassed); + Serial.print(",\"countWet\":"); + Serial.print(wetPassed); + Serial.print(",\"countDry\":"); + Serial.print(dryPassed); + Serial.print(",\"conveyorTimer\":"); + Serial.print(conveyorTimer); + Serial.print(",\"servoTimer\":"); + Serial.print(servoTimer); + Serial.print(",\"wetTimer\":"); + Serial.print(wetThresholdCounter); + Serial.print(",\"distance\":"); + Serial.print(distanceState); + Serial.print(",\"moisture\":"); + Serial.print(moistureState); + Serial.print(",\"metal\":"); + Serial.print(metalState); + Serial.println("}");/* + Serial.print("Distance "); + Serial.print(distanceState); + Serial.print(" Metal "); + Serial.print(digitalRead(27)); + Serial.print(" Moist "); + Serial.print(moistureState); + Serial.print(" | Conveyor Timer "); + Serial.print(conveyorTimer); + Serial.print(" Servo Timer "); + Serial.print(servoTimer); + Serial.print(" Wet Timer "); + Serial.println(wetThresholdTimer);*/ + + delay(tickDuration); +} diff --git a/Arduino/garbage_sorting_machine_good/garbage_sorting_machine_good.ino b/Arduino/garbage_sorting_machine_good/garbage_sorting_machine_good.ino new file mode 100644 index 0000000..302e74d --- /dev/null +++ b/Arduino/garbage_sorting_machine_good/garbage_sorting_machine_good.ino @@ -0,0 +1,187 @@ +// SERVO 44, 45 +// DISTANCE 25 +// METAL 27 +// CONVEYOR 4 +// MOISTURE A10 + +#include <Servo.h> + +bool isNotIdle = false; + +// Input +int distance = 25; +int metalSensor = 27; +int moisture = A10; + +// Output +Servo servo1, servo2; +int conveyor = 7; + +// Program +enum Material { + dry = 0, wet = 1, metal = 2, none = 3, unknown = 4, +}; +Material material = none; +Material prevMat; +int wetThreshold = 915; + +int tickDuration = 100; // Every clock ticks in ms +int conveyorTimer; +int servoTimer; +int wetThresholdCounter; + +// Duration for timer to reset. 1 : 100 ms (1 : 0,1 s) +int conveyorDuration = 150; +int servoDuration = 70; +int wetDuration = 5; + +// Counting +int total; +int metalPassed; +int wetPassed; +int dryPassed; + +// Servo positions +int servo1Init = 45; +int servo1On = 5; +int servo2Init = 60; +int servo2On = 10; + +// Turning off conveyor after servoTime == -1 +bool justTurnedOff = true; + +void setup() { + pinMode(metalSensor, INPUT_PULLUP); digitalWrite(metal, HIGH); + pinMode(moisture, INPUT); + pinMode(distance, INPUT); + + pinMode(conveyor, OUTPUT); + servo1.attach(44); servo2.attach(45); + servo1.write(servo1Init); servo2.write(servo2Init); + + Serial.begin(9600); +} + +void loop() { + int distanceState = !digitalRead(distance); + int metalState = !digitalRead(metalSensor); + int moistureState = analogRead(moisture); + + // Checking Logic + if (distanceState) { + conveyorTimer = conveyorDuration; + } + + if (metalState) { + servoTimer = servoDuration; + material = metal; + } + + if (moistureState <= wetThreshold && material != wet) { + wetThresholdCounter++; + if (wetThresholdCounter >= wetDuration) { + servoTimer = servoDuration; + material = wet; + } + //servoTimer = servoDuration; + //material = wet; + } + + // Timer & Logics + if (conveyorTimer >= 0) { + conveyorTimer--; + if (conveyorTimer == conveyorDuration - 1) { + justTurnedOff = false; + } + if (conveyorTimer == 0) { + if (material == none || material == unknown || prevMat == none) { + material = dry; + } + } + } else { + material = none; + } + + if (servoTimer >= 0) { + servoTimer--; + if (material == metal) { + servo1.write(servo1On); + } else if (material == wet) { + servo2.write(servo2On); + } + } else { + servo1.write(servo1Init); + servo2.write(servo2Init); + if (!justTurnedOff && material != none) { + conveyorTimer = -1; + wetThresholdCounter = 0; + justTurnedOff = true; + } + } + + digitalWrite(conveyor, conveyorTimer >= 0); + + // Counting + if (prevMat != material) { + if (material == metal) { + metalPassed++; + } else if (material == wet) { + wetPassed++; + } else if (material == dry) { + dryPassed++; + } + + total = metalPassed + wetPassed + dryPassed; + + prevMat = material; + } + + // On or off? + if (conveyorTimer >= 0) { + isNotIdle = true; + } else { + isNotIdle = false; + } + + // Debugging + + Serial.print("{\"running\":"); + Serial.print(isNotIdle); + Serial.print(",\"material\":"); + Serial.print(material); + Serial.print(",\"countTotal\":"); + Serial.print(total); + Serial.print(",\"countMetal\":"); + Serial.print(metalPassed); + Serial.print(",\"countWet\":"); + Serial.print(wetPassed); + Serial.print(",\"countDry\":"); + Serial.print(dryPassed); + Serial.print(",\"conveyorTimer\":"); + Serial.print(conveyorTimer); + Serial.print(",\"servoTimer\":"); + Serial.print(servoTimer); + Serial.print(",\"wetTimer\":"); + Serial.print(wetThresholdCounter); + Serial.print(",\"distance\":"); + Serial.print(distanceState); + Serial.print(",\"moisture\":"); + Serial.print(moistureState); + Serial.print(",\"metal\":"); + Serial.print(metalState); + Serial.println("}");/* + Serial.print("Distance "); + Serial.print(distanceState); + Serial.print(" Metal "); + Serial.print(digitalRead(27)); + Serial.print(" Moist "); + Serial.print(moistureState); + Serial.print(" | Conveyor Timer "); + Serial.print(conveyorTimer); + Serial.print(" Servo Timer "); + Serial.print(servoTimer); + Serial.print(" Wet Timer "); + Serial.println(wetThresholdTimer);*/ + + delay(tickDuration); +} diff --git a/Arduino/libraries/readme.txt b/Arduino/libraries/readme.txt new file mode 100644 index 0000000..96ce674 --- /dev/null +++ b/Arduino/libraries/readme.txt @@ -0,0 +1 @@ +For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries diff --git a/Arduino/stepper/stepper.ino b/Arduino/stepper/stepper.ino new file mode 100644 index 0000000..26786f4 --- /dev/null +++ b/Arduino/stepper/stepper.ino @@ -0,0 +1,115 @@ +// sesuaikan dengan PIN yang dipasang +int INA1=14; +int INA2=15; +int INA3=16; +int INA4=17; + +int INB1=18; +int INB2=19; +int INB3=20; +int INB4=21; +//int step = 100; +int delaytime=2; //makin kecil delay, makin cepat motor berputar + +int pinStatus = 13; + +void setup(){ + pinMode(INA1,OUTPUT); + pinMode(INA2,OUTPUT); + pinMode(INA3,OUTPUT); + pinMode(INA4,OUTPUT); + + pinMode(INB1,OUTPUT); + pinMode(INB2,OUTPUT); + pinMode(INB3,OUTPUT); + pinMode(INB4,OUTPUT); + + pinMode(pinStatus, INPUT); + + Serial.begin(9600); +} + +void loop(){ + //maju 60; + //for (int i=0; i<1000; i++){ + if (!digitalRead(pinStatus)) { + maju(); + } + //} + Serial.println(digitalRead(pinStatus)); +} + +void maju(){ +//step 4 +step1(); +delay(delaytime); +//step 3 +step2(); +delay(delaytime); +//step 2 +step3(); +delay(delaytime); +//step 1 +step4(); +delay(delaytime); +} + +void mundur(){ +//step 4 +step4(); +delay(delaytime); +//step 3 +step3(); +delay(delaytime); +//step 2 +step2(); +delay(delaytime); +//step 1 +step1(); +delay(delaytime); +} + +void step1(){ +digitalWrite(INA1,LOW); +digitalWrite(INB1,LOW); +digitalWrite(INA2,LOW); +digitalWrite(INB2,HIGH); +digitalWrite(INA3,HIGH); +digitalWrite(INB3,HIGH); +digitalWrite(INA4,HIGH); +digitalWrite(INB4,LOW); +} +void step2(){ +digitalWrite(INA1,HIGH); +digitalWrite(INB1,HIGH); +digitalWrite(INA2,LOW); +digitalWrite(INB2,HIGH); +digitalWrite(INA3,LOW); +digitalWrite(INB3,LOW); +digitalWrite(INA4,HIGH); +digitalWrite(INB4,LOW); +} +void step3(){ +digitalWrite(INA1,HIGH); +digitalWrite(INB1,HIGH); +digitalWrite(INA2,HIGH); +digitalWrite(INB2,LOW); +digitalWrite(INA3,LOW); +digitalWrite(INB3,LOW); +digitalWrite(INA4,LOW); +digitalWrite(INB4,HIGH); + + +} +void step4(){ +digitalWrite(INA1,LOW); +digitalWrite(INB1,LOW); +digitalWrite(INA2,HIGH); +digitalWrite(INB2,LOW); +digitalWrite(INA3,HIGH); +digitalWrite(INB3,HIGH); +digitalWrite(INA4,LOW); + +digitalWrite(INB4,HIGH); + +} |