November 20, 2017

Weekday Arduino Project #1

LCD Screen Reads Out Servo Angle

The Project:
Create an Arduino project that steadily increases the angle of a servo, and prints the angle on an LCD screen.

Pictures:
Why?:
This is a tester for a long-term project that I am working on currently.

What you need:

  1. Arduino Uno board
  2. 16 pin 16x2 LCD screen
  3. 3 pin servo
  4. 10k ohm potentiometer
  5. 220 ohm resistor
  6. Breadboard
  7. Arduino software
The Schematic:

The Code:
// Include the library code:

#include <LiquidCrystal.h>
#include <Servo.h>

// Initialize the library by associating any needed LCD interface pin
// With the arduino pin number it is connected to
// Const ints
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int numRows = 2;
const int numCols = 16;

// Ints
int potpin = 0;
int val;
int count  = 0;

// Create servo object to control servo
Servo myservo;

// Set up LCD display
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
 // Set up the LCD's number of columns and rows:
 lcd.begin(numCols, numRows);
 Serial.begin(9600);
 myservo.attach(9);
}

Void loop(){
 // Refresh the display
 lcd.clear();
 lcd.display();
 // Set display
 lcd.setCursor(0,0);
 
 // Math for Servo
 count = (count + 1);
 Serial.print(count);
// Reset counter if it reaches 1000
 if (count == 1000){
   count = 0;
}
 // Convert and write to servo
 val = map(count,0,1000,0,180);
 myservo.write(val);
 
 // Print angle on LCD
 lcd.print(val);
 delay(50);
}

Feel free to contact me if you have any questions!


No comments:

Post a Comment