Show Posts
Pages: 1 [2] 3
16  Using Arduino / Programming Questions / Serial.read() to string? on: April 17, 2013, 02:22:34 pm
So I decided to make some more advance "talking" with my arduino and I found a piece of code which I transform into this:

Code:
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
String reads;
int words = LOW;


void setup(){
  Serial.begin(9600);
}


void loop()
{
   while(Serial.available() > 0)
   {
       if(index < 19) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
       words = HIGH;
   }
   
   for(int i=0; i<=index; i++)
       {
         reads += inData[i];
         inData[i] = 0;
       }
   
   delay(10);
   if(words)
   {
     Serial.println(reads);
     words = LOW;
     reads = "";
   }
   
   
   index = 0;
   
}


the thing is that in the serial monitor the words that I wrote and send to the arduino get all scattered on the serial monitor separated by spaces like this:
                   test                                 test
test                              test                     t
est
something like that, how can I make it so that they come like:
test
test
test
...
???

Thanks
17  Using Arduino / General Electronics / Re: Conecting cpu fan? on: April 14, 2013, 01:06:51 pm
Thanks, it work better that way (the fan was able to spin faster)!
But my fan has 4 pins, I connect the vcc, gnd, and control, where should I connect the sense pin for readings?
an analog pin or a digital pin?

Thanks for replying.
18  Using Arduino / General Electronics / Conecting cpu fan? on: April 13, 2013, 06:27:08 pm
Hi,
today I took the cpu fan from an older computer and connect it to my arduino to control it.
It has 4 pins and I discovered the vcc, graund and signal (I think), the other must be for rpm readings, so I connect vcc to 5v, the graund to gnd and the signal to pin 6.

It work perfectly, my question is if can I damage my arduino uno r3 by connecting the signal directly into pin 6??

Thanks in advance.
19  Using Arduino / Programming Questions / Re: Controling 2 LEDs over internet on: April 07, 2013, 09:23:56 am
Thanks!!! your code is much easier then the one I found and modified.
Thank you again.
20  Using Arduino / Programming Questions / Re: Controling 2 LEDs over internet on: April 06, 2013, 08:01:27 pm
So where should I put it (the code to check the state of the checkboxes) to make it work??
And how could I only have one form with 2 checkboxes (I don't know much of html)??

Thanks for the reply
21  Using Arduino / Programming Questions / Controling 2 LEDs over internet on: April 06, 2013, 04:35:11 pm
Hi, I have recently bought an Ethernet shield and started playing around with it.
After some experiences with one LED I decided to controlling 2 here is the code:
Code:
#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 60); // IP address, may need to change depending on network
EthernetServer server(1204);  // create a server at port 80

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default
boolean LED_status1 = 0;

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(2, OUTPUT);       // LED on pin 2
    pinMode(3, OUTPUT);
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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("Connection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino LED Control</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>LED</h1>");
                    client.println("<p>Click to switch LED on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox(client);
                    client.println("<p>Click to switch LED on and off.</p>");
                    client.println("<form method=\"get\">");
                    ProcessCheckbox1(client);
                    client.println("</form>");
                    client.println("</body>");
                    client.println("</html>");
                    Serial.print(HTTP_req);
                    HTTP_req = "";    // finished with request, empty string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
    if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status) {
            LED_status = 0;
        }
        else {
            LED_status = 1;
        }
    }
    
    if (LED_status) {    // switch LED on
        digitalWrite(2, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\" checked>LED2");
    }
    else {              // switch LED off
        digitalWrite(2, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
        onclick=\"submit();\">LED2");
    }
}

void ProcessCheckbox1(EthernetClient cl)
{
    if (HTTP_req.indexOf("LED3=2") > -1) {  // see if checkbox was clicked
        // the checkbox was clicked, toggle the LED
        if (LED_status1) {
            LED_status1 = 0;
        }
        else {
            LED_status1 = 1;
        }
    }
    
    if (LED_status1) {    // switch LED on
        digitalWrite(3, HIGH);
        // checkbox is checked
        cl.println("<input type=\"checkbox\" name=\"LED3\" value=\"2\" \
        onclick=\"submit();\" checked>LED3");
    }
    else {              // switch LED off
        digitalWrite(3, LOW);
        // checkbox is unchecked
        cl.println("<input type=\"checkbox\" name=\"LED3\" value=\"2\" \
        onclick=\"submit();\">LED3");
    }
}

My problem is that controlling one is fine but when I try to turn both on it start messing up every thing becouse, only one lights up the last cliced if I turn the other off by checking the box it turns on but the other turns off.
Any way my question is how can I turn both on and off with out problems??

Thanks
22  Community / Products and Services / Re: ArduinoCommander - android application to manage arduino board on: March 24, 2013, 08:58:27 am
Quote
I am trying to connect using USB OTG cable converter on Samsung Galaxy S3.
My UNO is discovered in USB devices as /dev/bus/usb/002/003
I also have the Standard Firmata sketch installed
I tried every baud rate but it won’t connect.

Any ideas anyone?

I have the same problem, I connect it to my tablet and my arduino turns on but it wont show up on the USB devices tab.
How can I fix that?
23  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 03:40:44 pm
Here is the code:
Code:
import processing.serial.*;
import oscP5.*;
import netP5.*;


Serial arduinoPort;
OscP5 oscP5;

float redAmount = 0.0f;
float greenAmount = 0.0f;
float blueAmount = 0.0f;
byte pos = 0;
byte posc = 0;
int z;


void setup(){
  size(320, 480);
  background(0);
  oscP5 = new OscP5(this, 8000);
  arduinoPort = new Serial(this, "COM3", 9600);
 
 
}


void draw(){
  background(redAmount, greenAmount, blueAmount);
 
  fill(0);
  //red rect
  stroke(255,0,0);
  rect(34,39,67,255);
  fill(50,40,40);
  rect(34,39+255,67,-redAmount);
 
  //green rect
  fill(0);
  stroke(0,255,0);
  rect(124,39,67,255);
  fill(40,50,40);
  rect(124,39+255,67,-greenAmount);
 
  //blue rect
  fill(0);
  stroke(0,0,255);
  rect(216,39,67,255);
  fill(40,40,50);
  rect(216,39+255,67,-blueAmount);
 
 
 
 
}

void oscEvent(OscMessage theOscMessage){
  String addr = theOscMessage.addrPattern();
  float val = theOscMessage.get(0).floatValue();
 
  if(addr.equals("/1/red")){ redAmount = val;}
  if(addr.equals("/1/green")){ greenAmount = val;}
  if(addr.equals("/1/blue")){ blueAmount = val;}
  pos = byte(blueAmount);
  posc = byte(redAmount);
  z = (int)(word(pos, posc));
  arduinoPort.write(z);
 
 
 
}


and here is the error:
the fuction word(byte, byte) does not exist.
24  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 03:31:36 pm
Thanks, rely enjoyed the second link but when I put in processing "z = (int)(word(pos, posc));" it says the function does not exists  
25  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 03:20:04 pm
And what is that like?
26  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 03:12:25 pm
I am sending int like arduinoPort.write(pos); which is an int, how do I combine that with the strtok() command?
27  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 02:41:51 pm
And how do I do that??
sorry for being very annoying.
28  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 02:24:53 pm
In processing it is like this:
Code:
arduinoPort.write("h");
  arduinoPort.write(",");
  arduinoPort.write(pos);
29  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 02:19:25 pm
Still the same I am don't know how to use it in my situation.
30  Using Arduino / Programming Questions / Re: From processing to arduino on: March 15, 2013, 02:02:39 pm
I cant get it work:
Code:
#include <Servo.h>
 
Servo myservo;

 

int pos = 0;


 
void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
 
}
 
void loop()
{
           
     if (Serial.available() > 0) {
      pos = atoi(strtok(NULL,",")); //how do i use this??
      myservo.write(pos);
 
  }
 

}
Pages: 1 [2] 3