Hello,
I am beginner in Arduino programming. i have this code below:
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the kpd
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define dirPin 12
#define stepPin 9
#define enPin 13
int pdd;
void setup()
{ Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop()
{
int sval = getSensorvalue();
lcd.setCursor(0,1);
lcd.print("S.Value:");
lcd.print(sval);
int pdd = GetNumber();
lcd.setCursor(0,0);
lcd.print("D.Value:");
lcd.print(pdd);
motor_keypad();
if (pdd>(sval+10))
{
motor_rotate_right();
}
else if (pdd<(sval-10))
{
motor_rotate_left();
}
else
{
motor_sationary();
}
}
int getSensorvalue()
{
int pval = analogRead(A0);
int val = map (pval, 0, 1023, 0, 200);
Serial.println(val);
//lcd.setCursor(0,1);
//lcd.print("Value");
//lcd.print(val);
return val;
}
int GetNumber()
{
int num = 0;
char key = keypad.getKey();
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
// lcd.print(key);
num = num * 10 + (key - '0');
break;
case '*':
//num = 0;
lcd.clear();
break;
}
key = keypad.getKey();
}
return num;
}
int motor_keypad()
{
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
for(int x = 0; x < pdd; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
}
int motor_rotate_right()
{
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
for(int x = 0; x < 200; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
}
int motor_rotate_left()
{
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, LOW);
for(int x = 0; x < 200; x++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
}
int motor_sationary()
{
digitalWrite(enPin, LOW);
}
The code is uploaded, but it not compare with keypad input ( int pdd = GetNumber() ) with sensor input ( int sval = getSensorvalue() ) and (void loop() )code is not work properly.
Please guide me.
Thanks