Neuveriteľné pokusy o záchranu vesmíru pomocou Arduina: Rozdiel medzi revíziami

Zo stránky Robotický krúžok
Skočit na navigaci Skočit na vyhledávání
(Vytvorená stránka „v nasom programe sme pouzili JOYstick ktorym riadime servo motor LED a bzuciak Program: <syntaxhighlight lang="C++"> #define JOY_X 0 // this represents pin A0 #define JOY_Y 1 // this represents pin A1 #define JOY_SW 5 // this represents pin D5 #define JOY_D4 // this represents pin D4 #include <Servo.h> Servo s; void setup() { pinMode(JOY_SW, INPUT); digitalWrite(JOY_SW, HIGH); // this turns on internal pull-up, because the swit…“)
 
Bez shrnutí editace
Riadok 90: Riadok 90:
</syntaxhighlight>
</syntaxhighlight>


<youtube></youtube>
<youtube>ON-MnqfiEUY</youtube>

Verzia z 17:16, 1. december 2023

v nasom programe sme pouzili JOYstick ktorym riadime servo motor LED a bzuciak

Program:

#define JOY_X 0       // this represents pin A0
#define JOY_Y 1       // this represents pin A1
#define JOY_SW 5      // this represents pin D5
#define JOY_D4        // this represents pin D4

#include <Servo.h>


Servo s;

void setup() {
  pinMode(JOY_SW, INPUT);
  digitalWrite(JOY_SW, HIGH);   // this turns on internal pull-up, because the switch 
                                // has two modes: disconnected or grounded, so we have
                                // to make sure we read logical 1 when it is disconnected
  Serial.begin(9600);
  
  // na zaciatku zapipame
  tone(3, 880, 500);

  // zasvietime LED postupne slabne svetlo
  analogWrite(6, 255);
  delay(500);
  analogWrite(6, 200);
  delay(500);
  analogWrite(6, 150);
  delay(500);
  analogWrite(6, 100);
  delay(500);
  analogWrite(6, 50);
  delay(500);
  analogWrite(6, 0);
  delay(500);

  // pripojime servo na pin D7
  s.attach(7);

  // pohneme servo do krajnych poloh
  s.write(1);
  delay(1000);
  s.write(178);
  delay(1000);
  // a potom do stredu
  s.write(90);
  delay(1000);
}


int pipa=0;   // ked je pipa == 1  riadime bzuciak, servo, aj LED


void loop()
{
    int x = analogRead(JOY_X);
    int y = analogRead(JOY_Y);
    int sw = digitalRead(JOY_SW);
    
    if (sw==0)   // ak stlacil joystick
    {
      // prepneme riadenie on/off
      pipa = 1 - pipa;  
      
      while (sw==0)   // pockame kym pusti kliknutie
      {
        sw = digitalRead(JOY_SW);
      }
      delay(50);
    }

    if (pipa == 1) // ak riadime
    {
      analogWrite(6, x / 4);    // LED
      tone(3, x*2, 50);         // bzuciak
      s.write(1+x*32/183);      // servo
    }
    // vypiseme polohy pacok serva
    Serial.print(x);
    Serial.print(" ");
    Serial.print(y);
    Serial.print(" ");
    Serial.println(sw);
    delay(300);
}