Moin !
nun haben sich einige Antworten angesammelt. Ich versuche auf alle einmal einzugehen. Danke erst einmal an alle.
@Tommy56: war in Eile und verschlammt. Gelobe Besserung.
@HotSytems: Wie sollte ich das denn machen - Code steht am Ende (am Ende vereinfacht!).
es soll eine Weichensteuerung geben und zu Beginn auf eine Grundeinstellung gesetzt werden. Das Sketch arbeitet mit Roboremo auf dem Tablet zusammen.
Ich möchte einmal alles auf eine Basis stellen, das geht auch ohne auf BT zu warten - aber ich möchte auch die Grafik auf Roboremo nachführen und dafür brauche ich einen Hinweis, dass die BT steht.
Einen HC-05 hätte ich noch liegen, aber dann fehlt mir vermutlich ein PIN, da ich schon D2 bis D13 verwende. BT hängt an D11 und D12 damit ich den USB weiter verwenden kann. Oder kann ich noch andere PINS sonst für den BT nehmen.
Ach ja, das Ganze auf einem Nano.
Gruß Jan
Hier der Code im Ganzen
// Control Arduino led over Bluetooth using RoboRemo app
// www.roboremo.com
// Hardware setup:
// BT module Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin10
// RX-I ------ pin11
// Arduino UNO already has an LED attached to pin 13
// --- Gleisbild
// W1 W1
// +--------+-------- Gleis 1 -----------+--------+
// | +-------- Gleis 2 -----------+ |
// | |
// | |
// | +--- G3 -----------+ |
// | +--- G4 -----+ | |
// | +--- G5 -----+-------+ W3 |
// | W4 | |
// | STOP | |
// +---------------X- Gleis 6 ----+-W2------------+
//
// Bedeutung der Weichenschaltung in SwitchAction(int Number, int Direction)
// Parameter NUMBER
// Nummer der Weiche. Beginnt mit 1 und wird intern um eines heruntergesetzt für das struct-Element
// Parameter DIRECTION
// -1 ... NEUTRAL (-1) - nur das Roboremo-Icon auf Bild 1 (alles schwarz) ändern.
// 0 ... GERADEAUS
// 1 ... ABBIEGEN
#include <SoftwareSerial.h>
int bluetoothTx = 11;
int bluetoothRx = 10;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int led = 13;
int connection_status = 0;
int counter = 0;
char cmd[100];
int cmdIndex;
#define LED 13 // Onbaord Led zum Signalisieren des Schaltimpulses
//#define LED_Taste1G 2 // Taster Gerade - Weiche 1
//#define LED_Taste1A 3 // Taster Abbiegen - Weiche 1
//#define LED_Weiche1G 8 // LED Gerade - Weiche 1
//#define LED_Weiche1A 9 // LED Abbiegen - Weiche 1
#define Weichenanschluss1 4 // 1. Kabel von der Weiche 1
#define Weichenanschluss2 5 // 2. Kabel von der Weiche 1
#define Weichenanschluss3 6 // 1. Kabel von der Weiche 2
#define Weichenanschluss4 7 // 2. Kabel von der Weiche 2
#define Impulszeit 20
// Zeit in Millisekunden - Standard 20
int taster_val = 0; // Tasterwert init
struct _Weiche {
byte pin_straight;
byte pin_turn;
};
_Weiche Weiche[4];
void setup() {
// Init bluetooth
delay(500); // wait for bluetooth module to start
// Belegung der Weichen-Pin
Weiche[0].pin_straight = 2;
Weiche[0].pin_turn = 3;
Weiche[1].pin_straight = 4;
Weiche[1].pin_turn = 5;
Weiche[2].pin_straight = 6;
Weiche[2].pin_turn = 7;
Weiche[3].pin_straight = 8;
Weiche[3].pin_turn = 9;
bluetooth.begin(9600); // Bluetooth default baud is 115200 - change to 9600
Serial.begin(9600);
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$"); // enter cmd mode
delay(250);
// My Bluetooth default baud is 115200
// If your Bluetooth uses other baud rate,
// then you must modify it here,
// otherwise it will not work.
// for HC-05/06 it is usually 9600
bluetooth.println("U,9600,N"); // change baud to 9600
bluetooth.begin(9600);
// Init allgemeines
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
cmdIndex = 0;
Serial.print("Suche nach Bluetooth ...");
// Schleife durchlaufen, bis Bluetooth erreichbar
while (connection_status == 0) {
if(bluetooth.available()) {
connection_status = 1;
Serial.print("... gefunden");
} else {
counter++;
Serial.print(counter);
}//endif-bluetooth
}//end-while
bluetooth.println("+----------------------+");
bluetooth.println("| Weichenstrasse M-2-4 |");
bluetooth.println("+----------------------+");
Serial.println("+----------------------+");
Serial.println("| Weichenstrasse M-2-4 |");
Serial.println("+----------------------+");
// jetzt Init der Weichenstraße
}//end-setup
void loop() {
// hier sollte nichts mehr stehen vor dem Auslesen des Bluetooth
if(bluetooth.available()) {
//-------------------------------- Beginn ----------------------
if (connection_status == 1){
//bluetooth.println("+----------------------+");
//bluetooth.println("| Weichenstrasse M-2-4 |");
//bluetooth.println("+----------------------+");
bluetooth.println("led_status 1\n");
connection_status = 1;
Reset_Back(); // Strecken in Grundstellung
Reset_Front();// Strecken in Grundstellung
GotoGleis01(); // Ausgangsgleis 1, dammit eine Ausgangssituation bekannt ist
GotoGleis06(); // Ausgangsgleis 0, für den vorderen Bereich
// } else {
// connection_status = 0;
}
//-------------------------------- END ----------------------
char c = (char)bluetooth.read();
if(c=='\n') {
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
}
}
}//end-loop
void exeCmd() {
// "led" is the led id
if(strcmp(cmd, "bt_reset")==0) Reset();
if(strcmp(cmd, "bt_g01")==0) GotoGleis01();
if(strcmp(cmd, "bt_g02")==0) GotoGleis02();
if(strcmp(cmd, "bt_g03")==0) GotoGleis03();
if(strcmp(cmd, "bt_g04")==0) GotoGleis04();
if(strcmp(cmd, "bt_g05")==0) GotoGleis05();
if(strcmp(cmd, "bt_g06")==0) GotoGleis06();
}//end-exeCmd
// Schalten einer Weiche
// Direction - -1 ... NEUTRAGL 0 ...straight 1 ...turn
// Number, damit es mit der WeichenNummerierung besser klappt
void SwitchAction(int Number, int Direction)
{
int Index;
Index = Number - 1;
char buf[20];
if (Direction == 0){ // geradeaus
digitalWrite(Weiche[Index].pin_straight, HIGH);
delay(Impulszeit);
digitalWrite(Weiche[Index].pin_straight, LOW);
sprintf(buf, "img_sw%02d i 1\n", Number); // Steuerung Roboremo-Icons
} else if (Direction == 1) { // abbiegen
digitalWrite(Weiche[Index].pin_turn, HIGH);
delay(Impulszeit);
digitalWrite(Weiche[Index].pin_turn, LOW);
sprintf(buf, "img_sw%02d i 2\n", Number); // Steuerung Roboremo-Icons
} else { // Roboremo-neutral (nur schwarz) schalten
sprintf(buf, "img_sw%02d i 0\n", Number);
}
bluetooth.println(buf);
}//end-SwitchAction
// --------------------------- Weichen und Gleis-Icons
void GotoGleis01-6()
{
}//end-GotoGleis01
// --------------------------- Gleise zurücksetzen
void Reset () // alle auf deaktiv setzen
{
Reset_Back();
Reset_Front();
}
// Schalten der Weichendarstellung
void Reset_Back () // alle auf deaktiv setzen
{
}
// Schalten der Weichendarstellung
void Reset_Front () // alle auf deaktiv setzen
{
}