Controllo motore CC bidirezionale con IU su display LCD con scheda Arduino

Specifiche display

Azionamento

Retroazione

 


/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  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
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define BT_UP  9  //pulsante up
#define BT_DW 10  //pulsante dw

#define RPM_MAX  1023
#define RPM_MIN  -1023
#define EnA  6
#define In1  8 
#define In2  7 
#define KRPM 3600.0/1023

int RefRPM;
int pwm;

int Stato=0;
bool SRot=true;
int spire=5000;

void setup(){
  // Test LCD
  lcd.begin(16, 2);
  lcd.print("Test LCD OK") ; // messaggio di test
  delay(2000);               // pausa di 2 secondi
  lcd.clear();               // cancellazione display
  delay(10);                

  pinMode(BT_UP,INPUT_PULLUP);
  pinMode(BT_DW,INPUT_PULLUP);

  pinMode(In1,OUTPUT);
  pinMode(In2,OUTPUT);
  pinMode(EnA,OUTPUT);
  digitalWrite(In1,LOW);
  digitalWrite(In2,LOW);
  RefRPM=0;
  pwm=0;
  //---------01234567890123456789          
  lcd.print("Ref (RPM):") ; // messaggio di test
  lcd.setCursor(10,0);
  lcd.print(RefRPM*KRPM,0);
  lcd.setCursor(0,1);
  lcd.print("Ret (RPM):") ; // messaggio di test
  lcd.setCursor(10,1);
  lcd.print(0,0);
}

void Regolatore()
{
  int Err,Retro,r;
  Retro=analogRead(A0);
  lcd.setCursor(10,1);
  lcd.print(Retro*KRPM,0);
  lcd.print("  ");
  if (RefRPM>0)
  {
    digitalWrite(In1,LOW);
    digitalWrite(In2,HIGH);    
    r=RefRPM;
  }
  else
  {
    digitalWrite(In1,HIGH);
    digitalWrite(In2,LOW);    
    r=-RefRPM;
  }  
  Err=r-Retro;
  if (Err>0) 
  {    
    if (pwm<255)   
    {  
      pwm++;
      analogWrite(EnA,pwm);
    }          
  }  
  else  
  {
    if (pwm>0)   
    {  
      pwm--;
      analogWrite(EnA,pwm);
    }              
  }  
}
  
void loop(){
  if ((digitalRead(BT_UP)==LOW) && (RefRPM<RPM_MAX))
  {  
    RefRPM++;
    lcd.setCursor(10,0);
    lcd.print(RefRPM*KRPM,0);
    lcd.print("  ");
  }
  if ((digitalRead(BT_DW)==LOW) && (RefRPM>RPM_MIN))
  {
    RefRPM--;
    lcd.setCursor(10,0);
    lcd.print(RefRPM*KRPM,0);
    lcd.print("  ");
  }    
  Regolatore();  
}