#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd1(0x27,16,2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
LiquidCrystal_I2C lcd3(0x25, 16, 2);
LiquidCrystal_I2C lcd4(0x24, 16, 2);
Servo servo1;
Servo servo2;
Servo servo3;
int po1=A0;
int pot2 =A1;
int pot3 =A2;
int value1=0;
int value2 =0;
int value3 =0;
int angle=0;
int stepPin = 7; // Step pin for the motor
int dirPin = 8;
int speed=0;
byte glyph255[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup() {
// put your setup code here, to run once:
lcd1.init();
lcd1.backlight();
lcd2.init();
lcd2.backlight();
lcd3.init();
lcd3.backlight();
lcd4.init();
lcd4.backlight();
lcd1.print("LCD 1");
lcd2.print("LCD 2");
lcd3.print("LCD 3");
lcd3.print("LCD 4");
lcd1.createChar(0, glyph255);
lcd2.createChar(0, glyph255);
lcd3.createChar(0, glyph255);
servo1.attach(11);
servo2.attach(10);
servo3.attach(9);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
value1 = analogRead(A0);
value2 = analogRead(A1);
value3 = analogRead(A2);
// 2. Calculate speed WITHOUT changing the global 'value' variables
speed = calculateSpeed(value1, value2, value3);
int speed2 = map(speed, 30,300, 15000, 500);
lcd4.setCursor(0,0);
lcd4.print("LCD 4: ");
lcd4.print(speed);
lcd4.print(" ");
lcd255(value1,lcd1, 1, servo1);
lcd255(value2, lcd2,2, servo2 );
lcd255(value3, lcd3, 3, servo3 );
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed2);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed2);
}
void lcd255(int value, LiquidCrystal_I2C lcd, int number,Servo servo ){
int value2 = map(value, 0,1023, -1,15);
lcd.setCursor(0,1);
for(int x=0; x<16;x++){
if(x<=value2){
lcd.write(byte(0));
}
else{
lcd.print(" ");
}
}
lcd.setCursor(0,0);
lcd.print("LCD ");
lcd.print(number);
lcd.print(": ");
int speed = map(value, 0, 1023, 10, 100);
angle = map(value, 0, 1023, 0, 180);
lcd.print(speed);
lcd.print(" ");
lcd.setCursor(0,1);
servo.write(angle);
}
int calculateSpeed(int v1, int v2, int v3) {
// Create temporary variables so we don't corrupt the global ones
int s1 = map(v1, 0, 1023, 10, 100);
int s2 = map(v2, 0, 1023, 10, 100);
int s3 = map(v3, 0, 1023, 10, 100);
return s1 + s2 + s3;
}