Author Topic: control output pins over ethernet (arduino)  (Read 9845 times)

0 Members and 1 Guest are viewing this topic.

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
control output pins over ethernet (arduino)
« on: October 01, 2011, 06:14:20 AM »
Hello, for my wifi robot project, I'm using an arduino ethernet. however, I'm pretty new to the arduino language.
I've been playing around with it, and I'm able to make the dc motors go forward and backward using an H bridge IC.
Now, I want to control it over a local network... How would I go about that? What I want to do is kinda like this:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };
byte gateway[] = { 192,168,1, 1 };
byte subnet[] = { 255, 255, 0, 0 };
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

// telnet defaults to port 23
Server server(23);
boolean connected = false;

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  // open the serial port
  Serial.begin(9600);
}

void loop() {
  // wait for a new client:
  Client client = server.available();
 
  if (client) {
    connected = true;
    }
   
    // read the bytes incoming from the client:
    char thisChar = client.read();
   
   //AND HERE THERE WOULD A SELECT CASE DOING DIFFERENT THINGS DEPENDING ON WHAT BYTE THE ARDUINO RECIEVED.
   //for example either turning pin 2 or 3 high.
   //digitalWrite(2, HIGH);
   //digitalWrite(3, HIGH);
   //which would make my motor go either backwards or forwards.
  }
}

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: control output pins over ethernet (arduino)
« Reply #1 on: October 01, 2011, 12:17:07 PM »
Code: [Select]
[quote author=GalacticNerd link=topic=14556.msg106375#msg106375 date=1317471260]
    // read the bytes incoming from the client:
    char thisChar = client.read();
   
   //AND HERE THERE WOULD A SELECT CASE DOING DIFFERENT THINGS DEPENDING ON WHAT BYTE THE ARDUINO RECIEVED.
   //for example either turning pin 2 or 3 high.
   //digitalWrite(2, HIGH);
   //digitalWrite(3, HIGH);
   //which would make my motor go either backwards or forwards.
  }
}
[/quote]

That looks like a pretty good start to me. I don't understand the question I guess. Have you tried doing that and you're not getting the results you want?

Joe

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: control output pins over ethernet (arduino)
« Reply #2 on: October 01, 2011, 05:08:08 PM »
Code: [Select]
[quote author=GalacticNerd link=topic=14556.msg106375#msg106375 date=1317471260]
    // read the bytes incoming from the client:
    char thisChar = client.read();
    
   //AND HERE THERE WOULD A SELECT CASE DOING DIFFERENT THINGS DEPENDING ON WHAT BYTE THE ARDUINO RECIEVED.
   //for example either turning pin 2 or 3 high.
   //digitalWrite(2, HIGH);
   //digitalWrite(3, HIGH);
   //which would make my motor go either backwards or forwards.
  }
}

That looks like a pretty good start to me. I don't understand the question I guess. Have you tried doing that and you're not getting the results you want?

Joe


Well... in the comments I note where a part of the code should go, but I'm unsure on how it would look like in the arduino code.
The commented part at the end describes what should go there, instead. The code wouldn't do anything, because there's no controlling going on.

I want to be able to send bytes to the arduino from another computer on the local network, and it should make different output pins high depending on what byte.

Edit: since no one is replying, I want a piece of code, that would be essentially like a select case in visual basic. To clarify my point, it would be this in VB:

Select Case ThisChar
         case something
                 digitalWrite(2, HIGH);
                 digitalWrite(3, LOW);
         case somethingelse
                 digitalWrite(2, LOW);
                 digitalWrite(3, HIGH);
end select
« Last Edit: October 03, 2011, 08:48:14 AM by GalacticNerd »

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: control output pins over ethernet (arduino)
« Reply #3 on: October 03, 2011, 09:04:11 AM »
I figured out there's a switch ... case instead of a select case. Silly me, all the vb.net has got me brainwashed.

So would something like this work? I'm not used to using brackets anymore, so are they correct?
Also, is there a chart or tutorial on which I can see which ascii character would give me which byte.. or is it just their ascii number?

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };
byte gateway[] = { 192,168,1, 1 };
byte subnet[] = { 255, 255, 0, 0 };
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

// telnet defaults to port 23
Server server(23);
boolean connected = false;

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  // open the serial port
  Serial.begin(9600);
}

void loop() {
  // wait for a new client:
  Client client = server.available();
 
  if (client) {
    connected = true;
    }
  
    // read the bytes incoming from the client:
    char thisChar = client.read();
    
    switch (incomingChar) {
     case 1:
       //forward
       digitalWrite(3, LOW);
       digitalWrite(2, HIGH);
       break;
     case 2:
       //backward
       digitalWrite(3, HIGH);
       digitalWrite(2, LOW);
       break;
    default:
       //make sure it's not moving
       digitalWrite(3, LOW);
       digitalWrite(2, LOW);
       break;
  }
  }
}

Okay, so I found this chart: http://arduino.cc/en/Reference/ASCIIchart
What's the easiest way to connect to this, and send those values? Would I be able to telnet into it and type the letters?
« Last Edit: October 03, 2011, 09:08:30 AM by GalacticNerd »

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: control output pins over ethernet (arduino)
« Reply #4 on: October 03, 2011, 10:27:45 AM »
Are you asking what functions to call for serial communications? If so The arduino site has a reference page that explains all their functions. They also have a section on libraries other than the built in ones. This should give you what you need, look for the serial section:

http://arduino.cc/en/Reference/HomePage

Joe

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: control output pins over ethernet (arduino)
« Reply #5 on: October 03, 2011, 01:27:31 PM »
Are you asking what functions to call for serial communications? If so The arduino site has a reference page that explains all their functions. They also have a section on libraries other than the built in ones. This should give you what you need, look for the serial section:

http://arduino.cc/en/Reference/HomePage

Joe


I'm terribly sorry, but I don't think you're properly reading my posts. I think have a serious miscommunication either way. I'm asking about how to send the bytes over the local network to my arduino. I have proposed a piece of code (which I figured out after reading through the arduino references as I mentioned before you linked it to me.)
and I'm asking about the computer's side. I think serial communication has little to do with this?

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: control output pins over ethernet (arduino)
« Reply #6 on: October 03, 2011, 01:57:19 PM »
No, my mistake, sorry. I'll start taking my meds again.

Joe

Offline omkar

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 1
Re: control output pins over ethernet (arduino)
« Reply #7 on: June 16, 2012, 03:12:31 PM »
Hey... Galacticnerd have you been able to solve your problem? I have a similAr one but i have  a wifi. Shield. Here is the link to my post
http://www.societyofrobots.com/robotforum/index.php?topic=15732.msg112526#msg112526

 


Get Your Ad Here

data_list