Show Posts
Pages: 1 ... 48 49 [50] 51 52 ... 197
736  Using Arduino / Programming Questions / Re: Spliting serial strings, then convert to int on: December 09, 2013, 01:45:03 pm
What string were you sending that gave both red and blue to show?

When I tested my code, I noticed it was adding the colon to the second number,  this should take care of that.
Code:
int count= 0, t1= 0,t2= 0;
void setup(){
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0){
    char c = Serial.read();

    if(c == '.') // if c is a period, show data and clear everything
    {
      showme(t1,t2);  // Replace with  fade(); & led.update(); 
      count = 0;
      t1 = 0;
      t2 = 0; // if the received char is a period, reset count and t1,t2 back to zero
    }

    if(c == ':') // if c is a colon, change cases
    {
      count++;
    }

    else  // same as "if(c >= '0' || c <= '9')"
    {
      if(c != '.') // if c is a period, do NOT add it to t1 or t2, discard it!
      {
        switch(count){
        case 0:
          t1 = t1 * 10 + (c - '0');
          break;
        case 1:
          t2 = t2 * 10 + (c - '0');
          break;
        }
      }
    }
  }
}

void showme( int D1, int D2){
  Serial.print("D1: ");
  Serial.print(D1 );
  Serial.print(" : ");
  Serial.print("D2: ");
  Serial.println(D2);
}
737  Using Arduino / Programming Questions / Re: Spliting serial strings, then convert to int on: December 08, 2013, 11:33:07 pm
Ok I see what your doing, but in this case, I'll need to add a new line to the code I gave you. In order for this to work, you need to change your string to include a period at the end of it "XX:YY."

Actually, I'll change the code you provided.
Code:
if(Serial.available() > 0){
    char c = Serial.read();
   
    if(c <= '0' || c >= '9')
    {
        count++;
    }

    if( c == '.')
    {
        count = 0; t1 = 0; t2 = 0; // if the received char is a period, reset count and t1,t2 back to zero
     }
   
    switch(count){
       case 0:
         t1 = t1 * 10 + (c - '0');
       break;
       case 1:
         t2 = t2 * 10 + (c - '0');
       break;
    }
  }

This is no longer needed. Change incomingByte the way you were going to.
Quote
if (Serial.available() > 0) {
    incomingByte = Serial.parseInt();
  }
738  Using Arduino / Programming Questions / Re: Spliting serial strings, then convert to int on: December 08, 2013, 09:17:48 pm
That's fine, just make sure count = 1 (globally).
The conversion method works like this.
FirstNum starts at 0, then you get a value from the serial monitor, "c". Now because c is a char, you need it to be converted to an int, so (c - '0') does just that. Now "FirstNum = FirstNum * 10 + (c - '0')" takes single digits and meshes them together, into however large of a int or long.

Example:
c = '5'; FirstNum = 0.
0 = 0 * 10 + ('5' - '0');
0 = 0 * 10 + (5); by order of operations, multiplication is done first.
0 = 0 + 5;

Now FirstNum = 5. Get a new value.
c = '9'; FirstNum = 5.
5 = 5 * 10 + ('9' - '0');
5 = 5 * 10 + (9); by order of operations, multiplication is done first.
5 = 50 + 9;
Now FirstNum = 59.


If your LEDs are going crazy, make sure your using the PWM (~)pins. If that still doesn't work, post your full code.
739  Using Arduino / Programming Questions / Re: Spliting serial strings, then convert to int on: December 08, 2013, 08:22:04 pm
Do you know what a case statement is? Normally I would say to use the strtok() function, which you still could, but that's a little much for what your doing.

What you could do instead is use an IF statement to check if the value your receiving is a number or a colon. Once you can tell the difference, you can use case statements to collect the two numbers and convert them into ints. If you do get a semicolon, you can then switch the case statement and collect the values for the second number. Once you have both numbers and use them, you then need to clear them to start over for new data.

This is an example you can use to collect the data and convert it to an int.
Not the full code, it's just an idea
Code:
char val = Serial.read();
if( val <= '0' || val >= '9' ) // val is NOT a digit
{
  CaseInc++; // will switch case if not a digit
}
// make case statement here
FirstNum = FirstNum * 10 + ( val - '0' ); // first number
740  Using Arduino / Programming Questions / Re: Help with sending serial text string on: December 08, 2013, 06:59:00 pm
Make sure your button wiring is correct. If you press a button, does the input go HIGH or LOW. Do some debugging with the serial monitor to find out.
741  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 07, 2013, 10:16:52 pm
I'm interested.
742  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 07, 2013, 06:24:22 pm
Hey Pyro, after changing "byte table[16]" to "static const byte table[16]" I was able to beat your time by almost 177,084 us, and beat my old time by 1,150,552 us! I can't test the Mega code right now, but I will when I get a chance.

Change my section to this and re-run it.
Code:
byte MirrorByte_HAZARDSMIND(byte ary)
  {
    byte h, l;
    static const byte table[16] = {
      0x0, 0x8, 0x4, 0xC,
      0x2, 0xA, 0x6, 0xE,
      0x1, 0x9, 0x5, 0xD,
      0x3, 0xB, 0x7, 0xF
    };
    h = table[ (ary & 0xf0) >> 4 ]; // Notice the closing bracket is different from the one below. This is because of the look up table is written as 0xN and not 0x0N
    l = table[ ary & 0x0f ] << 4;
    //Serial.println( l | h ,HEX);
    return (l | h );
  }

743  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 06, 2013, 10:44:07 pm
Quote
The most straight forward solution is a for loop calling bitRead() and bitWrite().
I agree, but for some reason my IDE Version 1.0.5 does not know what the bitWrite() function is, even after I reinstalled it.

So I made that mirror function like it is, (I did not Google it) and it seems to work rather well.

Added:
Strange thing, I just downloaded version 1.0.5 on my old Windows XP laptop, and the bitWrite function works, but the exact same sketch on my new windows 8 laptop, with the same IDE version does not compile. It says "bitWrite was not declared in this scope", So I copied the IDE from my XP laptop, to my Windows 8 laptop, and the bitWrite function now works.

I just reinstalled the IDE from the download page, and again it works. Maybe something just wasn't downloading correctly, the first 3 times.  smiley-sad But never the less it works now.
744  Using Arduino / Programming Questions / Re: Laptop control using Arduino on: December 06, 2013, 10:37:42 pm
Quote
Can it be done using Arduino UNO? Because I am not directly using any keyboard command. I am just receiving a serial value and then telling my computer to act on the basis of received value.
That, I can't say because I don't know if your computer/laptop will be able to receive serial commands when it is sleeping.
745  Using Arduino / Programming Questions / Re: Laptop control using Arduino on: December 06, 2013, 10:13:28 pm
You will need an Arduino Leonardo in order to do that. So far none of the other Arduino's can interface with keyboard commands, so if anything that's where you should start.
 
Now if your computer has a sleep and wake button, I would see if there is a way you can make hot-keys for them, or see if you can make the key combination yourself with the libraries available keys.

If you able to figure out the hot-key / key combination , then the next part should not be difficult to implement.
746  Using Arduino / Project Guidance / Re: Arduino Robot Arm controlled by Joystick on: December 06, 2013, 08:38:08 pm
The 5 pins on the joystick should be labeled. V+, GND, Y, X, Button/AUX. Something along those lines. You can put them into the breadboard, or if you have wires with male to female connectors on the ends, you can use those instead.
747  Using Arduino / Project Guidance / Re: Arduino UNO not receiving serial data(TX) from Bluetooth module on: December 06, 2013, 04:24:40 pm
See if you can get Bluetooth Chat, that program should know works.
748  Using Arduino / Programming Questions / Re: Traffic Light Controller Programming Code on: December 06, 2013, 03:58:49 pm
Get rid of all the delays functions, and look at the example sketch Blink without Delay. It will help you tremendously.
749  Using Arduino / Programming Questions / Re: Ultrasonic sensor mounted on a servo on: December 06, 2013, 03:56:58 pm
Try this, instead of using the using || use &&, this way the code knows to compare the ranges from Left AND Right, as opposed to Left OR Right.

See if that works, if not it could be another issue somewhere else.
750  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 06, 2013, 03:12:22 pm
Thanks
Pages: 1 ... 48 49 [50] 51 52 ... 197