Created
June 10, 2016 13:48
-
-
Save dkavanagh/ede032d7b5126e69ed435fd696eb8cb2 to your computer and use it in GitHub Desktop.
Dual Pwm controller w/ display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sketch illustrates how to use an arduino to provide 2 PWM signals for motor | |
controllers. The LCD display shows the values as driven by pot inputs. It also has | |
the ability to lock the two outputs to the left input pot. | |
The circuit: | |
* LCD RS pin to digital pin 12 | |
* LCD Enable pin to digital pin 11 | |
* LCD D4 pin to digital pin 5 | |
* LCD D5 pin to digital pin 4 | |
* LCD D6 pin to digital pin 3 | |
* LCD D7 pin to digital pin 2 | |
*/ | |
// include the library code: | |
#include <LiquidCrystal.h> | |
#include <Servo.h> | |
const int buttonPin = 8; // the pin that the pushbutton is attached to | |
const int leftPotPin = A0; // select the input pin for the potentiometer | |
const int rightPotPin = A1; // select the input pin for the potentiometer | |
int leftPotValue = 0; // variable to store the value coming from the sensor | |
int rightPotValue = 0; // variable to store the value coming from the sensor | |
bool locked = false; | |
bool lastButtonState = true; | |
Servo leftServo; | |
Servo rightServo; | |
// initialize the library with the numbers of the interface pins | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
void setup() { | |
pinMode(buttonPin, INPUT); | |
// set up the LCD's number of columns and rows: | |
lcd.begin(16, 2); | |
// Print a message to the LCD. | |
lcd.print("left right"); | |
leftServo.attach(9); | |
rightServo.attach(10); | |
} | |
void loop() { | |
// check button state | |
bool tmp = digitalRead(buttonPin); | |
if (tmp != lastButtonState && tmp == false) { | |
locked = !locked; | |
delay(50); | |
} | |
lastButtonState = tmp; | |
// check pot states | |
leftPotValue = analogRead(leftPotPin); | |
if (locked == true) { | |
rightPotValue = leftPotValue; | |
} | |
else { | |
rightPotValue = analogRead(rightPotPin); | |
} | |
// set pwm outputs | |
leftServo.write(map(leftPotValue, 0, 1023, 0, 180)); | |
rightServo.write(map(rightPotValue, 0, 1023, 0, 180)); | |
// set the dynamic parts of the display | |
lcd.setCursor(7, 0); | |
if (locked == true) { | |
lcd.print("="); | |
} | |
else{ | |
lcd.print(":"); | |
} | |
lcd.setCursor(0, 1); | |
lcd.print(leftPotValue / 10.23); | |
lcd.print("% "); | |
lcd.setCursor(9, 1); | |
lcd.print(rightPotValue / 10.23); | |
lcd.print("% "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment