Show Posts
Pages: 1 ... 102 103 [104] 105 106
1546  Using Arduino / Programming Questions / Re: Could someone check this code on: December 28, 2011, 02:29:46 pm
This doesn't look right:
Code:
//Controll Tilting
if(bTiltDirPos)
{
if(bTiltDirPos)

Based on what you have in the previous if statement for bPan, I suspect you meant:
Code:
//Controll Tilting
if(bTilt)
{
if(bTiltDirPos)

Pete
1547  Using Arduino / Programming Questions / Re: Arduino recieving hexadecimal data on: December 28, 2011, 11:55:55 am
Your main problem is that you aren't reading and discarding the linefeed at the end of each line. You also pass a string address to strtol but don't use it - better to pass NULL.

Use this at the beginning of loop():
Code:
    if (Serial.available() >= 6 ) { // wait for 6 characters
        for (int i=0; i < 5; i++)
            command[i] = Serial.read();
        command[5] = '\0';
        // Throw away the \n
        Serial.read();


        strings[0] = strtok(command, " ");
        strings[1] = strtok(NULL, " ");

        delay(10);
        led = atoi(strings[0]);
        power = strtol(strings[1],NULL,16);

It waits for 6 chars, stores the first 5 and then throws away the sixth one (the linefeeed).
This code is still going to blow up if you don't enter exactly 5 chars plus linefeed.
If you are always going to enter something of the form "03 7F", you don't need strtok at all. The "led" value is always at &command[0] and the power value is at &command[3].

Pete
 
1548  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 10:57:42 pm
Also, which version of the Mini are you using? The pinout changed in a way that affects the reset. See http://www.arduino.cc/en/Main/ArduinoBoardMini

Pete
1549  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 10:55:10 pm
the pin isn't being initialized properly. Use this for your setup routine:
Code:
void setup() {
    pinMode(button1,INPUT);
    digitalWrite(button1,HIGH);
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

Pete
1550  Using Arduino / Programming Questions / Re: Trouble comparing a float variable in an If statement on: December 27, 2011, 10:02:58 pm
Have you initialized outPin properly in your setup() code?

Pete
1551  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 10:00:35 pm
yup, sorry. I'm still not up to speed with V1.0 !
Code:
#define MOTOR_PIN 10

void setup() {
    pinMode(MOTOR_PIN,INPUT);
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
    if (digitalRead(MOTOR_PIN)) {
        Serial.write((const uint8_t *)"a",1);
    } else {
        Serial.write((const uint8_t *)"b",1);
    }
}

Pete
1552  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 09:51:25 pm
Use this for the controller code:
Code:
#define MOTOR_PIN 10

void setup() {
    pinMode(MOTOR_PIN,INPUT);
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
    if (digitalRead(MOTOR_PIN)) {
        Serial.write("a",1);
    } else {
        Serial.write("b",1);
    }
}

But note that this will send a continuous stream of 'a' or 'b' depending upon whether MOTOR_PIN is high or low.

Pete
1553  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 09:08:01 pm
and?
1554  Using Arduino / Programming Questions / Re: serial driven robot on: December 27, 2011, 09:04:44 pm
The loop() in the robot will never receive anything because you keep throwing it away with Serial.flush(). Remove that statement and try again.

Pete
1555  Using Arduino / Programming Questions / Re: HMC5843L Compass Chip simple programing question on: December 27, 2011, 07:39:32 pm
OK, how about enlightening me as to what you did to fix it. It might help others in the future too.

Pete
1556  Using Arduino / Programming Questions / Re: Problem with Arduino receiving two-byte serial data stream on: December 27, 2011, 06:05:03 pm
You should not flush the serial buffer because you don't know what has been received. You will be continuously throwing away a command character or data character.

Replace your loop() function with this:
Code:
void loop(void)
{
char cmd,value;

if(Serial.available()) {
cmd = Serial.read();
// If it's not a command character, ignore it and wait for the next one
if(cmd != 'T' && cmd != 'L' && cmd != 'B')return;
// got the command, now wait for the data char
while(Serial.available() == 0);
value = Serial.read();
// got the command and value, now do the command
switch(cmd) {
case 'T':
throttle = map(value, 0, 255, 70, 255);
digitalPotWrite(throttle, throttleSelectPin);
break;
case 'B':
brake = map(value, 0, 255, 70, 255);
digitalPotWrite(brake, brakeSelectPin);
break;
case 'L':
  lights = value;
  if(lights == 0)
  digitalWrite(lightsSelectPin, LOW);
  else
  digitalWrite(lightsSelectPin, HIGH);
  break;
  }
  }
 }

Pete
1557  Using Arduino / Programming Questions / Re: HMC5843L Compass Chip simple programing question on: December 27, 2011, 02:04:39 pm
Do you have a spec sheet for the HMC5843L? It would also help if you could point to some code that works - i.e. code that reads the compass and returns a heading in degrees. I can only find the HMC5843 online and it doesn't seem to work quite the same (or your setup code is wrong).
Also, the values returned by the compass for each axis may not be directions, but a measurement of the magnetic field in which case you have to calibrate your program to what the chip returns.

Pete
1558  Using Arduino / Programming Questions / Re: Code review on: December 25, 2011, 06:59:57 pm
FWIW, you could use this:
const unsigned char DEG = 223;

and this (%c instead of %s):
    sprintf(printbuf,"Water: %d%c", int(temp[BURNER_POST]), DEG);

which saves a whole byte  smiley-grin

Pete
1559  Using Arduino / Programming Questions / Re: HMC5843L Compass Chip simple programing question on: December 25, 2011, 06:50:52 pm
The specs that I found for that chip say that it returns a value from -2048 to +2047 inclusive. If 0 is zero degrees then I would replace this:
Code:
      if(x>0)
       {
          res = map(x, 0, 246, 0, 90);
       }
       else if(x<0)
       {
         if(x!=0)
         {
           res = abs(x);
           res = map(res,0,255,90,0);
           res = 270+res;
         }
         else
          res = 0;
       }

with this:
Code:
 if(x >= 0)res = map(x,0,2047,0,180);
  else      res = map(x,-1,-2048,360,180);

Pete
P.S. the map function returns an integer so you might as well declare res to be an integer as well.
1560  Using Arduino / Programming Questions / Re: Drifting in a for loop with digitalwrite on: December 25, 2011, 03:27:32 pm
I would try this with a timer interrupt.

Pete
Pages: 1 ... 102 103 [104] 105 106