Hello Friends i Am creating a dc motor controller (for leg extension machine) which moves motor between two points specified by two potentiometers and as a feedback third potentiometer is used my code is as follows
#include <SPI.h>
#include <Wire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int Extension = A0 ;
int Extension_value ;
int Flexion = A1 ;
int Flexion_value ;
//int Pot_speed = A2 ;
int pot_position = A2 ;
int position_value ;
int forward = 11 ;
int forward_state ;
int reverse = 12 ;
int reverse_state;
int s_limit = 2 ;
int e_limit = 3 ;
bool done = false ;
int up = 4 ;
int dn = 5 ;
int sel = 6 ;
int fwd = 7 ;
int rev = 8 ;
int button1 = 0 ;
int button2 = 0 ;
int count_value = 0 ;
int prestate = 0 ;
int button3 = 0 ;
int button4 = 0 ;
int button5 = 0 ;
int sel_prestate = 0 ;
int fwd_prestate = 0 ;
int rev_prestate = 0 ;
int sel_state = 0 ;
int fwd_state = 0 ;
int rev_state = 0 ;
void setup() {
lcd.init();
lcd.backlight();
///////////////////////////HOMING//////////////////
homing();
///////////////////INPUT OUTPUT//////////////////
pinMode (Extension , INPUT) ;
pinMode (Flexion , INPUT) ;
pinMode (pot_position , INPUT) ;
pinMode (up , INPUT) ;
pinMode (dn , INPUT) ;
pinMode (sel , INPUT) ;
pinMode (fwd , INPUT) ;
pinMode (rev , INPUT) ;
pinMode (forward , OUTPUT) ;
pinMode (reverse , OUTPUT) ;
Serial.begin(115200);
digitalWrite (reverse , LOW);
digitalWrite (forward ,LOW);
}
void loop() {
button1 = digitalRead (up) ;
button2 = digitalRead (dn) ;
button3 = digitalRead (sel) ;
button4 = digitalRead (fwd) ;
button5 = digitalRead (rev) ;
///////////////////Buttons////////////////////////////
if (button1 == HIGH && prestate == 0) {
count_value++;
prestate = 1;
}
else if (button2 == HIGH && prestate == 0) {
count_value--;
prestate = 1;
}
else if(button1 == LOW && button2 == LOW) {
prestate = 0;
}
if(count_value<0){
count_value = 0;
}
//////////////mapping and displaying pot values ////////////////////////////
Extension_value = analogRead (Extension) ;
Extension_value = map(Extension_value, 0, 1023, 0, 120) ;
Flexion_value = analogRead (Flexion) ;
Flexion_value = map(Flexion_value, 0, 1023, 0, 120) ;
position_value = analogRead (pot_position) ;
position_value = map(position_value, 0, 1023, 0, 120) ;
/////////////////////////////LCD PRINT/////////////////////////////////////
lcd.setCursor(0,0);
lcd.print("Ex:");
lcd.print (Extension_value);
lcd.setCursor(7,0);
lcd.print("Fx:");
lcd.print(Flexion_value);
lcd.setCursor(0,1);
lcd.print("ExAngle:");
lcd.print(position_value);
lcd.print(" Rp:");
lcd.print(count_value);
////////////////////////// NEW CODE //////////////////////////////////////////
if (button3 == HIGH && sel_prestate == 0){
sel_state = 1 ;
}
//////////////////////// Motor Movement //////////////////////////////////////
if (sel_state > 0 ){
movemotor();
}
//////////////////////////////////////////////////////////////////////////
//count_value = count_value - 1 ;
}
//////////////// Homing Function////////////////////////
void homing()
{
while(digitalRead(s_limit)== HIGH){
digitalWrite(reverse , HIGH);
lcd.setCursor(0, 0);
lcd.print("INITIALISING PLS ");
lcd.setCursor (0 ,1);
lcd.print("WAIT.......");
delay(200);
lcd.clear();
}
}
/////////////// Motor Movement//////////////////////////
void movemotor()
{
if (Flexion_value < position_value){
digitalWrite (reverse , HIGH);
digitalWrite (forward , LOW);
}
if (Extension_value > position_value){
digitalWrite (forward , HIGH);
digitalWrite (reverse , LOW);
}
}
////////////////////////////////////////////////////////////////
i want to repeat ///// Motor Movemnt//////////// part count_value no of times ... and suggesions would be helpful i tried using while loop but potentiometer input freezed also tried using for loop but it not seems to work
//////////////////////// Motor Movement //////////////////////////////////////
**if (sel_state > 0 ){
movemotor();
}**
//////////////////////////////////
movemotor()
function takes only a very short time to execute (probably singe digit milliseconds). So repeating it 5 times will not have much of an effect. What are you trying to achieve here?