ค้นหาโค้ด

รหัส Arduino สำหรับใช้จอยสติ๊กแบบสองแกน XY ร่วมกับหน้าจอ LCD Nokia 5110

รหัส Arduino สำหรับใช้จอยสติ๊กแบบสองแกน XY ร่วมกับหน้าจอ LCD Nokia 5110

บทช่วยสอนนี้จะแนะนำคุณตลอดกระบวนการใช้จอยสติ๊ก XY แบบสองแกนเพื่อควบคุมจุดที่แสดงบนหน้าจอ LCD Nokia 5110 โปรเจกต์นี้เกี่ยวข้องกับการอ่านตำแหน่งของจอยสติ๊กและอัปเดตการแสดงผลตามนั้น เมื่อสิ้นสุดคู่มือนี้ คุณจะมีการตั้งค่าที่ใช้งานได้จริง โดยการเลื่อนจอยสติ๊กจะเปลี่ยนตำแหน่งของจุดบนหน้าจอ (ในวิดีโอที่ 00:00)

Nokia 5110_LCD

ในโปรเจกต์นี้ คุณจะใช้เอาต์พุตแอนะล็อกของจอยสติ๊กเพื่อกำหนดตำแหน่งบนแกน X และ Y ค่าที่อ่านได้จากจอยสติ๊กจะถูกแมปกับขนาดของหน้าจอ LCD ทำให้จุดเคลื่อนที่ได้อย่างราบรื่น นอกจากนี้ คุณจะได้เรียนรู้วิธีอ่านอินพุตสวิตช์ของจอยสติ๊ก ซึ่งสามารถใช้สำหรับการโต้ตอบเพิ่มเติมได้

อธิบายฮาร์ดแวร์

ส่วนประกอบหลักสำหรับโปรเจกต์นี้ ได้แก่ Arduino จอยสติ๊ก XY แบบสองแกน และหน้าจอ LCD Nokia 5110 Arduino ทำหน้าที่เป็นไมโครคอนโทรลเลอร์ ประมวลผลอินพุตจากจอยสติ๊กและควบคุมการแสดงผล LCD

โดยทั่วไปจอยสติ๊กจะมีโพเทนชิโอมิเตอร์สองตัว—หนึ่งตัวสำหรับแกน X และหนึ่งตัวสำหรับแกน Y เมื่อคุณเลื่อนจอยสติ๊ก โพเทนชิโอมิเตอร์เหล่านี้จะเปลี่ยนความต้านทาน ส่งระดับแรงดันไฟฟ้าที่แตกต่างกันไปยังพินอินพุตแอนะล็อกของ Arduino หน้าจอ LCD Nokia 5110 ถูกควบคุมผ่านการสื่อสาร SPI ซึ่งช่วยให้การถ่ายโอนข้อมูลและการอัปเดตการแสดงผลมีประสิทธิภาพ

รายละเอียดจาก Datasheet

ผู้ผลิตTexas Instruments
หมายเลขชิ้นส่วนL293D
แรงดันไฟฟ้าลอจิก/IO5 V
แรงดันไฟฟ้าจ่าย4.5–36 V
กระแสเอาต์พุต (ต่อช่อง)600 mA
กระแสสูงสุด (ต่อช่อง)1.2 A
คำแนะนำความถี่ PWM20 kHz
เกณฑ์อินพุตลอจิกเข้ากันได้กับ TTL
แรงดันตก / RDS(on) / ความอิ่มตัวสูงสุด 1.5 V
ขีดจำกัดความร้อน150 °C
แพ็กเกจDIP-16
หมายเหตุ / รุ่นต่างๆใช้กันอย่างแพร่หลายในแอปพลิเคชันไดรเวอร์มอเตอร์

  • ตรวจสอบให้แน่ใจว่ามีการระบายความร้อนที่เหมาะสมสำหรับการทำงานต่อเนื่อง
  • ใช้ตัวเก็บประจุแยกสัญญาณเพื่อให้แหล่งจ่ายไฟมีเสถียรภาพ
  • ตรวจสอบพิกัดแรงดันไฟฟ้าเพื่อหลีกเลี่ยงความเสียหายต่อส่วนประกอบ
  • ระมัดระวังกับสัญญาณ PWM ตรวจสอบให้แน่ใจว่าอยู่ภายในขีดจำกัดที่กำหนด
  • ตรวจสอบการเชื่อมต่อสายไฟเพื่อหลีกเลี่ยงการลัดวงจร

คำแนะนำการเดินสายไฟ

Arduino wiring for Nokia 5110 LCD  with Joystick
Arduino wiring for Nokia 5110 LCD with Joystick

ในการเดินสายจอยสติ๊กและหน้าจอ LCD เข้ากับ Arduino ให้เริ่มต้นโดยเชื่อมต่อพิน VCC ของจอยสติ๊กเข้ากับ 5V ของ Arduino และพิน GND เข้ากับ GND ของ Arduino เชื่อมต่อเอาต์พุตแกน X ของจอยสติ๊กเข้ากับพินแอนะล็อก A0 และเอาต์พุตแกน Y เข้ากับพินแอนะล็อก A1 เอาต์พุตสวิตช์ของจอยสติ๊กควรเชื่อมต่อกับพินดิจิทัล 2

สำหรับ LCD Nokia 5110 ให้เชื่อมต่อพินต่อไปนี้: VCC เข้ากับ 5V, GND เข้ากับ GND, SCE เข้ากับพิน 7, RST เข้ากับพิน 6, D/C เข้ากับพิน 5, DN(MOSI) เข้ากับพิน 11 และ SCLK เข้ากับพิน 13 สุดท้าย เชื่อมต่อพิน LED ผ่านตัวต้านทาน 330 โอห์มเข้ากับพิน 9 สำหรับควบคุมไฟแบ็คไลท์

ตัวอย่างโค้ดและคำอธิบาย

ในฟังก์ชัน setup เราจะเริ่มต้นการสื่อสารแบบอนุกรมและ LCD คำสั่ง lcdBegin() จะตั้งค่าพินและเริ่มต้นการแสดงผล ค่าคอนทราสต์ถูกตั้งค่าเพื่อการมองเห็นที่เหมาะสมที่สุด

void setup() {
  Serial.begin(9600);
  lcdBegin(); 
  setContrast(40); 
  delay(1000);
  clearDisplay(BLACK);
  updateDisplay();
}

การดำเนินการนี้จะเริ่มต้นส่วนประกอบที่จำเป็นสำหรับโปรเจกต์ เพื่อให้แน่ใจว่า LCD พร้อมที่จะแสดงข้อมูล

Nokia 5110_LCD back view

ในฟังก์ชัน loop เราจะอ่านตำแหน่งของจอยสติ๊กและแมปกับขนาดหน้าจอ ตำแหน่งของจุดจะถูกอัปเดตตามค่า X และ Y ของจอยสติ๊ก

void loop() {
  int x = analogRead(A0); // อ่านตำแหน่ง x ของจอยสติ๊ก
  int y = analogRead(A1); // อ่านตำแหน่ง y ของจอยสติ๊ก
  int xPos = map(x, 0, 1023, 0, screenWidth); // แมป x กับหน้าจอ
  int yPos = map(y, 0, 1023, 0, screenHeight); // แมป y กับหน้าจอ
  setCircle(xPos, yPos, thickness, BLACK, 2); // แสดงจุด
  updateDisplay();
  delay(10);
}

โค้ดนี้จะอัปเดตตำแหน่งของจุดบน LCD อย่างต่อเนื่องตามการเคลื่อนที่ของจอยสติ๊ก

การสาธิต / สิ่งที่คาดหวัง

เมื่อคุณจ่ายไฟให้วงจร LCD จะแสดงจุด การเลื่อนจอยสติ๊กจะเปลี่ยนตำแหน่งของจุดแบบเรียลไทม์ตามการเคลื่อนที่ของแกน X และ Y ของจอยสติ๊ก หากกดสวิตช์จอยสติ๊ก จะสามารถ触发ฟังก์ชันเพิ่มเติมที่คุณอาจ implement ได้ ควรระวังอินพุตลอยตัวหากจอยสติ๊กไม่ได้อยู่ตรงกลาง ซึ่งอาจทำให้จุดเคลื่อนที่โดยไม่คาดคิด (ในวิดีโอที่ 01:30)

ไทม์สแตมป์ในวิดีโอ

  • 00:00 - แนะนำโปรเจกต์
  • 01:30 - อธิบายการเดินสายไฟ
  • 02:45 - อธิบายโค้ด
  • 04:10 - สาธิตการทำงาน

ภาพ

Nokia 5110_LCD back view
Nokia 5110_LCD back view
Arduino wiring for Nokia 5110 LCD  with Joystick
Arduino wiring for Nokia 5110 LCD with Joystick
Nokia 5110_LCD
Nokia 5110_LCD
81-Arduino code: Dual-axis joystick with Nokia 5110 screen to display dots
ภาษา: C++
/*
 * This is Arduino code to use a dual-axis XY joystick with a Nokia 5110 screen to move a dot on the screen.
 * It also reads the switch.
 * Other Arduino libraries and videos: https://robojax.com
 
 * Watch the video for this code to learn it fully.
  * Watch the video here: https://youtu.be/zqDZybR5JSE
 * This code is offered "as is" without any warranty.
 * If you are sharing this code, you must keep this copyright note.
 */
 /*
/* Nokia 5100 LCD Example Code with Added Joystick by Robojax
   Graphics driver and PCD8544 interface code for SparkFun's
   84x48 Graphic LCD.
   https://www.sparkfun.com/products/10168
   Original source code:
https://github.com/sparkfun/GraphicLCD_Nokia_5110

  This stuff could all be put into a library, but we wanted to
  leave it all in one sketch to keep it as transparent as possible.

  Hardware: (Note most of these pins can be swapped)
    Graphic LCD Pin ---------- Arduino Pin
       1-VCC       ----------------  5V
       2-GND       ----------------  GND
       3-SCE       ----------------  7
       4-RST       ----------------  6
       5-D/C       ----------------  5
       6-DN(MOSI)  ----------------  11
       7-SCLK      ----------------  13
       8-LED       - 330 Ohm res --  9
   The SCLK, DN(MOSI), must remain where they are, but the other 
   pins can be swapped. The LED pin should remain a PWM-capable
   pin. Don't forget to stick a current-limiting resistor in line
   between the LCD's LED pin and Arduino pin 9!
   Modified by Ahmad S. for Robojax.com
   on Mar 11, 2018 at 20:49 at Ajax, Ontario, Canada
*/
#include <SPI.h>
#include "LCD_Functions.h"

#define sw 2 // pint 2 is used for joystick switch input
#define screenWidth 83 // NOkia screen width
#define screenHeight 47 // Nokia screen height
int thickness =1;// the thickness of the dot


void setup()
{
  // Robojax Dual Axis joystick  with Nokia 5110 LCD screen project
  Serial.begin(9600);

  lcdBegin(); // This will setup our pins, and initialize the LCD
  //updateDisplay(); // with displayMap untouched, SFE logo
  setContrast(40); // Good values range from 40-60
  delay(1000);

  clearDisplay(BLACK);
  updateDisplay();
}


void loop()
{
  clearDisplay(WHITE);  
   // Robojax Dual Axis joystick  with Nokia 5110 LCD screen project
  int x = analogRead(A0);// read the x position of joystick
  int y = analogRead(A1); // read the y position of joystick
  int xPos = map(x, 0, 1023,0,screenWidth);// map or translate the x of joystick to x of screen
  int yPos = map(y, 0, 1023,0,screenHeight); // map or translate the y of joystick to y of screen
  int sStat = digitalRead(sw);//read the switch from pin 2
  Serial.print("X: ");
  Serial.print(xPos);
  Serial.print(" Y: ");
  Serial.println(yPos);// Robojax prints y

  // Robojax Dual Axis joystick  with Nokia 5110 LCD screen project
   //setCircle(xPos, yPos, thicness, BLACK, 2);
   // xPos is the position of x
   // yPos is the position of y
   // thickness
  setCircle(xPos, yPos, thickness, BLACK, 2);// display the dot on the screen
    updateDisplay();
  delay(10);


}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ยังไม่มีทรัพยากรเลย

ไฟล์📁

ไฟล์ที่ต้องการ (.h)

ไฟล์ Fritzing