Show Posts
Pages: 1 ... 65 66 [67] 68 69 ... 196
991  Using Arduino / Project Guidance / Re: Adding Stop Button To Break the Process on: September 22, 2013, 11:19:41 pm
You need to understand how the Blink Without Delay sketch works. It gives you the delay you need without blocking your other functions from doing their jobs, like checking the state of a button.
992  Using Arduino / Programming Questions / [solved] Brain fart with using Serial in custom library on: September 22, 2013, 09:40:54 pm
I just bought a JP 4ch Serial servo controller chip off ebay, and it came with a simple example code, but I want to make it into a library rather than keep declaring the functions. Im know its something so simple, but I just can't think of how to use hardwareSerial in my library.

Here is my c++ file, keep in mind I'm still going to include more later, like more channels and even use softwareSerial. But for now I just want to test it out.

Code:
//CustServo.cpp
#include "Arduino.h"
#include "CustServo.h"
#include <HardwareSerial.h>


void CustServo::Number_of_Channels(uint8_t channel) // 4, 10, 23, 34
{
  if (channel == 4){  // change to case statements
  int ServoNum[5] = {48, 49, 50, 51, 52};
  }
  // more channels to be added later
}

void CustServo::SetBaud(uint8_t baud) // 4800 or 19200
{
  Serial.begin(baud);
}

void CustServo::Move(uint8_t chan, uint8_t position)
{
  int ServoNum[5];
  Serial.write(ServoNum[chan]);            // What channel to use  
  Serial.write(map(position,0,180,7,247)); // send position command
}

Any help would be great.

UPDATE: I figured it out, ha. I'm an idiot sometimes -_-

Can a moderator please delete this post.
993  Using Arduino / Project Guidance / Re: Capture people sitting on a bench on: September 22, 2013, 03:26:19 pm
Any particular reason why you want to do this?

Pressure pads, millis() function, SD card shield or some kind of data logger.
994  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 22, 2013, 02:04:50 pm
@PaulS
Added hang time. Edited code.
995  Using Arduino / Project Guidance / Re: wifi vs bluetooth. on: September 22, 2013, 03:20:14 am
As PaulRB pointed out, what are you going to use to control your project? Both wifi and bluetooth can be accessed from either a phone/tablet or computer. Like I said, bluetooth is easier to learn and there are bluetooth shields or you can simply get a bluetooth module online that work with arduino. eBay has tons, just search "arduino bluetooth module" and see what comes up.
996  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 22, 2013, 02:54:28 am
I forgot it needed a null terminator, but it did work. I'll fix it.
997  Using Arduino / Project Guidance / Re: wifi vs bluetooth. on: September 22, 2013, 02:48:52 am
What's your range?
 
Bluetooth is easier to learn, but the range is limited to about 30ft.
Wifi is harder to learn but your sending data over a network, so you can be anywhere and still be able to control it.
998  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 22, 2013, 02:38:31 am
Try this one, my first one was not tested, sorry about that.
Code:
const unsigned int num_of_data = 3;
char data[num_of_data];
int i=0, j=0;
int new_data;
unsigned long current_time = 0, time = 0, hang_time = 5000;
void setup() {
  Serial.begin(9600);
}

void loop(){
  if(Serial.available() > 0)
  {
    cleardata();
    if(Serial.read() == '#') // #34, #56, #19.....
    {
      current_time = millis(); // store current time
      digitalWrite(13,HIGH); // show that '#' was found
 
      while(time < hang_time){ // collect data within the hangtime limit
        time = millis() - current_time;
        //Serial.println(time);
        if(Serial.available() > 0)
        {
          data[ i ] = Serial.read();
          i++;
          if( i == (num_of_data - 1)) 
          {
            data[ i ] = '\n';
            i = 0;
            new_data = atoi(data);
            Serial.println(new_data);
            digitalWrite(13,LOW);// LED off means data was found within time limit
            time = 0; // reset time back to zero
            break;
          }
        }
      }
      i = 0;
      time = 0;
      digitalWrite(13,LOW); // LED off indicates that was not found, and has now refreshed.
    }
  }
}

void cleardata()
{
  j = 0;
  while ( j != (num_of_data - 1))
  {
    data[j] = 0;
    j++;
  }
}

999  Using Arduino / General Electronics / Re: how would i do this? on: September 21, 2013, 11:01:30 pm
Why not just have a real time clock module (RTC), keep track of the time and use the Arduino just to control the LEDs?

Using the millis() function is fine if you want to use it, but then its more processing for the Arduino to do. (not much more, but still more than necessary)

Then again, the millis() function will run regardless so you might as well use it, and spare yourself the hassle of waiting on another part.
1000  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 10:29:27 pm
Well that works too, but why make another function when you can just return it from the first function. If you want another function to just return the value, then it's ok to do that. It will mean more typing and functions to keep track of, but its fine I guess.
1001  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 10:14:17 pm
I updated my last post.
Quote
Essentially sending "45" will receive "4" and "5" then are later combined into a single int of "45". Since I will only be using 10-89 for my positions, I only need double digit numbers.

If this function is supposed to get both values, then why call it again for y? This function will keep looking for values, 3 to be exact, when all you need is just the one per call.
Your better off recoding your sketch with the example I gave you in reply #4.

1002  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 10:05:24 pm
Ok your setNewValue function.

Quote
void setNewValue() // Set to void, so it should not be expected to return anything. I would change this to "int setNewValue()"

  S_input[] = x; Since this would be the first value in S_input, shouldn't it be S_input[0] = x ? Also x should equal something when it was declaired globally
   delay(5);
   int i=0;     
   while(i<3) // how many more values are you looking for?
   {
     S_input[ i ] = Serial1.read();
     i++;
   }
   Serial1.flush(); // not really needed
   result = atoi(S_input); // result should be returned
}
1003  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 09:27:03 pm
Well if it doesn't receive anything then you can make "new_data" equal zero with an ELSE statement.

Are you using servos, steppers or dc motors? Servos will go the given value and stay in that position if there is nothing telling it to change. A stepper may be a little more difficult because if it will step so many times based on the number your telling it to. So if you keep sending the same number, it will keep stepping. What you could do then is get the value, store it and only step if the new value is different from the previous value.
1004  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 09:05:56 pm
You can still do that, but I would send a leading character, so your code does not convert the wrong data. Lets say you send 45,23,85, and something happens on the receiving end and now you only get 5,23,85, but with your code, it will see it as 52,38,5 null or 0. However if you send a leading char like * or #, then your data will know to only store the values that follow,  if it see * or # first. It is known as a handshake, so both sending and receiving devices are getting the correct data.

Your code could be as simple as this,

You send #45
Code:
if (Serial1.read() == '#' ) // looks at the first byte and sees if it is #. If the statement is true, then get the next two bytes
{
   data[0] = Serial1.read(); // 4
   data[1] = Serial1.read(); // 5
 
  new_data = atoi(data); // 45  <-  atoi ( '4' , '5')
}
1005  Using Arduino / Programming Questions / Re: Getting Serial Value to remain constant on: September 20, 2013, 08:38:11 pm
How is your data being sent? The process of sending the data, receiving it and converting it to an int, can be done easier than this.

Im assuming your sending the actual degree of rotation, so it looks like a regular int. But the receiving end, looks at it one int/char at a time.
So you send 123, you receive 1,2,3(int) or 49,50,51(char). You could also be sending HEX values AA, CD...FF, I don't know.

What I suggest is to send a packet. Send your data within brackets, and have the receiving end remove and convert what's in those brackets.
Example: <125>

Have your receiving code, look for the '<', then look at the data that follows it, and store it in a char array. Keep storing the data until the code see '>'. This will let the code know it has all the data, and that it should now convert the char array to an int, using the atoi() function.

It may sound difficult but it is a good way to handle large amounts of data without anything being mixed up.
Pages: 1 ... 65 66 [67] 68 69 ... 196