/*==========================================================================
  //  Source-Code : BTS7960.ino
  //  Program: Steuerung fuer DC Motor mit BTS7960 H-Bridge Treiber.
  //==========================================================================
  //  Connection to the BTS7960 board:
  //  BTS7960 Pin 1 (RPWM) to Arduino pin 5(PWM)
  //  BTS7960 Pin 3 (R_EN), 4 (L_EN), 7 (VCC) bekommt vom Arduino 5V VCC
  //  BTS7960 Pin 8 (GND) to Arduino GND
  //  BTS7960 Pin 5 (R_IS) and 6 (L_IS) keine Verbindung
*/
#include <LiquidCrystal_I2C.h> // LCD Display
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int SENSOR_PIN = A0; // Pin fuers Poti
int RPWM_Output = 11; // PWM Ausgangs Pin mit 3900Hz PWM
int PWM;
int sensorValue;
void setup()
{
  // ----------PWM Tackt fuer Pin 11&12 auf 3900Hz geaendert---------------
  TCCR1B = TCCR1B & 0b11111000 | 0x02;

  lcd.begin(20, 4);
  lcd.backlight();
  lcd.clear();

  Serial.begin(115200);
  pinMode(RPWM_Output, OUTPUT);
}
void loop()
{
  sensorValue = analogRead(SENSOR_PIN);
  // Serial.println(sensorValue);

  PWM = map(sensorValue, 0, 1023, 0, 254);
  analogWrite(RPWM_Output, PWM);

  lcd.setCursor (0, 1);
  lcd.print (F("PWM = "));
  lcd.setCursor (10, 1);
  lcd.print("    ");
  lcd.setCursor (10, 1);
  lcd.print(PWM);
}