Show Posts
Pages: [1] 2 3 ... 354
1  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: Today at 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.
2  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: Today at 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.

3  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: Today at 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


4  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: Today at 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.
5  Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: Today at 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( );
}
6  Community / Bar Sport / Re: Arvind Gupta - Toys From Trash on: Today at 01:45:14 am
I started just looking at the electrical toys but since I see many that are not.

One though has a bottle of water that leaks through a hole at the bottom with constant output despite changing water height.  smiley-draw  Take note, Cracked Pot thread readers!


7  Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: July 25, 2014, 08:02:13 pm
So first sketch : compiled successfully. After loading up and checking serial monitor, nothing can be found on the screen.

That's funny, it runs for me and it will be the last step that I can run as I don't have the sensors or whatever LCD you probably have (I have a liudr 4x20 with backpack) or GSM or SIM900.

Did you make sure that serial monitor is set to the right speed and adds a newline to entries?

Quote
requesting temperature: I checked DS18B20 specs -> reading out is under 200ms if I'm right. And it depends maybe the lenght of the cable. Another one : SHT10 take less time. I am not a pro smiley

Can you set up a test sketch using the setup in your sketch and nothing in loop(), but at the end of setup put

Code:
  readsensorstimer = millis();
  sensors.requestTemperatures();
  readsensorstimer = millis() - readsensorstimer;
  Serial.print( F( "\n\n Request took: " ));
  Serial.print( readsensorstimer ));
  Serial.print( F( " ms\n" ));

I already have 100 ms waits between the request and each of the the actual reads.
Maybe 200 or more is needed but I am REALLY interested in how long just the request takes.

That bobcat, she is mean.
 
8  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 25, 2014, 06:30:52 pm
And btw casemod, it is often easier to visualize bits in groups of 4, which is what hexadecimal (HEX) is.
2 hex digits per byte, 4 for 16 bit, etc.
Hex numbers are written in source starting with 0x and binary starts with 0b.

Here's my evil demonstration sketch.  smiley-twist

Code:
// Comments left out on purpose.
// I wrote it, you figure it out even if you have to look things up!
// LEARN. If you're stuck, ask about the part you are stuck on.

void setup()
{               
  Serial.begin( 115200 );
  Serial.println( "\n\n  decimal = hex = binary\n" );

  for ( byte i = 0; i < 16; i++ )
  {
    if ( i < 10 )
    {
      Serial.print( " " );
    }
    Serial.print( i, DEC );
    Serial.print( " = 0x" );
    Serial.print( i, HEX );
    Serial.print( " = 0b" );
    byte j = 8;
    while ( !( i & j ) && ( j > 1 ))
    {
      Serial.print( "0" );
      j /= 2;
    }
    Serial.println( i, BIN );
  }
}

void loop()
{
}
9  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 25, 2014, 06:05:55 pm
Hi all,

Does someone have a code that can allow me to set bits in a variable as if they were digital ports?

For example, say I want to set some serial data to a shift register. I have a variable with 8bits.
But I ned to be able to set each of this 8 bits individually.

Regards

What you are asking about is explained here: http://playground.arduino.cc/Code/BitMath

When you know what is going on it is much easier than following a cookbook.
Understanding will let you make better formulas to fit different needs. Cookbook will not.
10  Using Arduino / Programming Questions / Re: Program can't open file on SD card on: July 25, 2014, 03:58:55 pm
      Micro SD adapter can be installed directly on breadboard. It has a same pin step.
So it is. Cheap and useful.

Clear, easy and excellent. I give you your first Karma point!

I think that cheap SD is probably the most bang for money + effort that Arduino can have.
It should be a standard project for 2nd level Arduino users to enable a greater reach, a mass file storage system.


 
11  Using Arduino / Programming Questions / Re: for () Statement question. on: July 25, 2014, 02:51:35 am
Code:
32 is 2^5
No, 2^5 is 7  smiley-wink

You're right, I wrote in the wrong language! I used (pretty sure that's) BASIC!
Someone get the whip, I've been a baaaad programmer!

By 2^5 I meant 2 to the 5th power.

12  Using Arduino / Programming Questions / Re: for () Statement question. on: July 25, 2014, 02:40:23 am
I learned a little AVR trick from Nick Gammon a while back.
You can toggle any port pin by writing the corresponding bit in the PINx register.

For this example I hope you can forgive the sin of using delay()....

Go ahead, test it. I did.
Code:
byte ledPin = 13;

void setup()
{                
  pinMode( ledPin, OUTPUT );    
}

void loop()
{
  PINB |= 32;  // tada! pin 13 is port B pin 5, this line toggles pin 13
  delay( 1000 );
}

Edit: erroneous comment changed, thanks AWOL!
13  Using Arduino / Programming Questions / Re: for () Statement question. on: July 25, 2014, 02:17:51 am
This sketch im using has this for () statement in the void setup()

void setup()
{
  randomSeed(analogRead(0))
  for (int z=1; z<7; z++)
   {
   pinMode(z, OUTPUT);
    }
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!



pinMode(), digitalWrite() and all the set a pin some way commands are like light switches.
They stay flipped until you change them.

If you code that way then adding pins means having to make fewer code changes.
It also makes smaller though a bit slower code.
 
14  Using Arduino / Programming Questions / Re: for () Statement question. on: July 25, 2014, 02:13:37 am
less snarky comments from other forum members?  smiley-grin

Is it snarky to say you forgot less typing?  smiley-twist
15  Using Arduino / Programming Questions / Re: for () Statement question. on: July 25, 2014, 02:09:48 am
Using loops is slower tho - each pass thru adds ~ 12uS.
I don't mind where it is just one occurrence in setup.
Like you Duncan, I don't use loops where timing is tight.

Wow! That much! Even 12 cycles would be bad after a while.
Pages: [1] 2 3 ... 354