Show Posts
Pages: 1 [2] 3 4 ... 1329
16  Using Arduino / Programming Questions / Re: Direct port manipulation - SPI CS Pin on: September 19, 2014, 06:34:35 pm
Timing test:

Code:
#include <digitalWriteFast.h>

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  }  // end of setup

void loop ()
  {
  unsigned long start;
  unsigned long finish;
  const unsigned long iterations = 10000;
 
  unsigned long counter;
 
  start = micros ();
  for (counter = 0; counter < iterations; counter++)
     digitalWrite (10, HIGH);
  finish = micros ();
  
  Serial.print ("Using digitalWrite = ");
  Serial.print (finish - start);
  Serial.println (" uS");

  start = micros ();
  for (counter = 0; counter < iterations; counter++)
     digitalWriteFast (10, HIGH);
  finish = micros ();
  
  Serial.print ("Using digitalWriteFast = ");
  Serial.print (finish - start);
  Serial.println (" uS");
  
  while (true) {}
  }  // end of loop

Results:

Code:
Using digitalWrite = 54708 uS     <-- i.e. 5.471 µS each
Using digitalWriteFast = 5784 uS  <-- i.e. 0.579 µS each

So yep, you will save 5 µS by direct port manipulation. But compare that to how long it takes to access the SD card, open a file, write to it, and close it, and that will be virtually nothing.
17  Using Arduino / Programming Questions / Re: Direct port manipulation - SPI CS Pin on: September 19, 2014, 06:25:38 pm
Perhaps I should re-phrase my question:

Is there any macro I can use that allows me to write a function say "PORTx |= 1<<bitnumber" instead of having to do this manually each time I want to set up a library on a specific platform?

Maybe: https://code.google.com/p/digitalwritefast/

Quote
I am not sure how the SD library sets/clears the CS pins, however, assuming it does so using a digital write, this operation on its own might take as much or more than the time needed for the data to be sent.

I strongly doubt that. Have you got any figures to back that up? The SD library has to read/write hundreds if not thousands of bytes to the SD card, a few microseconds setting chip select at the start won't make any difference.
18  Using Arduino / Programming Questions / Re: Unable to active the pins at output on: September 19, 2014, 06:20:54 pm
It looks to me like you have some outputs all connected together, and some going HIGH and some LOW at the same time. This is a good way to burn up the output drivers on your Arduino. Why are you doing this?
19  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 19, 2014, 06:07:54 pm
Hey, on a side note, I notice you said Lua Rules. Are you an expert Lua programmer? Where are good resources to get examples and tutorials on learning it?

The Programming In Lua book is very good, written by Roberto Ierusalimschy, the author of Lua. That is available online:

http://www.lua.org/pil/

I have a post about Lua tables (which are pretty fundamental to Lua) here:

http://www.gammon.com.au/forum/?id=4903

and: http://www.gammon.com.au/forum/?id=6036

You can install Lua on Mac, Windows, Linux, and have a play with it. It's pretty good fun to work with.
20  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 19, 2014, 02:20:38 am
Yes, I think it is the principal file (the one the sketch is named after) followed by the other files in alphabetical order. I don't like this system personally, it makes things confusing. However you are right that global variables would logically go, therefore, in the principal .ino file.
21  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 19, 2014, 12:42:11 am
To clarify, if you add .ino files they are concatenated by the IDE into one file, and thus global variable are shared between all the .ino files.

If you add a .cpp file (or a .h file) they behave in the usual way, and you have to manually take steps to share variables between them (eg. using extern).
22  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 19, 2014, 12:31:40 am
What would you say is the benefit to internal functions versus external functions? It would almost seem like extra work in that variables would have to be defined in both places. Thanks!

The benefit is neatness, or more organized code. Only you can say if it is worth it in this particular case. You can always pass variables to functions, they don't have to be global.
23  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 19, 2014, 12:28:43 am
Regardless of the complexity of the rest of the code, what I posted should handle the general case. You have a "state" which is how far through the sequence they got. You could even have switches repeated, because as we work through the state array, only one switch is valid at any point (eg. it could be 2-4-2-4-4-1). Thus the fact that 4 appears more than once doesn't affect the behaviour, because the important thing is that they have to step through the states (ie. 1-2-3-4-5-6) with only one input being valid for each transition.

In fact, I think you should allow for the same switch being pressed more than once, because after all, if you can only press each switch once, and you have four switches, then once you have pressed three of them correctly, the remaining one must be the correct one for the last press. That makes it too easy.
24  Using Arduino / Programming Questions / Re: Simplify code using an array? on: September 18, 2014, 11:52:09 pm
How about this?

Code:
const int numberOfSwitches = 4;
const byte switchPins [numberOfSwitches]  = { 8, 9, 10, 11 };  // or whatever
const byte wantedOrder [numberOfSwitches] = { 4, 2, 1, 3 };  // press in this order

byte state = 0;

void disArm ()
  {
  Serial.println ("Bomb disarmed.");
  }  // end of disArm
  
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  // activate pull-ups, press switch by grounding it
  for (byte i = 0; i < numberOfSwitches; i++)
    pinMode (switchPins [i], INPUT_PULLUP);
  Serial.println ("Ready.");
  }  // end of setup

void loop ()
  {
  byte whichSwitch = 0;
  byte numberOn = 0;
  
  // read all switches
  for (byte i = 0; i < numberOfSwitches; i++)
    {
    if (digitalRead (switchPins [i]) == LOW)
      {
      whichSwitch = i;
      numberOn++;
      Serial.print ("Switch ");
      Serial.print (whichSwitch + 1);
      Serial.println (" pressed.");
      }  // end of switch pressed
    }  // end for loop
  
   // nothing pressed?
   if (numberOn == 0)
     return;
      
   delay (100); // debounce
  
   // detect cheating attempts to press more than one switch
   if (numberOn > 1)
     {
     state = 0;
     Serial.println ("More than one switch pressed, state reset.");
     return;
     }  // end of if too many
    
   // check this is the right one
   if ((whichSwitch + 1) == wantedOrder [state])
     {
     Serial.println ("Correct switch pressed.");
     state++;
     if (state >= numberOfSwitches)
       {
       disArm ();
       state = 0;  
       }  // end of all switches pressed correctly
     }  // end of if correct switch
   else
     {
     state = 0;
     Serial.println ("Incorrect switch pressed, state reset.");
     }  // end of wrong switch
  
  }  // end of loop

This allows for the actual switches to be connected to any pins (see switchPins). The correct sequence is in wantedOrder variable (numbered 1 to 4 in this case).

The "state" variable keeps track of how far through the correct sequence you are. If you get one wrong it goes back to state 0, which is no correct switches pressed.
25  Using Arduino / Programming Questions / Re: is it possible to use variables when calling functions? on: September 18, 2014, 07:35:23 pm
Isn't this confusing? All these "a" variables?

Code:
byte a[8];
...
void writeArduinoOnMatrix1() {
  /* here is the data for the characters */
  static byte a[8]={
    B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100,  };
...
  displayThem(a);
}  
void writeArduinoOnMatrix2() {            
  static byte a[8]={
    B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001,  };
...
  displayThem(a);
}
void writeArduinoOnMatrix3() {            
  static byte a[8]={
    B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001  };
...
  displayThem(a);
}
void writeArduinoOnMatrix4() {
  // here is the data for the characters
  static byte a[8]={
    B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100  };
...
  displayThem(a);
}

Instead of trying to call these different functions, why not just display the correct pattern from an array? Like this:

Code:
// 4 lots of 8
const byte bitPatterns [4] [8] =
{
  { B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100, } , //  matrix 1
  { B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001, } , //  matrix 2
  { B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001, } , //  matrix 3
  { B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100, } , //  matrix 4
};  // end of bitPatterns

And display like this:

Code:
 if (hex >= 1 && hex <= 4)
      displayThem (bitPatterns [hex - 1]);
  else
     lc.clearDisplay(0);

And make the display function simpler:

Code:
void displayThem(const byte a[8])
  {
  for (byte i = 0; i < 8; i++)
    lc.setRow(0,i,a[i]);  
  }  // end of displayThem

Now the problem of calling lots of different functions quietly goes away. Finished code (not tested):

Code:
//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn
 pin 11 is connected to the CLK
 pin 10 is connected to LOAD
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);
int pin1 = 2;
int pin3 = 3;
int pin4 = 4;
int pin6 = 5;
//pins with 0 on the left
//      4  5  6
//
//      1  2  3
//connect vcc to pins 2 and 5
int input1 = 0;
int input2 = 0;
int input3 = 0;
int input4 = 0;
int hex;

// 4 lots of 8
const byte bitPatterns [4] [8] =
{
  { B00111100,B01111110,B11100111,B11001011,B11010011,B11100111,B01111110,B00111100, } , //  matrix 1
  { B11111111,B11111110,B11100000,B11010000,B11001000,B11000100,B11000010,B10000001, } , //  matrix 2
  { B11111111,B01111111,B00000111,B00001011,B00010011,B00100011,B01000011,B10000001, } , //  matrix 3
  { B00011000,B00111000,B00111000,B00011000,B00011000,B00011000,B00111100,B00111100, } , //  matrix 4
};  // end of bitPatterns

/* we always wait a bit between updates of the display */
unsigned long delaytime=10;

void setup() {

  Serial.begin(9600);
  pinMode(pin1, INPUT);
  pinMode(pin3, INPUT);
  pinMode(pin4, INPUT);
  pinMode(pin6, INPUT); 
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0);
}

void displayThem(const byte a[8])
  {
  for (byte i = 0; i < 8; i++)
    lc.setRow(0,i,a[i]); 
  }  // end of displayThem

void loop()
  {
  Serial.println (hex);
  input1 = digitalRead(pin1);
  input2 = digitalRead(pin3);
  input3 = digitalRead(pin4);
  input4 = digitalRead(pin6); 
  if (input1 == 0)
  {
    bitWrite (hex,0,1);
  }
  else
  {
    bitWrite (hex,0,0);
  }
  if (input4 == 0)
  {
    bitWrite (hex,1,1);
  }
  else
  {
    bitWrite (hex,1,0);
  }
  if (input2 == 0)
  {
    bitWrite (hex,2,1);
  }
  else
  {
    bitWrite (hex,2,0);
  }
  if (input3 == 0)
  {
    bitWrite (hex,3,1);
  }
  else
  {
    bitWrite (hex,3,0);
  }

  if (hex >= 1 && hex <= 4)
      displayThem (bitPatterns [hex - 1]);
  else
     lc.clearDisplay(0);

}

I have no idea what this is doing, BTW:

Code:
input1 = digitalRead(pin1);
  input2 = digitalRead(pin3);
  input3 = digitalRead(pin4);
  input4 = digitalRead(pin6);  
  if (input1 == 0)
  {
    bitWrite (hex,0,1);
  }
  else
  {
    bitWrite (hex,0,0);
  }
  if (input4 == 0)
  {
    bitWrite (hex,1,1);
  }
  else
  {
    bitWrite (hex,1,0);
  }
  if (input2 == 0)
  {
    bitWrite (hex,2,1);
  }
  else
  {
    bitWrite (hex,2,0);
  }
  if (input3 == 0)
  {
    bitWrite (hex,3,1);
  }
  else
  {
    bitWrite (hex,3,0);
  }
26  Using Arduino / Programming Questions / Re: Compiler warning when using F() macro on: September 18, 2014, 05:25:17 pm
I got that result too. However if you open up Print.cpp (where the error is) and change the following it will go away.

Change:

Code:
size_t Print::print(const __FlashStringHelper *ifsh)
{
  const char PROGMEM *p = (const char PROGMEM *)ifsh;

to:

Code:
size_t Print::print(const __FlashStringHelper *ifsh)
{
  const char *p = (const char *)ifsh;

The error message was about putting an attribute on a local variable (in this case "p"). Removing the attribute appears to fix it.
27  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 18, 2014, 04:37:12 pm
This is absurd and irrational.

THAT'S WHAT IS HAPPENING HERE:
...

I DONT KNOW, sometimes is way the best answer.

@Piton: If you had actually posted your observed results, like I did above, someone else could easily have spotted that your loop was resetting after 600 times through. But your minimal posts, no posted output, no posted error messages, are just not helpful. The quality of advice you get depends on the work you put into asking good questions. And insulting people who are trying to help you doesn't help one bit.

How to use this forum
28  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 18, 2014, 04:17:43 pm
Rationality, something you need to go for coding.  That's why gammon library is not working at  ALL!

I compiled your most recently-posted code (after adding an extra brace at the end which you must have inadvertently left out), and it compiled OK.

Your  use of language is confusing, I was not sure what your real problem was. Initially I thought you were saying it wouldn't compile. Then you said this:

Quote
foo value 0 to 640 +-

I need 10.000 and more.

I'm not sure what that means, so I tried it out:

Code:
Received fnum = 1.58
Received foo = 81
Received fnum = 1.60
Received foo = 82
Received fnum = 1.61
Received foo = 83
Received fnum = 1.63
Received foo = 84
Received fnum = 1.64
Received foo = 85
...
Received fnum = 9.87
Received foo = 633
Received fnum = 9.88
Received foo = 634
Received fnum = 9.90
Received foo = 635
Received fnum = 9.91
Received foo = 636
Received fnum = 9.93
Received foo = 637
Received fnum = 9.94
Received foo = 638
Received fnum = 9.96
Received foo = 639
Received fnum = 9.97
Received foo = 640
Received fnum = 9.99
Received foo = 641      <---- here
Received fnum = 1.00
Received foo = 42
Received fnum = 1.01
Received foo = 43
Received fnum = 1.03
Received foo = 44
Received fnum = 1.04
Received foo = 45
Received fnum = 1.06
Received foo = 46
Received fnum = 1.07
Received foo = 47
Received fnum = 1.09
Received foo = 48
Received fnum = 1.10
Received foo = 49

And yes, foo stops at 640, and fnum stops at 9.99.

But that's exactly what your code told it to do:

Code:
void loop()
{

 long foo = 42;
 
 for (float fnum = 1; fnum <= 10; fnum += 0.015)

You told fnum to go from 1 to 10 (which it did) and foo starts at 42 and goes up by 1 each time. With the loop going from 1 to 10 in increments of 0.015, you will go through that loop 600 times, and thus foo will go from 42 to 642. (In fact, fnum goes to 9.99 and foo goes to 641 because you compare equal for the end of the loop, not greater than.)

Then foo goes back to 42 because "loop" exits, gets started again, and foo is reset back to 42.

Quote
Rationality, something you need to go for coding.  That's why gammon library is not working at  ALL!

With all your screaming at me in caps, and analogies about doctors, did you not think to check your code, and see if the results compared favourably to what the code does?

And yes, I am rational. And the library works perfectly. Thanks for posting code that confirms it.
29  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 18, 2014, 03:58:46 pm
I just cant get the error, i'm not that skilled programmer.

Do you mean "I don't understand the error?" What error?

Is that clear enough for you?
30  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 18, 2014, 06:19:11 am
I would really love the library to work as intended instead of focusing on the ERW problem.

The library is working as intended. ERW would appear to be the problem.
Pages: 1 [2] 3 4 ... 1329
Powered by SMF 1.1.19 | SMF © 2013, Simple Machines