Hi
I have almost reach to the end of testing Arduino for my project. If you are looking bellow you can see that the outcomming stream from Arduino is is like:
D0-(1-9)=(0/1)
D(10-53)=(0/1)
The Arduino are sending a stream containing all the states for each digital pins, see the code bellow:
My sketch for the Arduino board (loop function):
void setup()
{
Serial.begin(9600);
// Set pinMode for the board pins ===========================================
.....................
// ==========================================================================
}
void loop()
{
// Declaration of containers
String controlStr, controlStrType, controlStrPinType, controlStrPin, controlStrBool, readString;
// ======================== Save the (input / input_pullupp ) pinstates in an array of lenght = 54 ===============================
int pinStatesArr[54];
for (int i = 0; i < pinAmount; i = i + 1){
if(i == 0 || i == 1) pinStatesArr[0] = -1;
else pinStatesArr[i] = digitalRead(digitalPinsArr[i]);
}
// ======================== Write state of the changing pin
// If loopIndex is bellow 10 add a zero next to 2-53
if(loopIndex < 10){
if(pinStatesArr[loopIndex] == 0) Serial.println(String('D')+String('0')+loopIndex+String("=0")); // Opened = INPUT_PULLUP = 0
if(pinStatesArr[loopIndex] == 1) Serial.println(String('D')+String('0')+loopIndex+String("=1")); // Closed = INPUT_PULLUP = 1
}
if(loopIndex >= 10){
if(pinStatesArr[loopIndex] == 0) Serial.println(String('D')+loopIndex+String("=0")); // Pin = Opened = INPUT_PULLUP = 0
if(pinStatesArr[loopIndex] == 1) Serial.println(String('D')+loopIndex+String("=1")); // Pin = while(Serial.available()){
delay(1);
if(Serial.available()>0){
char incommingString = Serial.read();
if(isControl(incommingString)){
break;
}
// Save the string and add the new one
readString += incommingString;
// Save the string in more of a global container reacheable further
controlStr = readString;
}
}
loopIndex++;
if (loopIndex == pinAmount) loopIndex = 0;
}
Inside the C# program is a while loop tha will handle the string (serialPort.ReadLine())
My class for "ConnectionArduinoD1" (C# side):
using ....
namespace FSPanel_App___Backend
{
class ConnectionArduinoD1
{
static string type;
static string pinNr;
static string pinState;
static string[] boardsPinstates = new string[]{
"state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state",
"state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state",
"state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state" };
static bool firstRunOpen = true;
static bool connect;
static SerialPort serialPort;
public static void BoardStart()
{
Thread readThread = new Thread(Read);
serialPort = new SerialPort();
OpenSerialPort();
Console.WriteLine($"Connected to port {serialPort.PortName} at {serialPort.BaudRate} baud.");
readThread.Start();
}
public static void Read()
{
while (connect)
{
try
{
var message = serialPort.ReadLine();
Console.WriteLine(message);
SplitString(message);
if (message == "D2=Open\r") boardsPinstates[2] = "Open";
if (message == "D2=Close\r") boardsPinstates[2] = "Close";
if (message == "D7=Open\r") boardsPinstates[7] = "Open";
if (message == "D7=Close\r") boardsPinstates[7] = "Close";
Switch3Poss();
}
catch (TimeoutException) { }
firstRunOpen = false;
//serialPort.WriteLine("");
}
}
private static void SplitString(string messagesSplit)
{
type = messagesSplit.Substring(0, 1);
pinNr = messagesSplit.Substring(1, 2);
pinState = messagesSplit.Substring(4, 1);
}
private static void Switch3Poss()
{
/*Control string for Arduinos component commands. There are different typ according the components. It starts = inp/out
inp:
Switch - 2 / 3 poss (Middle poss =
out:
_a/d_01-53_1/0
*/
if (boardsPinstates[2] == "Open" && boardsPinstates[7] == "Close") {
serialPort.WriteLine("out_d_10_1");
}
if(boardsPinstates[2] == "Close" && boardsPinstates[7] == "Close")
{
serialPort.WriteLine("out_d_11_1");
firstRunOpen = false;
}
if(boardsPinstates[2] == "Close" && boardsPinstates[7] == "Open")
{
serialPort.WriteLine("out_d_10_0");
serialPort.WriteLine("out_d_11_0");
}
}
private static void OpenSerialPort()
{
serialPort.PortName = "Com4";
serialPort.BaudRate = 9600;
serialPort.ReadTimeout = 5000;
serialPort.WriteTimeout = 5000;
serialPort.Open();
connect = true;
Console.WriteLine($"Connected to port {serialPort.PortName} at {serialPort.BaudRate} baud.");
}
}
}
All works fine but I will add additional behavior. I need a funktion in Arduino that will immediately send the current pinstate of the changing ping. I have search for it and ask in a forum. I was told that I need some sort of "interrupt" function
-->
You have a table next to hte first text subparagraph for "Mega, Mega2560, MegaADK" you can read that this function just work for digitalpins "2, 3, 18, 19, 20, 21".
It causing a problem since I have about 30 switches for almost every boards.
The question is am I able to find a solutions so I can use Arduino?
Look at the txt file
Best regards Fredrik
Code.txt (10.1 KB)