diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-06-25 22:21:22 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-06-25 22:21:22 +0300 |
| commit | cc90a7efdab05b5a4566037273553d69e9213aca (patch) | |
| tree | 5946d7a6b252f785d009883b4eef5cc1fc49571f /Code from circuits | |
| parent | 46faa621fa4244bfe6fdfa014dffe79f61ad36db (diff) | |
| download | RemiHap-cc90a7efdab05b5a4566037273553d69e9213aca.tar RemiHap-cc90a7efdab05b5a4566037273553d69e9213aca.tar.gz RemiHap-cc90a7efdab05b5a4566037273553d69e9213aca.zip | |
Reworked dispenser microcontroller code to be smarter, shorter and more flexible.
Diffstat (limited to 'Code from circuits')
| -rw-r--r-- | Code from circuits/DispenserMicrocontroller/DispenserMicrocontroller.ino | 83 |
1 files changed, 22 insertions, 61 deletions
diff --git a/Code from circuits/DispenserMicrocontroller/DispenserMicrocontroller.ino b/Code from circuits/DispenserMicrocontroller/DispenserMicrocontroller.ino index 900ce6a..3025ed7 100644 --- a/Code from circuits/DispenserMicrocontroller/DispenserMicrocontroller.ino +++ b/Code from circuits/DispenserMicrocontroller/DispenserMicrocontroller.ino @@ -1,76 +1,37 @@ -#include <Servo.h> -// Note: the library disables analogWrite on pins 9 and 10 +#include <Servo.h> +// Note: servo.h library disables analogWrite on pins 9 and 10 #define INPUT_PIN A0 +int potValue; // Potentiometer value, will be the input value from the main controller -#define SERVO_1_PIN 3 -#define SERVO_2_PIN 5 -#define SERVO_3_PIN 6 -#define SERVO_4_PIN 9 -#define SERVO_5_PIN 10 - -Servo servo_1; -Servo servo_2; -Servo servo_3; -Servo servo_4; -Servo servo_5; - -// Potentiometer value, will be the input value from the main controller -int potValue; +Servo servos[5]; +const int SERVO_PINS[] = {3, 5, 6, 9, 10}; void zeroOutServos() { - servo_1.write(0); - servo_2.write(0); - servo_3.write(0); - servo_4.write(0); - servo_5.write(0); + for (int i = 0; i < sizeof(servos)/sizeof(*servos); i++) { // would've used range-based for loop, but its not supported on C++98 + servos[i].write(0); + } } -void setup() -{ +void setup() { pinMode(INPUT_PIN, INPUT); - - pinMode(SERVO_1_PIN, OUTPUT); - servo_1.attach(SERVO_1_PIN); - pinMode(SERVO_2_PIN, OUTPUT); - servo_2.attach(SERVO_2_PIN); - pinMode(SERVO_3_PIN, OUTPUT); - servo_3.attach(SERVO_3_PIN); - pinMode(SERVO_4_PIN, OUTPUT); - servo_4.attach(SERVO_4_PIN); - pinMode(SERVO_5_PIN, OUTPUT); - servo_5.attach(SERVO_5_PIN); - - delay(2000); + + for(int i = 0; i < sizeof(servos)/sizeof(*servos); i++) { + pinMode(SERVO_PINS[i], OUTPUT); + servos[i].attach(SERVO_PINS[i]); + } + + delay(1500); zeroOutServos(); } -void loop() -{ - potValue = analogRead(INPUT_PIN) >> 2; - // For demo purposes, continuously rotates servo - dispensePill((potValue * 5 / 255) + 1); - delay(100); -} - void dispensePill(int servoIndex) { - switch(servoIndex) { - case 1: - servo_1.write((servo_1.read() == 180)?0:180); - break; - case 2: - servo_2.write((servo_2.read() == 180)?0:180); - break; - case 3: - servo_3.write((servo_3.read() == 180)?0:180); - break; - case 4: - servo_4.write((servo_4.read() == 180)?0:180); - break; - case 5: - servo_5.write((servo_5.read() == 180)?0:180); - break; - } + servos[servoIndex].write((servos[servoIndex].read() == 180)?0:180); delay(1500); } +void loop() { // For demo purposes, continuously rotates selected servo + potValue = analogRead(INPUT_PIN) >> 2; + dispensePill(potValue * 5 / 255); + delay(100); +} |
