Практика 03
Управление сервомотором с помощью джойстика
1. Вывести данные от джойстика в монитор порта.
2. Изменять значение целочисленной переменной с помощью джойстика,
выводить значения переменной в монитор порта.
3. Программа использует данные от джойстика для управления
сервомотором.
Пример 1
#define D_MIN 350 #define D_MAX 650 #define JOYSTICK_MIN 0 #define JOYSTICK_MAX 1023 #define VAL_MIN 0 #define VAL_MAX 250 #define VAL_STEP 10 int sensorPin = A2; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int val = 0; void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // Serial.print("sensorValue = "); // Serial.println(sensorValue); if ((sensorValue >= JOYSTICK_MIN) && (sensorValue < D_MIN)) { val = val + VAL_STEP; } if ((sensorValue <= JOYSTICK_MAX) && (sensorValue > D_MAX)) { val = val - VAL_STEP; } if (val < VAL_MIN) { val = VAL_MIN; } if (val > VAL_MAX) { val = VAL_MAX; } // Serial.print("val = "); Serial.println(val); delay(50); }