Commit 11580d4d by Risto Heinsar

added wk 4 starter code for the lab

parent 61d1d3fb
Showing with 91 additions and 0 deletions
#include <Servo.h>
#define RIGHT_LED_PIN 7
#define LEFT_LED_PIN 8
#define LEFT_QTI_PIN A0
#define MIDDLE_QTI_PIN A1
#define RIGHT_QTI_PIN A2
#define RIGHT_SERVO_PIN 5
#define LEFT_SERVO_PIN 6
#define SERVO_MIN_PULSE 1300
#define SERVO_MAX_PULSE 1700
#define SERVO_STANDSTILL 1500
#define QTI_THRESHOLD 0 // Pane siia õige väärtus!
/* Servomootorite globaalmuutujate deklaratsioonid */
Servo leftWheel;
Servo rightWheel;
void setup()
{
/* Konfigureerime LEDid väljundiks */
pinMode(LEFT_LED_PIN, OUTPUT);
pinMode(RIGHT_LED_PIN, OUTPUT);
/* Konfigureerime servomootorid */
leftWheel.attach(LEFT_SERVO_PIN, SERVO_MIN_PULSE, SERVO_MAX_PULSE);
rightWheel.attach(RIGHT_SERVO_PIN, SERVO_MIN_PULSE, SERVO_MAX_PULSE);
/* Anname servodemootorite külge võtmiseks hetke aega */
delay(10);
/* Seadistame servomootorid seisvale asendile */
SetWheels(SERVO_STANDSTILL, SERVO_STANDSTILL);
}
void loop()
{
bool leftQtiSeesBlack = IsQtiDetectingBlack(LEFT_QTI_PIN);
bool rightQtiSeesBlack = IsQtiDetectingBlack(RIGHT_QTI_PIN);
/* Vastavalt sensori tulemustele otsustame käitumise */
if (leftQtiSeesBlack && rightQtiSeesBlack)
{
MoveForward(20);
}
//else if (/* Jätka tingimuste kirjutamist */)
{
}
}
void MoveForward(uint16_t time)
{
SetWheels(1700, 1300);
SetLed(HIGH, HIGH);
delay(time);
}
void SetWheels(int leftWheelImpulseLength, int rightWheelImpulseLength)
{
leftWheel.writeMicroseconds(leftWheelImpulseLength);
rightWheel.writeMicroseconds(rightWheelImpulseLength);
}
bool IsQtiDetectingBlack(uint8_t qtiPin)
{
/* Lülitame infrapuna dioodi sisse 1 ms*/
digitalWrite(qtiPin, HIGH);
delayMicroseconds(1000);
digitalWrite(qtiPin, LOW);
/* Loeme kondensaatori pinge */
uint16_t qtiValue = analogRead(qtiPin);
/* Tavastame kas oli üle nivoo (tõene / väär) ehk tuvastas musta värvi */
return qtiValue > QTI_THRESHOLD;
}
void SetLed(uint8_t leftLedState, uint8_t rightLedState)
{
digitalWrite(RIGHT_LED_PIN, leftLedState);
digitalWrite(LEFT_LED_PIN, rightLedState);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment