Show Posts
Pages: 1 ... 5 6 [7] 8 9 10
91  Using Arduino / Programming Questions / Re: declarations in libraries on: January 25, 2013, 07:59:22 am
I think this will be the last step in helping me figure out how to write libraries.  Here is the code as it stands now:

sketch:
Code:
#include <SPI.h>
#include <Ethernet.h>
#include "eth.h"

Eth eth;

EthernetServer server(80);

void setup(){
  Serial.begin(9600);
  Serial.println("Setup has begun");
eth.init();
}


void loop(){
  //Serial.println("Loop");
 eth.servClient();
}

eth.h:
Code:
#ifndef eth_h
#define eth_h

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



class Eth{


 public:


void init();
void servClient();

private:
byte mac[5];

};
#endif

eth.cpp:
Code:
#include "Arduino.h"
#include "eth.h"

void Eth::init(){
 mac[0] = 0xDE;
 mac[1] = 0xAD;
 mac[2] = 0xBE;
 mac[3] = 0xEF;
 mac[4] = 0xFE;
 mac[5] = 0xED;
IPAddress ip(192,168,1,177);
extern EthernetServer server;
  Ethernet.begin(mac, ip);
  server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
 
}

void Eth::servClient(){
   Serial.print("Server is at:  ");
 Serial.println(Ethernet.localIP());
  //Serial.println("servClient called");
  extern EthernetServer server;
   // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head><title>Lorens Page</title></head>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
         
          //client.print("Transport is currently:  ");
          //client.print(stat);
          client.println("<br />");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }

}

Clients are unable to connect to the server.  I have a feeling this has something to do with the way that I have declared "EthernetServer server(80)" and the way I'm using extern. 

Are the functions "init" and "serveClient" in the Eth class creating their own instance of server?

Again many many thanks!

Loren
92  Using Arduino / Programming Questions / Re: declarations in libraries on: January 25, 2013, 06:30:41 am
For those trying to avoid the pain of what I just went through here is what I found:

Please explain why those are global variables (that is why they are created twice), rather than class fields.

You could define the variables in the sketch, and add extern in front of the declaration statements in the header. That way, the header file would simply affirm that the variables are defined somewhere else, and would allow access to them, wherever they are defined.

I didn't necessarily want them to be global variables.  After seeing that comment I took another look at the library tutorial and noticed that I should be declaring the variables below with the private declarations for the class:

Code:
#ifndef eth_h
#define eth_h

#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
 
class Eth{

public:
void init();

private:
byte mac[5];
};
#endif

93  Using Arduino / Programming Questions / Re: declarations in libraries on: January 24, 2013, 08:46:06 pm
Well,  you need to be careful not to define things twice.

That's why most header files have that  #ifndef  stuff at the beginning,  to prevent them being included twice.

Your "variables"  mac and server  are being defined twice.  This could mean one of two things:
(a)  the same variable is being included twice,  or
(b)  two different variables are being defined, using the same name.

To distinguish these cases,   try re-naming your instances of these variables  to something else.   Like  mac_xxx  and server_xxx.
And then see what happens.



I tried to rename the variables.  The same errors are still occurring. 

I did lose the () to create the class.  No changes in the errors there either.
94  Using Arduino / Programming Questions / Re: declarations in libraries on: January 24, 2013, 07:09:37 pm
So here is the code:

Sketch:

Code:
#include <SPI.h>
#include <Ethernet.h>
#include "eth.h"

Eth eth();
void setup(){

}


void loop(){
 
}

eth.h:
Code:
#ifndef Eth_h
#define Eth_h

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

 byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 IPAddress ip(192,168,1, 177);
 EthernetServer server(80);


class Eth{
 

 public:
void init();

};
#endif

and eth.cpp:

Code:
#include "Arduino.h"
#include "eth.h"

void Eth::init(){

 
 
 
}

With the variables declared I get these errors:

Code:
eth.cpp.o:eth.cpp:14: multiple definition of `ip'
consRemoteTwo.cpp.o:consRemoteTwo.cpp:11: first defined here
/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
eth.cpp.o:eth.cpp:14: multiple definition of `server'
consRemoteTwo.cpp.o:consRemoteTwo.cpp:11: first defined here
eth.cpp.o:(.data.mac+0x0): multiple definition of `mac'
consRemoteTwo.cpp.o:(.data.mac+0x0): first defined here

Any suggestions on what I'm still doing wrong?

Thanks again,

Loren
95  Using Arduino / Programming Questions / Re: declarations in libraries on: January 24, 2013, 06:52:17 am

What does your sketch look like? Are you including Ethernet.h in the sketch?

No....  And that was the problem!  Thanks
96  Using Arduino / Programming Questions / Parsing csv file from sd card to an array on: January 23, 2013, 10:48:59 pm
I have searched hi and low and cannot find anyway to parse a csv file into array. 

I can open the file and write the whole file to the serial port, but I have not been able to figure out how to use delimiters to break it up into lines and columns.

If anyone could help me find a tutorial or get me pointed in the right direction I would certainly appreciate it.

Thanks,

Loren
97  Using Arduino / Programming Questions / Re: declarations in libraries on: January 23, 2013, 09:12:21 pm
What is mentioned above makes sense  here is what I attempted:

Code:
#ifndef Eth_h
#define Eth_h

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

byte mac [5];
IPAddress ip;


class Eth{
   
 public:
void init();

};
#endif

Which returns the error: 
Code:
error: 'IPAddress' does not name a type

I thought that IPAddress was a member of the Ethernet source. 

What am I not doing or missing?
98  Using Arduino / Programming Questions / declarations in libraries on: January 23, 2013, 10:25:32 am
I'm working on a project that I think will be fairly large.  I'm hoping to write some libraries to help maintain the ability to read through it easily.  I did go through the library tutorial but I am confused about where to put a some of the declarations that the library will need to access.

for example I'm working with a ethernet shield and need to declare this all of this:

Code:
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

I got an error when I tried to declare it at the top of the header file and when I tried to put it at the top of the source file.  I did try and declare all of it in the main file but that also created problems.  I assume that in this scenario the header and source file don't have access to the variables. 

Any help would be greatly appreciated. 

Thanks,

Loren
99  Using Arduino / Programming Questions / Re: MIDI Shield buttons on: January 21, 2013, 07:45:48 pm
It turned out to be the ground was cross patch.  Ugg

Thanks for all of the help!

Loren
100  Using Arduino / Programming Questions / Re: MIDI Shield buttons on: January 21, 2013, 06:41:10 pm
Quote
I would think that it would change to zero if I pushed the button.

So press the button and see what it does!

I did nothing happend
101  Using Arduino / Programming Questions / Re: MIDI Shield buttons on: January 21, 2013, 10:44:50 am
Fantastic! thanks for the explanation.  I tried this:

Code:
  if (button(BUTTON1)) {

      Serial.println("on");

    }

That didn't print any results.  So I decided to try and read the digital pin and print that out.  When I did this: 
Code:
  buttonState = digitalRead(BUTTON1);
Serial.println(buttonState);

the serial monitor reads a continuous string of 1s
I would think that it would change to zero if I pushed the button.

Thoughts?
102  Using Arduino / Programming Questions / MIDI Shield buttons on: January 21, 2013, 10:05:07 am
I have this: https://www.sparkfun.com/products/9595? midi shield connected to my arduino.  I decided to start simple with just a button or two.  All of the simple button examples show a button sending a digital input high, but the shield I'm using connects the digital pin to ground when the switch is closed which confused me.  So I looked at the example code and tried to pull out the bits that are pertinent to just utilizing two of the three buttons.

Here are the pertinent portions of the setup code:

Code:
#define BUTTON1  2
#define BUTTON2  3


#define STAT1  7
#define STAT2  6



void setup() {

  Serial.begin(9600);
  pinMode(STAT1,OUTPUT);   
  pinMode(STAT2,OUTPUT);

  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
 

  digitalWrite(BUTTON1,HIGH);
  digitalWrite(BUTTON2,HIGH);
 

  for(int i = 0;i < 10;i++) // flash MIDI Sheild LED's on startup
  {
    digitalWrite(STAT1,HIGH); 
    digitalWrite(STAT2,LOW);
    delay(30);
    digitalWrite(STAT1,LOW); 
    digitalWrite(STAT2,HIGH);
    delay(30);
  }
  digitalWrite(STAT1,HIGH);   
  digitalWrite(STAT2,HIGH);
 
}
 

In the main loop the example code uses the buttons like this:

Code:
if(button(BUTTON1) || button(BUTTON2) || button(BUTTON3))
  { 
    Midi_Send(0x90,note,0x45);
    while(button(BUTTON1) || button(BUTTON2) || button(BUTTON3));
  }

Can someone explain to me what is going on with the buttons in the main loop?  This is the part that I don't get.  I think that If I understood this I could get my sketch to react to a button push.

Thanks for stopping by.

Loren
103  Using Arduino / Project Guidance / Re: RC Battery voltages with ATTiny and nano on: January 02, 2013, 08:11:09 pm
Ahh Now I see what you are saying.  What you are saying complicates things a bit...  Actually a lot.  Is there a simpler way to accomplish this?

Thanks so much for your help!
104  Using Arduino / Project Guidance / Re: RC Battery voltages with ATTiny and nano on: January 02, 2013, 05:42:30 pm
Sorry I didn't explain myself very well.  The batteries that I use are 3cell lypo batteries.  The sum of the cells is 11.1v

This is the diagram that helped me understand how the balance connector worked:

https://f47fb5b4-a-62cb3a1a-s-sites.googlegroups.com/site/tjinguytech/charging-how-tos/balance-connectors/rcheli-diagram-3s_lipo.png?attachauth=ANoY7crPH4yZMADK663EOJEZREBTu9DqXmRVV5T9ojjW0tujB1G-tZS3mb7ltL_e3Eg2hYc8YArvHATh_NP6vaeoqByCTCqK3aSQi0-P4ie0VXsQhR4OAPfoRHsX7elvjDV8YuO33Lo8e6P2Yf2OAO0qDF_ZZIo0DKnKnb3qd_qH3oTNLjJRNN9h11L5gavUOeIo556BJlPnEaXQvCP446wjN28dpwL8deQKrj-_VcdHUqm7T71coJ87bwbCcD8vCfk8zqNt3az95UU8S4021x4ytAvbvSGsmg%3D%3D&attredirects=1

The goal is to measure both the individual cells and the overall battery voltage.

Does that help?

Loren
105  Using Arduino / Project Guidance / RC Battery voltages with ATTiny and nano on: January 02, 2013, 05:09:53 pm
I was doing some research on reading voltages and found someone who suggested using ATTiny 45s for reading individual cell voltages.  Then pass the voltage reading onto a "master" meaning the nano on the right.  Would this work?



Thanks,

Loren
Pages: 1 ... 5 6 [7] 8 9 10