Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- // PIN CONSTANTS
- const byte PIN_LED = 13; // Pin 13 has an LED connected on most Arduino boards.
- const byte PIN_LDR_UP = A0; // LDR ülesse poole paneeli
- const byte PIN_LDR_DOWN = A1; // LDR alla poole paneeli
- const byte PIN_LIMIT_UP = A2; // lääne lõpu lüliti.Peab olema high lõpus low. Kui valgus langeb määratud tasemele-LDR_NIGHT siis pöörab itta?
- const byte PIN_LIMIT_DOWN = A3; // ida lõpu lüliti.peab olema high.Lõpus low.Et ei hakkaks enam pöörama itta. Nii riist- kui tarkvaraliselt
- const byte PIN_MOTOR_DRIVE_UP = 7;// OUT 1 h-bridge
- const byte PIN_MOTOR_DRIVE_DOWN = 8;// OUT 2 h-bridge vahetada OUT 1-ga kui pöörab valele poole
- // LOGIC CONSTANTS
- const byte LDR_DEAD_BAND = 30; // erinevus LDR-de vahel, et hakkaks pöörama
- const byte LDR_NIGHT = 1; // väärtus, et on pime ja paneel sõidaks itta ?
- const int LDR_INTERVAL = 100; // check LDR every x milliseconds
- const int MOTOR_STOP = 0; // 0 to motor_control will stop motors
- const int MOTOR_UP = 255; // Amplitude = PWM, sign = direction
- const int MOTOR_DOWN = -255; // Amplitude = PWM, sign = direction
- const int MOTOR_INTERVAL = 500; // update motor controls every x milliseconds
- const int PRINT_INTERVAL = 1000; // print status to serial interface every x milliseconds
- // GLOBALS
- unsigned long timeMotor = 0;
- unsigned long timeLDR = 0;
- unsigned long timePrint = 0;
- int ldrDown = 0;
- int ldrUp = 0;
- int motor_direction = 0;
- // light dependant resistor measurememt function
- // expects a byte containing the pin to read
- // read the analog value of the pin several times, filtering the value to be more repeatable
- // protection: check the value of the pin is not full scale or zero, indicating a sensor error
- // if protection invoked, return an error message
- // otherwise return the result
- int read_ldr(byte pin)
- {
- int ldr_average = analogRead(pin);
- for (byte i = 0; i < 5; i++)
- {
- int value = analogRead(pin);
- // if (value == 0 || value == 1023)
- // return -1;
- ldr_average += value; // ldr_average = ldr_average + value;
- ldr_average /= 2; // ldr_average = ldr_average / 2;
- }
- return ldr_average;
- }
- // motor control function
- // expects a signed integer to define direction and duty to drive the motor
- // if the parameter is out of range or zero then we stop motors
- // if the parameter is greater than zero we drive to the west
- // if the parameter is less than zer we drive to the east
- // protection: we don't drive in the direction of a limit switch, when it is active.
- // returns what happened
- int motor_control(int dir)
- {
- // stop condition
- digitalWrite(PIN_LED, LOW);
- if (dir == 0 || dir > 255 || dir < -255)
- {
- // stop motors
- //digitalWrite(PIN_MOTOR_ENABLE, LOW);
- digitalWrite(PIN_MOTOR_DRIVE_DOWN, LOW);
- digitalWrite(PIN_MOTOR_DRIVE_UP, LOW);
- motor_direction = MOTOR_STOP;
- digitalWrite(PIN_LED, HIGH);
- return 0;
- }
- // greater than zero -> drive UP
- if (dir > 0)
- {
- // if on Up limit or driving Down (<0), stop
- if (digitalRead(PIN_LIMIT_UP) || motor_direction < 0)
- {
- motor_control(MOTOR_STOP);
- return 2;
- }
- // drive UP
- digitalWrite(PIN_MOTOR_DRIVE_UP, HIGH);
- motor_direction = MOTOR_UP;
- return 1;
- }
- // less than zero -> drive Down
- if (dir < 0)
- {
- // if Down limit or currently driving west (>0), stop
- if (digitalRead(PIN_LIMIT_DOWN) || motor_direction > 0)
- {
- motor_control(MOTOR_STOP);
- return -2;
- }
- // drive Down
- digitalWrite(PIN_MOTOR_DRIVE_DOWN, HIGH);
- //digitalWrite(PIN_MOTOR_DRIVE_UP, HIGH);
- motor_direction = MOTOR_DOWN;
- return -1;
- }
- }
- // the setup routine runs once when you press reset:
- void setup()
- {
- // digital outputs
- pinMode(PIN_LED, OUTPUT);
- // digital pwm outputs
- pinMode(PIN_MOTOR_DRIVE_DOWN, OUTPUT);
- pinMode(PIN_MOTOR_DRIVE_UP, OUTPUT);
- // analog inputs
- pinMode(PIN_LDR_DOWN, INPUT);
- pinMode(PIN_LDR_UP, INPUT);
- // digital inputs
- pinMode(PIN_LIMIT_DOWN, INPUT);
- pinMode(PIN_LIMIT_UP, INPUT);
- digitalWrite(PIN_LIMIT_DOWN, HIGH); // enable internal pull-up resistor
- digitalWrite(PIN_LIMIT_UP, HIGH); // enable internal pull-up resistor
- Serial.begin(9600);
- }
- // the loop routine runs over and over again forever:
- void loop()
- {
- unsigned long time = millis();
- // check LDRs
- if (timeLDR <= time)
- {
- timeLDR = time + LDR_INTERVAL;
- ldrDown = read_ldr(PIN_LDR_DOWN);
- ldrUp = read_ldr(PIN_LDR_UP);
- }
- // do motor control
- if (timeMotor <= time)
- {
- timeMotor = time + MOTOR_INTERVAL;
- if (ldrDown < 0 || ldrUp < 0)
- {
- // stop everything
- // wait 1 minute
- // continue
- motor_control(MOTOR_STOP);
- Serial.print("Motor stop");
- timeMotor = time + 6000;
- motor_control(MOTOR_DOWN);
- Serial.print("Pöörame ALLA");
- delay(5000);
- }
- else
- {
- // main operation
- if (abs(ldrDown - ldrUp) < LDR_DEAD_BAND && ldrDown > LDR_NIGHT && ldrUp > LDR_NIGHT)
- motor_control(MOTOR_STOP);
- else if (ldrDown < ldrUp) // when west is more illuminated than east
- motor_control(MOTOR_UP);
- else if (ldrUp < ldrDown)
- motor_control(MOTOR_DOWN);
- else if (ldrDown < LDR_NIGHT && ldrUp < LDR_NIGHT)
- motor_control(MOTOR_DOWN);
- }
- }
- // print to serial interface
- if (timePrint <= time)
- {
- timePrint = time + PRINT_INTERVAL;
- byte limitDown = digitalRead(PIN_LIMIT_DOWN);
- byte limitUp = digitalRead(PIN_LIMIT_UP);
- Serial.print("Lower LDR, ");
- Serial.print(ldrDown, DEC);
- Serial.print(",Upper LDR, ");
- Serial.print(ldrUp, DEC);
- Serial.print(",End switch E-W, ");
- Serial.print(limitDown);
- Serial.print(",");
- Serial.print(limitUp);
- Serial.print(",Drive, ");
- Serial.print(motor_direction, DEC);
- // Serial.print(",Panel,");
- Serial.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement