Commit 6fe8c0a5 by Risto Heinsar

Added lab 2 demo codes

parent 23823f64
#define BTN_PIN 3
const int tickDelay = 100;
void setup()
{
// Alustame jadaühenduse
Serial.begin(9600);
// Määrame nupu sisendiks
pinMode(BTN_PIN, INPUT);
}
void loop()
{
// Loeme nupu oleku
bool btnState = digitalRead(BTN_PIN);
// Saadame nupu oleku üle jadaühenduse
Serial.print("btn:");
Serial.println(btnState);
delay(tickDelay);
}
\ No newline at end of file
// Konstant tüüpi muutuja deklareerimine
const int leftLedPin = 7;
void setup()
{
// Seadistame parempoolse LEDi väljundiks
pinMode(leftLedPin, OUTPUT);
}
void loop()
{
// Lülitame LEDi sisse 500 ms
digitalWrite(leftLedPin, HIGH);
delay(500);
// Seejärel lülitame LEDi välja ning ootame veel 500 ms
digitalWrite(leftLedPin, LOW);
delay(500);
}
\ No newline at end of file
// Makro defineerimine
#define LED_LEFT_PIN 7
void setup()
{
// Seadistame parempoolse LEDi väljundiks
pinMode(LED_LEFT_PIN, OUTPUT);
}
void loop()
{
// Lülitame LEDi sisse 500 ms
digitalWrite(LED_LEFT_PIN, HIGH);
delay(500);
// Seejärel lülitame LEDi välja ning ootame veel 500 ms
digitalWrite(LED_LEFT_PIN, LOW);
delay(500);
}
\ No newline at end of file
// Potentsiomeeter kasutab analoogviiku
#define POT_PIN A2
// Uuendus iga 200 ms tagant
const int tickDelay = 200;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Loeme potentsiomeetri väärtuse ja edastame üle jadaühenduse
int potVal = analogRead(POT_PIN);
Serial.println(potVal);
delay(tickDelay);
}
\ No newline at end of file
#define LED_PIN 3
#define MIN_BRIGHTNESS 20
#define MAX_BRIGHTNESS 90
const int tickDelay = 20;
int brightness = MIN_BRIGHTNESS;
int brightnessShift = 1;
void setup()
{
}
void loop()
{
// Muudame eredust
brightness += brightnessShift;
// Jõudes valgustugevuse piirideni, muudame ereduse muutumise suunda
if (brightness >= MAX_BRIGHTNESS || brightness <= MIN_BRIGHTNESS) {
brightnessShift = -brightnessShift;
}
// Kasutades PWMi, uuendame LEDi eredust
analogWrite(LED_PIN, brightness);
delay(tickDelay);
}
\ No newline at end of file
#define LED_LEFT_PIN 7
const int tickDelay = 500;
bool ledState = true;
void setup()
{
// Seadistame parempoolse LEDi väljundiks
pinMode(LED_LEFT_PIN, OUTPUT);
}
void loop()
{
// Anname LEDile uue oleku
digitalWrite(LED_LEFT_PIN, ledState);
// Kontrollime, kas siide ühest olekust teise on lubatud
if (btnState)
{
// Teostame siirde
ledState = !ledState;
}
delay(tickDelay);
}
\ No newline at end of file
#define LED_LEFT_PIN 7
const int tickDelay = 500;
bool ledState = true;
void setup()
{
// Seadistame parempoolse LEDi väljundiks
pinMode(LED_LEFT_PIN, OUTPUT);
}
void loop()
{
// Anname LEDile uue oleku
digitalWrite(LED_LEFT_PIN, ledState);
// Muudame LEDi olekut järgmiseks tsükliks
ledState = !ledState;
delay(tickDelay);
}
\ No newline at end of file
// LEDi viigu defineerimine
#define LED_PIN 3
// Kodeeritud süsteemi olekud
#define STATE_LED_OFF 0
#define STATE_LED_HALF_POWER 1
#define STATE_LED_FULL_POWER 2
// PWM täitetegurid
#define LED_PWN_OFF 0
#define LED_PWM_HALF 127
#define LED_PWM_FULL 255
const int tickDelay = 500;
int ledState = STATE_LED_OFF;
int brightness = LED_PWN_OFF;
void setup()
{
}
void loop()
{
// Leiame oleku põhjal PWM signaali täiteteguri
switch (ledState)
{
case STATE_LED_OFF:
brightness = LED_PWN_OFF;
break;
case STATE_LED_HALF_POWER:
brightness = LED_PWM_HALF;
break;
case STATE_LED_FULL_POWER:
brightness = LED_PWM_FULL;
break;
default:
brightness = LED_PWN_OFF;
}
// Kasutades PWMi, uuendame LEDi eredust
analogWrite(LED_PIN, brightness);
// Siire järgmisse olekusse
ledState = (ledState + 1) % 3;
delay(tickDelay);
}
\ No newline at end of file
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