Reputation: 51
I am confused a bit. I have written the VB code to make ON and OFF the LED connected to Arduino. I am sending data from VB app over COM port (instead of serial monitor) and the data is '1' for LED ON and '0' for OFF. Here I want to send this signal through RF-433 module. I have connected the TX pin of Arduino to Data pin of the RF module. On other hand, the second Arduino is connected to RF receiver with LED on Pin 12. Now I am not getting how to write code for Arduino of TX side to send data through RF? I mean if I use serial monitor to send data, then Serial.available()
and Serial.read()
can be used to send data over serial monitor with help of keyboard, but here I am sending that data from VB app. So what is the code for Arduino to activate RF TX connected on TX pin of Arduino?
Here is my VB code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com12" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
picOn.Visible = True
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
picOn.Visible = False
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
End Class
Upvotes: 3
Views: 9627
Reputation: 307
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com12" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
picOn.Visible = True
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
picOn.Visible = False
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
End Class
but I would suggest if you can send a byte not a bit once on a button_click, and check your baud rate , for 433mhz module it is good to use the as least baud rate as possible. If your data is not that much bigger use 1200bps, and set both micro controller with same baudrate
Upvotes: 0
Reputation: 51
Huh.... Finally did it... Following code is working successfully. I used SoftwareSerial library. The Tx code is simple and can be implemented without any library. I just took data from VB app on RX pin of arduino and sent it to the TX of arduino to which the RF module is connected. The receiver requires software serial library.
Tx Code :
WITHOUT LIBRARY.
int inByte;
void setup()
{
Serial.begin(2400);
}
void loop()
{
if(Serial.available()>0)
{
inByte=Serial.read();
switch(inByte)
{
case '0':
Serial.write(inByte);
break;
case '1':
Serial.write(inByte);
break;
default:
break;
delay(100);
}
}
}
WITH LIBRARY.
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial(10,11); //RX & TX
int ch;
void setup()
{
pinMode(rxPin,INPUT);
pinMode(txPin,OUTPUT);
Serial.begin(9600);
//Serial.println("Hi");
mySerial.begin(2400);
//mySerial.println("Hello");
}
void loop()
{
if(Serial.available()>0)
{
ch=Serial.read();
mySerial.write(ch);
}
}
RX CODE:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial(10,11); //RX & TX
int ch=0;
void setup()
{
pinMode(rxPin,INPUT);
pinMode(13,OUTPUT);
//pinMode(txPin,OUTPUT);
Serial.begin(9600);
//Serial.println("Hi");
mySerial.begin(2400);
//mySerial.println("Hello");
}
void loop()
{
if(mySerial.available()>0)
{
ch=mySerial.read();
//Serial.write(ch);
switch(ch)
{
case '0':
digitalWrite(13,LOW);
break;
case '1':
digitalWrite(13,HIGH);
break;
default:
break;
}
}
}
Btw thanx a lot @Yve for the guidance and the time you gave me to complete this code... :) implementation.
Upvotes: 2
Reputation:
First you have declared _serialPort As SerialPort
and then proceeded to use SerialPort1
You need to test if serial port is open, as shown below. Opening (or closing) a port that is already open will throw an error.
You have no start or stop bits for your read and write.
Public Class Form1
' unsure what this is being used for
Shared _continue As Boolean
' you had not declared SerialPort1
Shared SerialPort1 As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I don't understand why you are closing the port???
SerialPort1.Close()
'A statement like this would be better to check if it is open
If SerialPort1.IsOpen = True Then
SerialPort1.close()
SerialPort1.PortName = "com12" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
picOn.Visible = True
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
SerialPort1.Write("1")
End if
SerialPort1.Close()
End Sub
Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
picOn.Visible = False
If SerialPort1.IsOpen = True Then
SerialPort1.Write("0")
SerialPort1.Close()
End if
End Sub
End Class
Edit
See this, from the following link and incorporate your if statements into your button on and off events.
int SerialValue = 0;
void setup(){
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop(){
SerialValue = Serial.read();
if(SerialValue == 50){
digitalWrite(13, HIGH);
}
if(SerialValue == 10){
digitalWrite(13, LOW);
}
}
http://forum.arduino.cc/index.php/topic,8566.0.html
I would also suggest looking at this site:
http://competefornothing.com/?p=738
I know you are on this site and I would recommend utilizing it thoroughly:
Upvotes: 1