Code:
//libraries
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#include <IRremote.h>
#include <Bounce2.h>
uint8_t RECV_PIN = 13;
uint8_t taste = 0;
uint8_t zaehler = 1;
//resett pin definieren
#define PIN2RESET 10
// DIR und STEP pins definieren
#define dirPin_VL 2
#define stepPin_VL 3
#define dirPin_HL 4
#define stepPin_HL 5
#define dirPin_VR 6
#define stepPin_VR 7
#define dirPin_HR 8
#define stepPin_HR 9
//enable pins definieren
#define enbl_VL 40
#define enbl_HL 42
#define enbl_VR 41
#define enbl_HR 43
//steps pro umdrehung definieren:
#define stepsPerRevolution 200
//IR pin definieren
IRrecv irrecv(RECV_PIN);
decode_results results;
// debouncer instanz definieren
Bounce debouncer = Bounce();
void setup()
{
//pins als output:
pinMode(dirPin_VL, OUTPUT);
pinMode(stepPin_VL, OUTPUT);
pinMode(dirPin_HL, OUTPUT);
pinMode(stepPin_HL, OUTPUT);
pinMode(dirPin_VR, OUTPUT);
pinMode(stepPin_VR, OUTPUT);
pinMode(dirPin_HR, OUTPUT);
pinMode(stepPin_HR, OUTPUT);
Serial.begin(115200);
//resett pin zustand definieren
pinMode(PIN2RESET, INPUT);
// IR empfänger pin mit bounce verbinden
debouncer.attach(RECV_PIN);
// debounce interval in ms
debouncer.interval(5);
// starte IR receiver
irrecv.enableIRIn();
//enable pins deaktivieren:
digitalWrite(enbl_VL, HIGH);
digitalWrite(enbl_HL, HIGH);
digitalWrite(enbl_VR, HIGH);
digitalWrite(enbl_HR, HIGH);
//resett pin aktivieren
digitalWrite(PIN2RESET, HIGH);
}
void loop()
{
/*
// aktiviere enable pins:
digitalWrite(enbl_VL, LOW);
digitalWrite(enbl_HL, LOW);
digitalWrite(enbl_VR, LOW);
digitalWrite(enbl_HR, LOW);
*/
// bounce instance updaten:
debouncer.update();
// neuen bounce wert holen :
zaehler = debouncer.read();
if (zaehler == 1)
{
zaehler = 0;
// taste = 0;
if (irrecv.decode(&results))
{
taste = results.value;
Serial.println(taste);
// delay(1000);
// nächsten IR-wert empfangen
irrecv.resume();
}
}
tasten_abfrage();
}
/***********************************************************/
void tasten_abfrage(void)
{
switch (taste)
{
case 151 ://taste 1 große FB
{
if (taste == 151 )
{
Serial.println("szenario_1");
Serial1.println("szenario_1");
//fahre szenario_1
delay (1000);
break;
}
}
case 103://taste 2 große FB
{
if (taste == 103)
{
Serial.println("szenario_2");
Serial1.println("szenario_2");
//fahre szenario_2
delay (1000);
break;
}
}
case 79://taste 3 große FB
{
if (taste == 79)
{
Serial.println("szenario_3");
Serial1.println("szenario_3");
//fahre szenario_3
delay (1000);
break;
}
}
case 207://taste 4 große FB
{
if (taste == 207)
{
Serial.println("szenario_4");
Serial1.println("szenario_4");
//fahre szenario_4
delay (1000);
break;
}
}
case 253://OK taste, motor stop
{
if (taste == 253)
{
alle_stepper_stop();
break;
}
}
case 61:// rotate rechts große FB
{
if (taste == 61)
{
rechts_drehen();
break;
}
}
case 221:// rotate links große FB
{
if (taste == 221)
{
links_drehen();
break;
}
}
case 157:// fahre vor große FB
{
if (taste == 157)
{
vorwaerts();
break;
}
}
case 87:// fahre rückwärts große FB
{
if (taste == 87)
{
rueckwaerts();
break;
}
}
}
}
/***********************************************************/
void alle_stepper_stop(void)
{
//enable pins deaktivieren
digitalWrite(enbl_VL, HIGH);
digitalWrite(enbl_HL, HIGH);
digitalWrite(enbl_VR, HIGH);
digitalWrite(enbl_HR, HIGH);
reboot();
}
/***********************************************************/
void vorwaerts(void)
{
// enable pins aktivieren:
digitalWrite(enbl_VL, LOW);
digitalWrite(enbl_HL, LOW);
digitalWrite(enbl_VR, LOW);
digitalWrite(enbl_HR, LOW);
//richtung bestimmen
digitalWrite(dirPin_VL, LOW);
digitalWrite(dirPin_HL, LOW);
digitalWrite(dirPin_VR, HIGH);
digitalWrite(dirPin_HR, HIGH);
for (int i = 0; i < stepsPerRevolution; i++)
{
digitalWrite(stepPin_VL, HIGH);
digitalWrite(stepPin_HL, HIGH);
digitalWrite(stepPin_VR, HIGH);
digitalWrite(stepPin_HR, HIGH);
delayMicroseconds(250);
digitalWrite(stepPin_VL, LOW);
digitalWrite(stepPin_HL, LOW);
digitalWrite(stepPin_VR, LOW);
digitalWrite(stepPin_HR, LOW);
delayMicroseconds(250);
}
}
/**********************************************************/
void rueckwaerts(void)
{
// enable pins aktivieren:
digitalWrite(enbl_VL, LOW);
digitalWrite(enbl_HL, LOW);
digitalWrite(enbl_VR, LOW);
digitalWrite(enbl_HR, LOW);
//richtung bestimmen
digitalWrite(dirPin_VL, HIGH);
digitalWrite(dirPin_HL, HIGH);
digitalWrite(dirPin_VR, LOW);
digitalWrite(dirPin_HR, LOW);
for (int i = 0; i < stepsPerRevolution; i++)
{
digitalWrite(stepPin_VL, HIGH);
digitalWrite(stepPin_HL, HIGH);
digitalWrite(stepPin_VR, HIGH);
digitalWrite(stepPin_HR, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin_VL, LOW);
digitalWrite(stepPin_HL, LOW);
digitalWrite(stepPin_VR, LOW);
digitalWrite(stepPin_HR, LOW);
delayMicroseconds(500);
}
}
/***********************************************************/
void rechts_drehen(void)
{
// enable pins aktivieren
digitalWrite(enbl_VL, LOW);
digitalWrite(enbl_HL, LOW);
digitalWrite(enbl_VR, LOW);
digitalWrite(enbl_HR, LOW);
//richtung bestimmen
digitalWrite(dirPin_VL, LOW);
digitalWrite(dirPin_HL, LOW);
digitalWrite(dirPin_VR, LOW);
digitalWrite(dirPin_HR, LOW);
for (int i = 0; i < stepsPerRevolution; i++)
{
digitalWrite(stepPin_VL, HIGH);
digitalWrite(stepPin_HL, HIGH);
digitalWrite(stepPin_VR, HIGH);
digitalWrite(stepPin_HR, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin_VL, LOW);
digitalWrite(stepPin_HL, LOW);
digitalWrite(stepPin_VR, LOW);
digitalWrite(stepPin_HR, LOW);
delayMicroseconds(500);
}
}
/**********************************************************/
void links_drehen(void)
{
//enable pins aktivieren
digitalWrite(enbl_VL, LOW);
digitalWrite(enbl_HL, LOW);
digitalWrite(enbl_VR, LOW);
digitalWrite(enbl_HR, LOW);
//richtung bestimmen
digitalWrite(dirPin_VL, HIGH);
digitalWrite(dirPin_HL, HIGH);
digitalWrite(dirPin_VR, HIGH);
digitalWrite(dirPin_HR, HIGH);
for (int i = 0; i < stepsPerRevolution; i++)
{
digitalWrite(stepPin_VL, HIGH);
digitalWrite(stepPin_HL, HIGH);
digitalWrite(stepPin_VR, HIGH);
digitalWrite(stepPin_HR, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin_VL, LOW);
digitalWrite(stepPin_HL, LOW);
digitalWrite(stepPin_VR, LOW);
digitalWrite(stepPin_HR, LOW);
delayMicroseconds(500);
}
}
/***********************************************************/
void reboot()
{
pinMode(PIN2RESET, OUTPUT);
digitalWrite(PIN2RESET, LOW);
delay(100);
}
/***********************************************************/
/************************************************************/
der letzter stand ist: halbschritt und 2,5ms zwischen HIGH und LOW, damit fährt er 1m in ca 2sekunden. Das reicht erstmal, würde ich sagen. Damit schafft er auch eine 20° steigung, mehr konnte ich innen nicht testen und mehr haben wir hier kaum an der Elbe
Code:
// https://microcontrollerslab.com/esp32-web-server-arduino-led/#Displaying_HTML_web_server_with_ESP32
#include <Arduino.h>
#include <analogWrite.h>
#include <WiFi.h>
#include <WebServer.h>
double velo = 0;
const char* ssid = "ESP32-lok"; //PTCL-BB
const char* password = "12345678"; //5387c614
WiFiServer server(80);
String header;
String LED_ONE_STATE = "vorwaerts";
String LED_TWO_STATE = "aus";
String LED_THREE_STATE = " 0 ";
String LED_FOUR_STATE = "aus";
//String MOTOR_STATE = "off";
//String DIRECTION_STATE = "off";
const int GPIO_PIN_NUMBER_5 = 5;
const int GPIO_PIN_NUMBER_18 = 18;
const int GPIO_PIN_NUMBER_22 = 22;
const int GPIO_PIN_NUMBER_19 = 19;
//const int GPIO_PIN_NUMBER_9 = 9; //motor
//const int GPIO_PIN_NUMBER_10 = 10;//direction
IPAddress local_ip(192, 168, 1, 101);
IPAddress gateway(192, 168, 1, 101);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
Serial.print("code----ESP_32/pico_lok/HTTP_AP_server_motor_control_8_buttons_velo");
pinMode(GPIO_PIN_NUMBER_5, OUTPUT);
pinMode(GPIO_PIN_NUMBER_18, OUTPUT);
pinMode(GPIO_PIN_NUMBER_22, OUTPUT);
pinMode(GPIO_PIN_NUMBER_19, OUTPUT);
// pinMode(GPIO_PIN_NUMBER_9, OUTPUT);
// pinMode(GPIO_PIN_NUMBER_10, OUTPUT);
digitalWrite(GPIO_PIN_NUMBER_5, LOW);
digitalWrite(GPIO_PIN_NUMBER_18, LOW);
digitalWrite(GPIO_PIN_NUMBER_22, LOW);
digitalWrite(GPIO_PIN_NUMBER_19, LOW);
// digitalWrite(GPIO_PIN_NUMBER_9, LOW);
// digitalWrite(GPIO_PIN_NUMBER_10, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
/*
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Trying to connect to Wifi Network");
}
*/
Serial.println("");
Serial.println("Successfully connected to WiFi network");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client)
{
Serial.println("New Client is requesting web page");
String current_data_line = "";
while (client.connected())
{
if (client.available())
{
char new_byte = client.read();
Serial.write(new_byte);
header += new_byte;
if (new_byte == '\n')
{
if (current_data_line.length() == 0)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// lok vorwärts/rückwärts
if (header.indexOf("LED1=ON") != -1)
{
Serial.println("GPIO5 LED is ON");
LED_ONE_STATE = "vorwaerts";
digitalWrite(GPIO_PIN_NUMBER_5, HIGH);
}
if (header.indexOf("LED1=OFF") != -1)
{
Serial.println("GPIO5 LED is OFF");
LED_ONE_STATE = "rueckwaerts";
digitalWrite(GPIO_PIN_NUMBER_5, LOW);
}
// lok an/aus
if (header.indexOf("LED2=ON") != -1)
{
Serial.println("GPIO18 LED is ON");
LED_TWO_STATE = "an";
analogWrite(GPIO_PIN_NUMBER_18, velo); //HIGH);
}
if (header.indexOf("LED2=OFF") != -1)
{
Serial.println("GPIO18 LED is OFF");
LED_TWO_STATE = "aus";
analogWrite(GPIO_PIN_NUMBER_18, LOW);
}
//lok schneller/langsammer
if (header.indexOf("LED3=ON") != -1)
{
Serial.println("GPIO22 LED is ON");
velo = velo + 5;
LED_THREE_STATE = velo; //"on";
//analogWrite(GPIO_PIN_NUMBER_22, velo);//HIGH
}
if (header.indexOf("LED3=OFF") != -1)
{
Serial.println("GPIO22 LED is OFF");
velo = velo - 5;
LED_THREE_STATE = velo; //"off";
//analogWrite(GPIO_PIN_NUMBER_22, velo); //LOW
}
if (header.indexOf("LED4=ON") != -1)
{
Serial.println("GPIO19 LED is ON");
LED_FOUR_STATE = "an";
digitalWrite(GPIO_PIN_NUMBER_19, HIGH);
}
if (header.indexOf("LED4=OFF") != -1)
{
Serial.println("GPIO19 LED is OFF");
LED_FOUR_STATE = "aus";
digitalWrite(GPIO_PIN_NUMBER_19, LOW);
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: 2px solid #4CAF50;; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 1px 1px; cursor: pointer; }");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
// Web Page Heading
client.println("</style></head>");
//client.println("<body><center><h1>ESP32 Web server LED controlling example</h1></center>");
//client.println("<center><h2>Web Server Example Microcontrollerslab.com</h2></center>" );
//client.println("<center><h2>Press on button to turn on led and off button to turn off LED</h3></center>");
client.println("<form><center>");
client.println("<p> richtung ist " + LED_ONE_STATE + "</p>");
client.println("<button class=\"button\" name=\"LED1\" value=\"ON\" type=\"submit\">VORW</button>") ;
client.println("<button class=\"button\" name=\"LED1\" value=\"OFF\" type=\"submit\">RUECKW</button></center>");
client.println("<p>motor ist " + LED_TWO_STATE + "</p>");
client.println("<button class=\"button\" name=\"LED2\" value=\"ON\" type=\"submit\">AN</button>");
client.println("<button class=\"button\" name=\"LED2\" value=\"OFF\" type=\"submit\">AUS</button></center>");
client.println("<p>geschwindigkeit ist " + LED_THREE_STATE + "</p>");
client.println("<button class=\"button\" name=\"LED3\" value=\"ON\" type=\"submit\">SCHNELLER</button>");
client.println("<button class=\"button\" name=\"LED3\" value=\"OFF\" type=\"submit\">LANGSAMMER</button></center>");
client.println("<p>kamera ist " + LED_FOUR_STATE + "</p>");
client.println("<button class=\"button\" name=\"LED4\" value=\"ON\" type=\"submit\">AN</button>");
client.println("<button class=\"button\" name=\"LED4\" value=\"OFF\" type=\"submit\">AUS</button></center>");
client.println("</center></form></body></html>");
client.println();
break;
}
else
{
current_data_line = "";
}
}
else if (new_byte != '\r')
{
current_data_line += new_byte;
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
sollte umgeschrieben werde, war ursprünglich für die autonome TT - lok geschrieben, bzw. abgewandelt worden...
Lesezeichen