hi there,
I am making a program for a project that takes in a variable from the serial port and uses it to control a set of magnetic locks (currently simulated with LEDs). I am getting the value from a Visual Basic program I made. The VB code works, as I have tested it from other programs. There are 2 parts that run arduino in the project. The first is the arduino used to decipher the code from the computer. i used the following code for this part:
#include <SoftwareSerial.h>
SoftwareSerial door1(2,3); //setup of the serial port for door 1. RX on pin 2 tx on pin 3
SoftwareSerial door2(4,5); //setup of the serial port for door 2. RX on pin 4 tx on pin 5
SoftwareSerial door3(6,7); //setup of the serial port for door 3. RX on pin 6 tx on pin 7
int door[3]={0,0,0}; //holding variables for the converted value of each door from computer value
int input[3]={8,8,8}; //holding variables for the status of each door
int use[3]={0,0,0}; //variables used to hold the converted door status
int compVal=0; //value recieved from computer
int trans = 0; //holding variable for data to be transmited to the computer
int transOld = 0; //last value that was sent to the computer
/******************************************************************************
*
* Section: setup()
* Purpose: setup the main parts
* of the code on startup
*
*
******************************************************************************/
void setup(){
Serial.begin(9600); //Start comunications with serial port
door1.begin(9600); //Start comunications with door 1 port
door2.begin(9600); //Start comunications with door 2 port
door3.begin(9600); //Start comunications with door 3 port
}
/******************************************************************************
*
* Section: loop()
* Purpose: Section that holds the looping code for the arduino.
* Takes in the value sent from the computer, converts
* it to 1's and 2's based on open or closed respectivaly
* for each door. transmits those values to their respective
* door. Then, the arduino looks to see what the status
* of each dor is based on the returned value on teh RX
* pin, conerts it to a usable value and transmits it back
* to the computer.
*
********************************************************************************/
void loop(){
if(Serial.available()>0){ //if there is a transmission from the computer, comtinue
compVal=Serial.read()-'0'; //read value from computer. deduct ascii value of '0' to find numeric value of sent number. found from http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/
decode(compVal); //See decode funtion
}
door1.write(door[0]); //transmit found value for door 1 (1=locked, 2=unlocked)
door1.listen(); //listen to port for data
if(door1.available()>0){ //if data on port,
input[0]=door1.read(); //read data from port
}
door2.write(door[1]); //transmit found value for door 2 (1=locked, 2=unlocked)
door2.listen(); //listen to port for data
if(door2.available()){ //if data on port,
input[1]=door2.read(); //read data from port
}
door3.write(door[2]); //transmit found value for door 3 (1=locked, 2=unlocked)
door3.listen(); //listen to port for data
if(door3.available()){ //if data on port,
input[2]=door3.read(); //read data from port
}
trans = use[0]+use[1]+use[2]; //add found values from the door status to get data to be transmitted
if(trans != transOld){ //compare value to be transmission to the last value transmitted, if different, then:
Serial.print(trans); //transmit the value to the computer
transOld=trans; //store the value transmitted for later comparison
}
}
/*********************************************************************
* Section: decode(val) *
* Purpose: Takes the value read from the serial port and *
* converts it to a usable value *
* Variables: val: the value that was taken from the computer *
* door[]: the value that is to be printed to the doors *
* *
*********************************************************************/
int decode(int val){
switch(val){
case 0:
door[0]=2;
door[1]=2;
door[2]=2;
break;
case 1:
door[0]=1;
door[1]=2;
door[2]=2;
break;
case 2:
door[0]=2;
door[1]=1;
door[2]=2;
break;
case 3:
door[0]=1;
door[1]=1;
door[2]=2;
break;
case 4:
door[0]=2;
door[1]=2;
door[2]=1;
break;
case 5:
door[0]=1;
door[1]=2;
door[2]=1;
break;
case 6:
door[0]=2;
door[1]=1;
door[2]=1;
break;
case 7:
door[0]=1;
door[1]=1;
door[2]=1;
break;
}
}
/********************************************************************
*
* section: code(val1, val2, val3)
* purpose: This section of code takes the 3 status values from the
* doors and converts them to their coded values. door1 is1,
* door2 is 2 and door3 is 4.
*
*********************************************************************/
int code(int val1, int val2, int val3){
switch(val1){
case '1':
use[0]=1;
break;
case '2':
use[0]=0;
break;
}
switch(val2){
case '1':
use[1]=2;
break;
case '2':
use[1]=0;
break;
}
switch(val3){
case '1':
use[2]=4;
break;
case '2':
use[2]=0;
break;
}
}
The second part is the arduino connected to the door. It takes in the code sent from the computer's arduino on the SoftwareSerial port and sets the appropriate pin to high or low. The arduino then checks the voltage on the LED and returns a values based on if it HIGH or LOW to the computers arduino. This is the code for the doors:
//code for second door
#include <SoftwareSerial.h>
SoftwareSerial doors(5, 4); //initialise comunication port. RX on pin 5, tx on pin 4
int led=12; //set LED pin
int input=8; //set the status read pin for lock/led
int stat=0; //holding value for status of input pin
int value=0; //holding value for data from computer's arduino
int valueOld=0; //Previous value sent back to computer's arduino
int value2=0; //Value to be sent back to computer's arduino
void setup(){
doors.begin(9600); //start comunication with port
pinMode(led, OUTPUT); //initialize led pin as output
pinMode(input,INPUT); //initialize input pin as INPUT
}
/********************************************************************************
*
* In this sectionof code, the arduino takes in the value sent down from
* the computer's arduino. using this value, it turns the lock/led on or
* off. Next, the arduino looks at the status of the lock/led and sends
* a corrsponding signal back to the computer's arduino. If the door it
* locked (off) it sends back a 2, if it is off locked (on) it sends back
* a 1.
*
*********************************************************************************/
void loop(){
value=doors.read(); //read the value sent from the computer's arduino
if(value==1){
digitalWrite(led, HIGH);
}
if(value==2){
digitalWrite(led, LOW);
}
stat=digitalRead(input); //check status of the
if(stat==HIGH){
value2=1;
}
else{
value2=2;
}
if(value2 != valueOld){
doors.write(value2);
valueOld=value2;
}
}
Now, my problem comes in the feedback loop. I am not getting a value other than 0 on the serial port of the computer's arduino, regardless of the status of the doors. Any advice would be greatly welcome.