Hey, I'm new to programming and am trying to use both Arduino and C sharp to run a relay board (R410Pro) off of an Arduino Mega 2560. The set up is the USB connects to the Mega and the Relay is connected to the Mega via RS232.
As of now, the program is set up so that the Console in C sharp opens while the program runs. The user then enters a 0 or 1 which turns on/off the Mega LED (no problems so far). The Arduino is programmed to send whatever is written in the console to the relay. Thus if the correct numbers are entered, it should turn on/off different relays and their respective LED's. Instead, nothing happens.
Ideally the goal would be to control the relays using buttons on the Arduino, so if you have any advice on how to set that instead of/inaddition to help with running it out of the console, I would appreciate it greatly.
Here is the code. C sharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace test
{
class LED
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM4", 9600);
port.Open();
while (true)
{
Console.WriteLine("open");
String s = Console.ReadLine();
if (s.Equals("exit"))
{
break;
}
port.Write(s + '\n');
}
string sInput;
sInput = Console.ReadLine();
Console.WriteLine(sInput);
port.Write(input);
port.Close();
}
}
}
Arduino code
#define BAUD_RATE 9600
#define ledPin 13
void setup()
{
pinMode(ledPin, OUTPUT);
// serial communication
Serial.begin(BAUD_RATE);
Serial2.begin(BAUD_RATE); //serial comms with relay board
}
void loop()
{
if(Serial.available()>0){ //available means it is receiving data
byte(Serial.read);
Serial2.write(inByte);
//protocol for Mega LED on/off
switch(inByte){
case '1':
// led is ON
digitalWrite(ledPin, HIGH);
break;
case '0':
// led is OFF
digitalWrite(ledPin, LOW);
break;
}
}
if(Serial2.available()>0){ //check if serial 2 is recieving data
//and have it send it to c sharp to be displayed
byte inByte = Serial2.read();
Serial2.write();
}
}
Thank you.