Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save futureshocked/c45ea0802d848eff57cebf4ae9a0431a to your computer and use it in GitHub Desktop.
Save futureshocked/c45ea0802d848eff57cebf4ae9a0431a to your computer and use it in GitHub Desktop.
Grove - Project 4: Motor control with a potentiometer
/* 10.40 - Grove - Project 4: Motor control with a potentiometer
This sketch copies the position of a potentiometer to the servo motor.
As you turn the knob of the potentiometer, the servo motor moves accordingLy.
*** A challenge for you: ***
Can you make the necessary changes to this sketch and the hardware so that the motor
indicates the current temperature drawn on a sheet of paper?
Component
--------
- Grove Base Shield
- An Arduino Uno compatible board (such as Arduino/Genuino Uno or Seeeduino)
- Grove potentiometer
- Grove servo motor
- Grove LCD RGB Backlight module
_ Two Grove cables (the servo motor has its own cable)
_ Optional: a mounting piece of cardboard with nylon nuts, bolts and spacer.
IDE
---
Arduino IDE
Libraries
---------
- Wire.h Cpart of the Arduino IDE)
- rgb_icd.h (Get it from https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight)
- Servo.h
Connections
-----------
- Use a Grove cable to connect the servo motor module to Grove connector D8.
- Use a Grove cable to connect the LCD module to Grove connector I2C.
- Use a Grove cable to connect the potentiometer module to Grove connector A0.
Other information
-----------------
- Use this sketch along side the video lecture 10.40 of Grove For Busy People
- Grove LCD RGB Backlight: https://txplo.re/0c9fb
- Grove potentiometer module: https ://txplo.re/7b837
- Grove Servo module: https://txplo.re/d55cd
Github Gist
-----------
<script src="https://gist.github.com/futureshocked/c45ea0802d848eff57cebf4ae9a0431a.js"></script>
https://gist.github.com/futureshocked/c45ea0802d848eff57cebf4ae9a0431a
For course information, please go to https://techexplorations.com/product/grove-for-busy-people/
Created on July 9 2019 by Peter Dalmaris
*/
#include <Wire.h>
#include "rgb_lcd.h"
#include <Servo.h>
rgb_lcd lcd;
Servo myservo; // create servo object to control a servo
const int motorPin = 8; // The motor is connected to Arduino digital pin 8 (Grove connector D8)
const int potPin = 0; //The potentiometer is connected to Arduino Analog pin 0 (Grove connector A0)
// A custom character for the degree symbol
byte degree[8] = {
0b00100,
0b01010,
0b01010,
0b00100,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup()
{
myservo.attach(motorPin); // attaches the servo on motorPin to the servo object
lcd.begin(16, 2);
lcd.createChar(0, degree); // Create the custom degrees character
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Starting...");
delay(1000) ;
lcd.clear();
lcd.write("Pot value:");
lcd.setCursor(0, 1);
lcd.write("Servo pos: ");
lcd.setCursor(14, 1);
lcd.write((unsigned char)0);
}
void loop()
{
int pot_value = analogRead(potPin);
int servo_position = map(pot_value, 0, 1023, 10, 180);
update_lcd(pot_value, servo_position) ;
move_servo(servo_position) ;
}
void move_servo(int pos)
{
myservo.write(pos) ; // tell servo to go to position in variable "pos
delay(15); // waits 15ms for the servo to reach the position
}
void update_lcd(int pot_value, int servo_position)
{
// update the potentiometer value
lcd.setCursor(11, 0);
lcd.write(" ");
lcd.setCursor(11, 0);
lcd.print(pot_value) ;
// update the servo value
lcd.setCursor(11, 1);
lcd.write(" ");
lcd.setCursor(11, 1);
lcd.print(servo_position) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment