Show Posts
Pages: 1 ... 127 128 [129] 130 131 ... 364
1921  Using Arduino / Programming Questions / Re: getting keypress output as linearity. on: August 07, 2013, 06:34:00 pm
You name the int array 'key'. It is not good to name a char 'key' as well.
What is i? Where did you declare it?
It's a good thing that p doesn't increment because there's nothing to stop it from running past the end of key[].

But don't fix those until you learn more how. You are not familiar with the code you try to write.

In your IDE click File->Examples->05 Control-> and go through all of those examples starting with Arrays. Then go do 02 Digital->BlinkWithoutDelay which tells how to deal with time on Arduino.

Code:
int key[10];
int p;

void loop()
{  
  
  key[p] = key;
  i++;
  
  char key = keypad.getKey();

  if (key != NO_KEY)
  {
     Serial.print(key);
  
  }
}

but   error shows here key = key;
1922  Using Arduino / Programming Questions / Re: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's on: August 07, 2013, 06:08:19 pm
Thank You for your help GoForSmoke and Runaway Pancake,  The Potentiometers and the Leds work great.

Thanks Much.....


BenBenBen

Okay but it's up to you to figure out how to make the code non-blocking. That will allow you to put in other features. A good start would be learning BlinkWithoutDelay.

1923  Using Arduino / Programming Questions / Re: Beginner Serial Communication on: August 07, 2013, 06:00:52 pm
If you are initially just interested in turning on and off the laser, take the

Code:
  if(Serial.available() > 0);{
   inByte = Serial.read();          //Read what's on the RX line
   delay(500);
   Serial.print("Incoming Byte: "); 
   Serial.println(inByte);   
   delay(500);
  }
}

out, and test it. It could be causing confusion.

See reply #24 for the updated code. It's better.
1924  Using Arduino / Programming Questions / Re: getting keypress output as linearity. on: August 07, 2013, 03:25:46 pm
I put the code inside code tags. The # button above the  smiley-money makes code tags.
You can quote-reply this post to see the code tags.

Does the keypad.getKey() function wait for a key to be pressed?
If not then that for loop will run in no time flat.
But you won't notice at all. What you type will be echoed regardless.

You need to model more what you want to happen and how before coding.

Code:
void loop()
{
  for(r=0;r<10;r++)
   {
  char key = keypad.getKey();

     
  if (key != NO_KEY)
  {
     Serial.print(key);
    }
  }
}
1925  Using Arduino / Programming Questions / Re: Serial Port Error - Arduino and Processing can't run together on: August 07, 2013, 03:15:45 pm
Actually COM has two ends. That makes it easier to get Arduino and ( Processing or some other app like the IDE ) to communicate. But you could open Processing and the IDE & Serial Monitor then when Arduino plugs in it should get assigned another COM port.

Serial for Processing has functions to check port availability.

When you can check, don't assume. If you find yourself relying on assumption then it's time to learn how to check.
1926  Using Arduino / Programming Questions / Re: Beginner Serial Communication on: August 07, 2013, 12:46:26 pm
In the datasheet specifies that Rx and Tx pins used 3V, but the pins 10 and 11 in Arduino UNO are 5V and I try to connect directly, so it might not work?

Much obliged.

5V TX going into a 3V RX may work or it may burn up the RX circuit either quickly or slowly.

You can make a voltage divider to change the 5V level closer to 3V.
I used 3 4.7k resistors because 2 of those in series made a 9.4k and I have no 9.4k's. So my 5V TX goes into 1 4.7k resistor and at the other end I connect my wire to the 3V RX. Same place as the wire connects I have the 2 4.7k resistors in series also connected and the other end of those (one after the other, in series) connects to ground. 1/3 of the voltage is grounded while 2/3 goes to RX.

Regular diodes drop .7V. Two in a row drops 1.4V. 4.5V - 1.4V = 3.1V.

The diode and resistor method requires 3.3V at the device end and uses that to make a pullup for RX. It starts with a diode connected to the Arduino TX wire, pointed to stop flow from TX to RX. The other side of the diode connects to the RX pin and also connected is 3.3V through a resistor like 4.7k or 10k. When TX is HIGH the RX pin gets 3.3V. When TX is low the pullup current flows through the diode and RX is pulled LOW.

The last one is probably the most energy-efficient. All of them are cheap and easy.
 
1927  Using Arduino / Programming Questions / Re: Serial Port Error - Arduino and Processing can't run together on: August 07, 2013, 12:30:28 pm
When the IDE has the com port resource, what do you expect the PC to tell you when you start anther program that asks for the same resource?

Load the program to Arduino, either shut the IDE off or change the serial port it uses in Tools, start up you other program (Processing) and get on with it.

1928  Using Arduino / Programming Questions / Re: Beginner Serial Communication on: August 07, 2013, 12:22:24 pm
How you put your Arduino serial to 3.3V? Did you use a ttl converter, eg https://www.sparkfun.com/products/8745?

No, I didn't use a TTL converter. I ended up using software serial on digital IO pins 10 and 11. The RX pin (10) is receiving data at 3.5V from the LR4 interface board while the TX pin (11) is transmitting at 4.5V (~5V standard).

There are at least 3 ways to get that 4.5V-5V TX signal down to nominal 3.3V. One uses a diode and a resistor and the device 3.3V output, if it has one. One uses a voltage divider with one resistor at half the value of the other, and the third is to put 2 diodes in series between Arduino TX and device RX.
1929  Using Arduino / Programming Questions / Re: Arduino receiving different values through serial on: August 07, 2013, 11:58:48 am
Try just running your Arduino program with serial monitor. If you get garbage then check the baud rate at each end. Once you have that working right then use that to check whatever your PC program is sending.

These two lines compile exactly the same. Which is easier to read/understand?

Code:
    if (key == 57)


Code:
    if (key == '9')
1930  Using Arduino / Programming Questions / Re: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's on: August 05, 2013, 01:23:29 am
Your TX sends 3 bytes down serial but the bits are transmitted over time, 520 usecs per character at 19200 with start and stop bits. Arduino runs 8333 cycles in the time that 1 serial character comes through at that rate. Will your code get to read before then? Write as if it will, it certainly can, don't add useless blocking delays such as I will now put into the RX code.

The example sketch I put above uses 2 "UNO"s and 3 jumpers and each one blinks led 13 for status, showing that other tasks can be added to loop() with one example in there looking for backslash sequences to write binary data. That's an example of handling coded data as it arrives. Both UNOs run the same code.

Your TX --- that sends bytes FE val2 val5 then delays 150 ms, repeat
Code:

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(0);
analogValue5 = analogRead(4);

// remap values

val2 = map(analogValue2, 0, 1023, 0, 253);  // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 0, 253);

Serial.write(254); //SYNC char
Serial.write(val2);
Serial.write(val5);
delay(150);                 // blocking code
}


Your RX modified to wait for data that may not come = lockup
Code:

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// led pins

pinMode (10, OUTPUT);
pinMode (11, OUTPUT);

delay(1000);

}

void loop()
{
  if (Serial.available() > 0)
{
    incomingByte = Serial.read();
    if (incomingByte == 254)
   
      while ( Serial.available() == 0 ); // blocking code that fits with top-down coding
      sensor1 = Serial.read();
      while ( Serial.available() == 0 ); // blocking code
      sensor2 = Serial.read();
      analogWrite (10, sensor1); // why do you write to sensors?
      analogWrite (11, sensor2);
      delay(150);
      Serial.print("S1 = ");
      Serial.println(sensor1,DEC);
      Serial.print("S2 = ");
      Serial.println(sensor2,DEC);
    }
  }   
[/quote]
1931  Using Arduino / Programming Questions / Re: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's on: August 04, 2013, 01:54:30 pm
Here is a way of coding for serial bytes as asynchronous events on 2 UNO's.

Code:
/*
Sketch to run on 2 UNO's with USB to a PC and a software serial connection between.
Open 2 monitors, what you enter on one should make output on the other.
What characters that can be printed are, the rest are displayed as hex.
This sketch also looks for backslashes followed by 2 hex digits to write as binary data.
Lastly the sketch uses the pin 13 led as a blinking status light.

Software serial wiring is ground to ground, 8 to 9, 9 to 8. That's all.

Tested and working, IDE 1.03
*/

#include <ctype.h>
#include <SoftwareSerial.h>
SoftwareSerial Ser1(8,9);

byte statLed = 0; // 0 is off, 1 is on
const byte statLedPin = 13; // status led blinks 1 sec ON/OFF
unsigned long statLedMark, statLedWait = 1000UL;
unsigned long timeNow, timeLast;

byte ASCtoHEX( char isHexDigit, byte shiftLeft )
{
  static byte hexCode;

  if ( isdigit( isHexDigit ))
  {
    hexCode = ( isHexDigit - '0' ) << shiftLeft;
  }
  else
  {
    hexCode = ( toupper( isHexDigit ) - 'A' ) << shiftLeft;
  }

  return hexCode;
}

void setup()
{
  Serial.begin(57600);
  Ser1.begin(57600);

  pinMode( statLedPin, OUTPUT ); // default is LOW
  statLedMark = timeLast = millis();
}

void loop()
{
  static char serRead[ 2 ] = {
    0, 0   };
  static byte justRead = 0; // bits 0 & 1 for ser/ser1 
  static byte escapeCodeState = 0;
  static byte escapeCodeByte = 0;

  timeNow = millis();
  if ( timeNow != timeLast )
  {
    timeLast = timeNow;
   
    if ( timeNow - statLedMark >= statLedWait ) // blink
    {
      statLedMark = timeNow;
      statLed = !statLed;
      digitalWrite( statLedPin, statLed );
    }
  }

  if ( Serial.available())
  {
    *serRead = Serial.read();
    justRead |= 1; // Serial read sets bit 0
    if ( isprint( *serRead ))
    {
      Ser1.print( *serRead );
    }
    else if ( *serRead == 10 )
    {
      Ser1.print( '\n' );
    }
    else
    {
      Ser1.print( " x" );
      if ((byte) *serRead < 0x10 )
      {
        Ser1.print( "0" );
      }
      Ser1.print((byte) *serRead, HEX );
    }
  }

  if ( Ser1.available())
  {
    *( serRead + 1 ) = Ser1.read();
    justRead |= 2; // Serial read sets bit 1
    if ( isprint( *( serRead + 1 )))
    {
      Serial.print( *( serRead + 1 ));
    }
    else if ( *( serRead + 1 ) == 10 )
    {
      Serial.print( '\n' );
    }
    else
    {
      Serial.print( " x" );
      if ((byte)( *( serRead + 1 )) < 0x10 )
      {
        Serial.print( "0" );
      }
      Serial.print((byte) *( serRead + 1 ), HEX );
    }
  }

  if ( justRead ) // looking for \## to print data bytes from hex
  {
    if ( justRead & 1 ) // Serial read
    {
      justRead &= 0xFE; // & mask clears bit 0

      switch ( escapeCodeState )
      {
      case 0 : // looking for backslash
        if ( *serRead == '\\' )
        {
          escapeCodeState = 1; // will expect 2 HEX digits
        }
        return; // to loop()
      case 1 : // looking for 1st hex digit in ascii
        if ( isxdigit( *serRead ))
        {
          escapeCodeByte = ASCtoHEX( *serRead, 4 );
          escapeCodeState = 2;
        }
        else
        {
          escapeCodeState = 0; // error, back to watching for \
          escapeCodeByte = 0;
        }
        return; // to loop()

      case 2 : // looking for 1st hex digit in ascii
        if ( isxdigit( *serRead ))
        {
          escapeCodeByte |= ASCtoHEX( *serRead, 0 );
          Ser1.write( escapeCodeByte );
        }
        escapeCodeState = 0; // back to watching for \
        escapeCodeByte = 0;
        return; // to loop()
      }
    }
  }

}

1932  Using Arduino / Programming Questions / Re: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's on: August 04, 2013, 11:36:41 am
Quote from: Runaway Pancake link=topic=179860.msg1341264#msg1341264
If not, then what?
Are you sure that -every- serial read is preceded by a serial available check?

Does -every- require that?
I don't think so.
Let's not play "20 Questions".  If you have something to add then do so.

Yes you need to make sure there's a byte ready for every read, not just assume or assume a small wait will always do the trick. Arduino is insanely faster than serial data.

This isn't 20 questions for us, it's 20 clues and lately they're the same clue that keeps getting dropped.
1933  Using Arduino / Programming Questions / Re: Two Potentiometer Arduino to xbee to xbee to Arduino to two Led's on: August 04, 2013, 01:15:31 am
Quote from: Runaway Pancake link=topic=179860.msg1341264#msg1341264 If not, then what?
[/quote

Are you sure that -every- serial read is preceded by a serial available check?
 
1934  Using Arduino / Programming Questions / Re: Help Please! Can someone check the source code of my program please? on: August 02, 2013, 10:24:30 am
With regards to your code, everywhere an error can occur you should have a planned way to deal with it and an error handler to implement that even if it means shutdown.

In practice nothing is perfect. Engineers design fail safes and will put in a cheap part that breaks before extensive damage will occur where they can. It's better to lose a rocker arm than have a cylinder go through the head, etc. But these things usually don't come up in Programming Questions threads.

If a project looks dangerous we usually point that out and there's a general back off.

1935  Using Arduino / Programming Questions / Re: Help Please! Can someone check the source code of my program please? on: August 02, 2013, 09:58:51 am
The second one.

Member of the fundamentalist church of Murphy here. Please come and visit.
Pages: 1 ... 127 128 [129] 130 131 ... 364