Pages: [1]   Go Down
Author Topic: Noobie help please  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, my name is Adam and I'm very new to the Arduino world but very excited to get my hands dirty. I've spent hours searching online for some answers to a project I have in mind but haven't found what I'm looking for or maybe I did but didn't realize it was the answer I needed. My project is: getting Arduino to read hex messages 1 wire bus communication system or from a serial line and convert specific strings to relay switching on/off. I have about 4-6 specific hex strings that I'd like each 1 to activate a different relay if possible. Any help would be greatly appreciated.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 149
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Adam,

this is a short example to switch 4 relais on and off over the serial line.
Code:
// change the pin numbers if necessary
int relais1 = 2; // relais 1 at pin 2
int relais2 = 3; // relais 2 at pin 3
int relais3 = 4; // relais 3 at pin 4
int relais4 = 5; // relais 4 at pin 5

void setup()
{
  // switch pins to outputs
  pinMode(relais1,OUTPUT);
  pinMode(relais2,OUTPUT);
  pinMode(relais3,OUTPUT);
  pinMode(relais4,OUTPUT);
  // initialize serial port
  Serial.begin(9600); // Baud rate 9600
}

void loop()
{
  // wait until a character received
  if (Serial.available())
  {
    char c = Serial.read();
    if (c == '1') digitalWrite(relais1,HIGH);
    if (c == '2') digitalWrite(relais2,HIGH);
    if (c == '3') digitalWrite(relais3,HIGH);
    if (c == '4') digitalWrite(relais4,HIGH);
    if (c == 'a') digitalWrite(relais1,LOW);
    if (c == 'b') digitalWrite(relais2,LOW);
    if (c == 'c') digitalWrite(relais3,LOW);
    if (c == 'd') digitalWrite(relais4,LOW);
  }
}

The relais must be connected to the above specified pins with a transistor or something else as amplifier (depending on the type of relais). If you load this program to the board you can switch relais 1 on and off by sending the character '1' or 'a' to the serial port (use the integrated serial monitor or hyperterminal or something else).
Use '2' or 'b' for relais 2 and so on.

Good luck
Mike
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mike,

Thank you so much for your reply. I sort of understand the sketch you wrote for me... but here is a further question, you wrote 1,2,3,4 as the serial input coming in, can I replace those #s in between the ' ' with actual strings that I need to convert that look more like this:
F0 05 FF 47 00 38 75 ? This is one full string. so can I say
"if (c == 'F0 05 FF 47 00 38 75') digitalWrite(relais1,HIGH);"?
The other thing I'd like to do is once this string sets the relay to high, upon sending the same string again, will set the relay back to low.  So basically I need some additional language that will say if relay 1 is high and serial string 1 is read, set relay 1 to low or if it's low, then set to high. basically 1 string that will change the state from high to low. I'd like to do this for all 4 strings and associated relaies. Is it possible?
« Last Edit: August 19, 2009, 04:27:50 pm by adam32 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I edited Mike's serial read '1' to one of my strings and the added what I think might be the right language to flip flop relay1 on and off if the same string is present again or will this cause a loop? I'm way out of my league  :-[
Code:
// change the pin numbers if necessary
int relay1 = 2; // relay 1 at pin 2
int relay2 = 3; // relay 2 at pin 3
int relay3 = 4; // relay 3 at pin 4
int relay4 = 5; // relay 4 at pin 5

void setup()
{
  // switch pins to outputs
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(relay3,OUTPUT);
  pinMode(relay4,OUTPUT);
  // initialize serial port
  Serial.begin(9600); // Baud rate 9600
}

void loop()
{
  // wait until a character received
  if (Serial.available())
  {
    char c = Serial.read();
    if (c == '50 04 68 3B 01 06') digitalWrite(relay1,HIGH);
    if (digitalWrite(relay1) == HIGH && c == '50 04 68 3B 01 06'){
        digitalWrite(relay1,LOW);
}
    if (c == '50 04 68 3B 01 06') digitalWrite(relay2,HIGH);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay3,HIGH);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay4,HIGH);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay1,LOW);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay2,LOW);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay3,LOW);
    if (c == '50 04 68 3B 01 06') digitalWrite(relay4,LOW);
  }
}
« Last Edit: August 19, 2009, 06:27:39 pm by adam32 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24307
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You've probably found that this
Code:
char c = Serial.read();
    if (c == '50 04 68 3B 01 06') digitalWrite(relay1,HIGH);
doesn't compile.
A "char" holds a single byte, but a string in C is an array of bytes, terminated with a zero, so even if you'd specified the string correctly with double quotes,
Code:
"50 04 68 3B 01 06"
, the comparison between a single character variable "c" and a string is meaningless.

You're kind-of on the right track, but you need to write some simpler sketches to get the hang of string and character handling.
Have a look around the playground for examples.
« Last Edit: August 20, 2009, 01:34:38 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: