Show Posts
Pages: 1 2 3 [4] 5 6 ... 358
46  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 01:29:56 pm
...
Answer is to work in smaller units. If I want meters to 3 places then I work in millimeters or smaller depending on what math I will be doing. 32 bit int gives me 9 places. 64 bit int gives me 19 places. Both are much faster than 32 bit float.
Can be effective but not in this case, the point of my reasoning,
 
(180-162)/(184-162) => 0    if all numbers are treated as integer

making them long does not change that

(180000-162000)/(184000-162000)  => 0L

That's fine but for other values you can get 0 using 16 bit and non-zero using 32 bit.
That is a result of precision.
47  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 01:24:00 pm
Quote
very true, but the integer division makes the math less precise as it truncates to 0
straightforward casting to long won't help for the given formula

Answer is to work in smaller units.

newVal = 260000 - (( 260000 - 240000 )  *  ( 180000 - 162000 )  / ( 184000 - 162000 )) = 243636

Code:
void setup( void )
{
  Serial.begin( 115200 );
  Serial.println( );
  
  long range = 260000L - 240000L;
  long scaleMult = 180000L - 162000L;
  long scaleDiv = 184000L - 162000L;
  unsigned long newVal = 260000L - ( range * scaleMult / scaleDiv );

  Serial.println( newVal );
  
  Serial.print( newVal / 1000L );
  Serial.print( "." );
  newVal %= 1000L;
  if ( newVal < 100L )  Serial.print( "0" );
  if ( newVal < 10L )   Serial.print( "0" );
  if ( newVal == 0L )   Serial.print( "0" );
  else Serial.println( newVal );
}

void loop( void )
{
}

I used these techniques for industrial machining (NC/CNC), payroll and billing code long before FPU's became standard default PC hardware. You want to see someone go off, let them find out that their paycheck  is a penny too low or their bill is a penny too high.  smiley-roll
48  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 12:28:46 pm
Is that PIC code?
 
49  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 26, 2014, 12:23:03 pm
Because the serial print is your delay and you have buttons that finish bouncing by the time it's done.

Do you use a delay after serial available to allow the rest of the message to arrive?

50  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 26, 2014, 12:05:29 pm
Arrays, ARRAYS... use ARRAYS

You want to have the arduino remember the last button press state, and if the state does NOT change,( high to low or low to high) then don't do anything. Only when the button changes, do something.

You may also need to debounce the buttons too.

I might have something you can use, let me look.

Code:
const byte button = 2;
boolean state, lastState = LOW;

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  pinMode(button, INPUT);
}

void loop()
{
  state = digitalRead(button);
 
  if(state != lastState) // only do something if the button state changes, ie. not held down
  {
    lastState = state; // update lastState
    Serial.print("The buttons state is: ");
    Serial.println(state);
   
    if(state == HIGH) // do something ONLY when the buttons state is HIGH
      digitalWrite(13, !digitalRead(13) );
  }
}


That doesn't address bounce at all.

Bounce is when physical contacts make and break the connection many times in a short period.
Bounce happens over time. If all you do is look for an instant, you can't tell what is happening.

Debounce is when you watch or wait for the bouncing to stop for longer than the bouncing happens.
If my code watches the switch change state back and forth, and it's easy to test-sketch that, then it's bouncing.

Once the state stays the same for some period of time, once it is stable, THEN it's time to set a state that the rest of the code can use as button state.

Unstable button state has some uses but generally the desired logic is based on the ideal people think of rather than the reality we need to code for.

Throw in an array of buttons needing to work at the same time together with an array of leds (strips) and you have something that I gave working examples of how to do 3 days ago, but not all in 1 package.
http://forum.arduino.cc/index.php?topic=256847.msg1816262#msg1816262

I could have posted the 4x4 multiplexed buttons sketch but that's a whole nother step more complicated.
Maybe the thing to do is get it done for 1 button first.

51  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 11:31:10 am
Here are links to well done ready made lessons/explanations/examples ready already:

Arduino Foundations page:
http://arduino.cc/en/Tutorial/Foundations
Beginner howto on functions:
http://arduino.cc/en/Reference/FunctionDeclaration

A more complete Arduino programming  site:
http://sheepdogguides.com/arduino/FA1main.htm
Functions that return values:
http://sheepdogguides.com/arduino/FA1funcret.htm

More links to Arduino tutorials and example:
http://playground.arduino.cc/Main/TutorialList
http://playground.arduino.cc/Main/GeneralCodeLibrary


52  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 10:15:07 am
I always avoid float math on processors without FPU when possible, and even with, I don't like FP.
Cast the intermediate values to 32 bit, it's more than a magnitude faster.
 
very true, but the integer division makes the math less precise as it truncates to 0
straightforward casting to long won't help for the given formula

Quote
newVal = 260 - ((260-240) * ((180-162)/(184-162)))

as in the division the numerator > denominator


doing the multiplication first improves the result - and there casting to long will prevent overflow.
Quote
newVal = 260 - ((260-240) * (180-162)) /(184-162)

the result is now 244 which is in fact quite good.


Answer is to work in smaller units. If I want meters to 3 places then I work in millimeters or smaller depending on what math I will be doing. 32 bit int gives me 9 places. 64 bit int gives me 19 places. Both are much faster than 32 bit float.


53  Using Arduino / Project Guidance / Re: rpm - program on: July 26, 2014, 10:03:10 am
need a program to find the rpm of a wheel using a proximity sensor...


What kind of proximity sensor and what kind of wheel?

There are induction sensors capable of counting gear teeth as they pass, no magnet on the gear at all.
Such a sensor is not affected by dirt or oil and has no bulb to change.

If there is no dirt, a light and sensor are cheaper to use. Again, gear teeth or holes or slits in a wheel can be used.



 
54  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: July 26, 2014, 09:00:11 am
Try putting a led and resistor on pin 2 and make it light.

55  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 08:54:03 am
I always avoid float math on processors without FPU when possible, and even with, I don't like FP.
Cast the intermediate values to 32 bit, it's more than a magnitude faster.
 
56  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 05:18:43 am
Aditionally, how do I force the arduino to display all the 16 digits, even if they all are zero?

The evil demonstration sketch above shows one way to do that.
57  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 05:17:23 am
To keep the number of overloads of "print" to a reasonable number, "int"s are promoted to "long"s

And because the variable is a signed integer, the sign bit fills the extra bits. Try with 0x7FFF or unsigned int.

58  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: July 26, 2014, 04:20:14 am
I don't have one but here's the Arduino site specs page. Hey, I didn't know it's got a 32U4!  2.5K SRAM!

http://arduino.cc/en/Main/ArduinoBoardMicro

One question comes to mind, is your battery if it goes to VI at least 7V?

Did you try sleep mode idle?

Also the Pin Map is not as the UNO but instead the Leonardo.
It may matter if you address port registers, which I don't see.
http://arduino.cc/en/Hacking/PinMapping32u4


59  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: July 26, 2014, 03:34:30 am
A sleep mode tutorial with not the same code as yours:
http://www.engblaze.com/hush-little-microprocessor-avr-and-arduino-sleep-mode-basics/

Also you should put your code posted here into code tags so it views without html interpretation.
In the forum post edit window, the # button makes code tags around highlighted text.
60  Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: July 26, 2014, 02:48:57 am
Sensor code added, compiles but not tested at all and needs it very much.
Can't go on without feedback and corrections which takes time.
So when you're ready, there it is.

Code:

// SIMlib mode change as event code 7-25-14 rev 0

#include <OneWire.h>
#include <DallasTemperature.h>

#include <string.h>

// user serial i/o defines
const byte serialLen = 50;
char serialChar, serialData[ serialLen ];
byte serialCount, serialState; // default of every declared variable is 0
// end user serial i/o defines

char gsmMsg[ serialLen ] = "test GSM message";

// sensor defines
// Data wire is plugged into port 3 on the Arduino
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9
OneWire oneWire( ONE_WIRE_BUS );
DallasTemperature sensors( &oneWire );
// arrays to hold device addresses
const byte thermometers = 3;
DeviceAddress Thermometer[ thermometers ];
int temperature[ thermometers ];
unsigned long sensorsTimerStart, sensorsTimerWait;

byte  readSensorsState, readSensor;
// end sensor defines


const int ledPin =  8;

//================================================SETUP

void setup( )
{
  pinMode( ledPin, OUTPUT );
  Serial.begin( 115200 );

  // sensor setup
  // locate devices on the bus
  Serial.print( F( "Locating devices..." ));
  Serial.print( F( "Found " ));
  Serial.print( sensors.getDeviceCount(), DEC );
  Serial.println( F( " devices." ));

  // report parasite power requirements
  Serial.print( F( "Parasite power is: " ));
  if ( sensors.isParasitePowerMode() )
  {
    Serial.println( F( "ON" ));
  }
  else
  {
    Serial.println( F( "OFF" ));
  }

  oneWire.reset_search();

  byte f = 0; // this will flag if any devices are 'missing'.
  for ( byte i = 0; i < thermometers; i++ )
  {
    if (!oneWire.search( Thermometer[ i ] ))
    {
      Serial.print( F( "Unable to find address for Thermometer " ));
      Serial.println( i );

      // BIG QUESTION HERE -- what to do about the unfound sensors?
      // Answer is stop sketch and have the user fix them.
      f = 1;
    }
    else
    {
      Serial.print( F( "Device " ));
      Serial.print( i );
      Serial.print( F( "Address: " ));
      printAddress( Thermometer[ i ] );
      Serial.println();

      // set the resolution to 9 bit
      sensors.setResolution(Thermometer[ i ], TEMPERATURE_PRECISION );
      Serial.print( F( "Device " ));
      Serial.print( i );
      Serial.print( F( " Resolution: " ));
      Serial.println( sensors.getResolution( Thermometer[ i ] ), DEC );
      Serial.println();
    }
  }
  if ( f )
  {
    Serial.println( "\nFix the unfound sensors please!\n" );
    while ( 1 ); // halts the sketch
  }
  // end sensor setup

}

//================================================FUNCTIONS

inline void hwSerialRead( )
{
  switch ( serialState )
  {

  case 0 :

    if ( Serial.available( ))
    {           
      serialChar = Serial.read( );

      if (( serialChar == '\n' ) || ( serialCount == serialLen - 1 )) // end of line
      {
        serialState = 10;
      }
      else
      {
        serialData[ serialCount++ ] = serialChar;
      }
    }
    break;

  case 10 :

    if( !strcmp( serialData, "/END" ))
    {
      //      serialData[ 0 ] = 0x1a; // -- 0x1A is what gets sent to gsm
      Serial.println( F( "_" )); // for now, print to monitor
    }
    //Send a saved AT command using serial port.
    else if( !strcmp( serialData, "TEST" ))
    {
      Serial.println( F( " SIGNAL QUALITY " ));
      //     gsm.SimpleWriteln("AT+CSQ");
    }
    //Read last message saved.
    else if( !strcmp( serialData, "MSG" ))
    {
      Serial.println( gsmMsg );
    }
    else
    {
      Serial.println( serialData );
      //     gsm.SimpleWriteln( serialData );
    }   

    memset( serialData, 0, serialLen );
    serialCount = 0;
    serialState = 0;

    break;
  }
}

// sensor functions

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print(F("0"));
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(F("Temp C: "));
  Serial.print(tempC);
}

// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print(F("Resolution: "));
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();   
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print(F("Device Address: "));
  printAddress(deviceAddress);
  Serial.print(F(" "));
  printTemperature(deviceAddress);
  Serial.println();


// read sensors
// byte  readSensorsState, readSensor;
// unsigned long sensorsTimerStart, sensorsTimerWait;

inline void readSensorsToSerial( )
{
  switch ( readSensorsState )
  {

  case 10 : // REQUEST READ TEMPERATURE
    Serial.print( F( "Requesting temperatures..." ));
    sensorsTimerStart = millis();
    sensors.requestTemperatures(); // how long does this take???
    sensorsTimerStart = millis() - sensorsTimerStart;
    Serial.print( F( "DONE -- request ms " ));
    Serial.println( sensorsTimerStart );
    Serial.print( F("\nTemperatures are " ));
    sensorsTimerStart = millis();
    sensorsTimerWait = 100UL;
    readSensor = 0;
    readSensorsState = 20;
    break;

  case 20 : //  READ TEMPERATURE
    if ( millis() - sensorsTimerStart >= sensorsTimerWait )
    {
      temperature[ readSensor ] = ( sensors.getTempC( Thermometer[ readSensor ] ));
      Serial.print( temperature[ readSensor ] );
      Serial.print( F( " Celsius" ));
      if ( readSensor < thermometers - 1 )
      {
        Serial.print( F(", "));
        readSensor++;
      }
      else
      {
        Serial.print( F("\n\n"));
        readSensor = 0;
        //        lcd.clear();
        //        readSensorsState = 30;
        readSensorsState = 0;
      }
      sensorsTimerStart = millis();
    }
    break;

/*           uncomment when the LCD is added
  case 30 :
    if ( readSensor < thermometers )
    {
      lcd.setCursor( 0, readSensor );
      lcd.print( F( "T" ));
      lcd.print( readSensor );
      lcd.print( F( ":   C" ));
      lcd.setCursor( 3, readSensor );
      lcd.print( temp[ readSensor ] );
      readSensor++;
    }
    else
    {
      readSensor = 0;
      readSensorsState = 0;
    }
    break;
*/

  }
}

// end sensor functions


//================================================LOOP

void loop()
{
  hwSerialRead( );
  readSensorsToSerial( );
}
Pages: 1 2 3 [4] 5 6 ... 358