OK, das ist schon mal gut.
das ist das Sketch
#include "TropfenBox.h"
String commandString;
boolean commandStringReceived = false;
TropfenBox tropfenBox;
int startPin = 2;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop()
{
if(commandStringReceived)
{
commandStringReceived = false;
tropfenBox.ExecuteCommand(commandString);
commandString = "";
}
if(digitalRead(startPin)== HIGH)
{
tropfenBox.Run();
}
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == '\r')
{
commandStringReceived = true;
}
else
{
commandString += inChar;
}
}
}
und "TropfenBox.h"
#include <Arduino.h>
#define NAME "Tropfen Box"
#define FIRMWARE "1.2.0"
#define ENDLN "\n"
#define MAXELEMENTS 201
#define STARTPIN 2
class TropfenBox
{
struct Parameter
{
int pin;
long vorlaufzeit;
long dauer;
Parameter(){
}
Parameter(int pin, long vorlaufzeit, long dauer) :
pin(pin), vorlaufzeit(vorlaufzeit), dauer(dauer) {
}
};
struct Step
{
int pin;
long time;
boolean state;
Step(){
}
Step(int pin, long time, boolean state) :
pin(pin), time(time), state(state) {
}
};
private:
Parameter configuration[MAXELEMENTS];
private:
Step steps[MAXELEMENTS * 2];
private:
int counter;
private:
int stepCounter;
public:
TropfenBox():
counter(0), stepCounter(0)
{
Init();
}
public:
void ExecuteCommand(String command)
{
String response = "";
int commaPosition;
int argCounter = 0;
int maxArgs = 10;
String args[maxArgs];
do
{
if(argCounter > maxArgs)
{
Serial.println("NOK,Too many arguments");
return;
}
commaPosition = command.indexOf(',');
if(commaPosition != -1)
{
args[argCounter] = command.substring(0,commaPosition);
command = command.substring(commaPosition+1, command.length());
argCounter++;
}
else
{
if(command.length() > 0)
{
args[argCounter] = command;
argCounter++;
}
}
}
while(commaPosition >= 0);
if(argCounter > 0)
{
command = args[0];
}
if(command == "get_name")
{
response = "OK," + GetName()+ ENDLN;
}
else if(command == "get_firmware")
{
response = "OK," + GetFirmware() + ENDLN;
}
else if(command == "configure") //Format:configure,pin,vorlaufzeit,dauer
{
int pin = args[1].toInt();
// Nötig?
if (pin > STARTPIN && pin < 21) // && Zeile > -1 && Zeile < 200)
{
InsertParameter(pin, args[2].toInt() * 1000, args[3].toInt() * 1000);
response = "OK\n";
}
else
{
response = "NOK, Pin number out of range[2..20]\n";
}
}
else if(command == "print_config")
{
PrintConfig();
response = "OK\n";
}
else if(command == "print_steps")
{
PrintSteps();
response = "OK\n";
}
else if(command == "run")
{
Run();
response = response = "OK\n";
}
else
{
response = "NOK, Unknown command: " + command + ENDLN;
}
Serial.println(response);
}
public:
void Run()
{
ConfigureOutputPins();
unsigned long looptime;
unsigned long starttime = micros();
for(int i = 0; i < stepCounter ; i++)
{
while ((looptime = micros() - starttime) < steps[i].time);
digitalWrite(steps[i].pin, steps[i].state);
}
}
public:
String GetFirmware()
{
return FIRMWARE;
}
public:
String GetName()
{
return NAME;
}
private:
void Init()
{
InsertParameter(21, 10000, 1000000);
InsertParameter(20, 500000, 400000);
InsertParameter(19, 1300000, 30000);
}
private:
void PrintConfig()
{
String line;
for(int i = 0; i < counter; i++)
{
line = String(configuration[i].pin, DEC) + "\t";
line += String(configuration[i].vorlaufzeit, DEC) + "\t";
line += String(configuration[i].dauer, DEC) + ENDLN;
Serial.println(line);
}
}
private:
void PrintSteps()
{
String line, flag;
for(int i = 0; i < stepCounter; i++)
{
if(steps[i].state)
flag = "true";
else
flag = "false";
line = String(steps[i].pin, DEC) + "\t";
line += String(steps[i].time, DEC) + "\t";
line += flag + ENDLN;
Serial.println(line);
}
}
// Neue Konfiguration in das Array einfügen
private:
void InsertParameter(int pin, long vorlaufzeit, long dauer)
{
configuration[counter] = Parameter(pin, vorlaufzeit, dauer);
Step start = Step(pin, vorlaufzeit, true);
Step stop = Step(pin, vorlaufzeit + dauer, false);
boolean flag = false;
// Zwei Elemente in Steps Liste einsortieren
if(stepCounter > 1)
{
for(int i = stepCounter -1; i >= 0; i--)
{
if(stop.time > steps[i].time)
{
steps[i+1] = stop;
stepCounter++;
break;
}
else
{
steps[i+1] = steps[i];
}
}
for(int i = stepCounter -1; i >= 0; i--)
{
if(start.time > steps[i].time)
{
steps[i+1] = start;
stepCounter++;
break;
}
else
{
steps[i+1] = steps[i];
}
}
}
else
{
steps[0] = start;
steps[1] = stop;
stepCounter = 2;
}
counter++;
}
private:
void ConfigureOutputPins()
{
for(int i = 0; i < counter; i++)
{
pinMode(configuration[i].pin, OUTPUT);
}
}
};