Show Posts
Pages: [1] 2 3 ... 1329
1  Using Arduino / Programming Questions / Re: Converting code to Arduino Sketch on: Today at 02:52:36 am
I was just looking at the datasheet, always a good starting point.
2  Using Arduino / Programming Questions / Re: Converting code to Arduino Sketch on: Today at 02:50:20 am
... unless I leave the kitchen.
3  Using Arduino / Programming Questions / Re: Converting code to Arduino Sketch on: Today at 02:49:54 am
I was using 1.0.0  in my kitchen I must admit. I can't comment on how well it works on other versions.
4  Using Arduino / Programming Questions / Re: Converting code to Arduino Sketch on: Today at 02:21:04 am
also I need to understand how to manipulate with direct port of Arduino , i read about it in the Arduino reference but it is still not completely clear to me . 

The same as in the code you posted, once you get the spelling of the constants right.
5  Using Arduino / Programming Questions / Re: Converting code to Arduino Sketch on: Today at 02:19:31 am
If you change PC3 to PINC3 and PC4 to DDC4 to match the names in the datasheet, it compiles without further changes.


Code:
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>

//global variables: encoder position and direction of rotation
volatile unsigned int enc_pos;  // use unsigned long for large distances
volatile unsigned char enc_dir;

/*
PORTC Pin change interrupt service routine. Decodes the encoder.
For algorithm, see Scott Edwards article from Nuts&Volts V1 Oct. 1995
(righthand bit of old A,B) xor (lefthand bit of new A,B) => dir.
Increment or decrement encoder position accordingly
*/

ISR (PCINT1_vect) {
static unsigned char enc_last=0,enc_now;
enc_now = (PINC & (3<<PINC4))>>4; //read the port pins and shift result to bottom bits
enc_dir = ( enc_last & 1 )^( (enc_now & 2)>>1 ); //determine direction of rotation
if (enc_dir == 0) enc_pos++;
else enc_pos--; //update encoder position
enc_last = enc_now; //remember last state
}


int main(void)
{

enc_pos=0; //Initialize encoder position

DDRC  &= ~(3<<DDC4); //Port C pins 4 and 5 as input
PCMSK1 |= (3<<PCINT12); //enable interrupt on pin change, bits 4&5 PORTC
PCICR |= 1<<PCIE1; //enable interrupt on pin change, PORTC
sei(); //enable global interrupts
}
6  Using Arduino / Installation & Troubleshooting / Re: Arduino Micro - new out of box - just weird blinks and USB not detected on: Today at 12:11:38 am
Did you read this?

http://arduino.cc/en/Guide/ArduinoLeonardoMicro

You may have to install a USB device driver.
7  Using Arduino / Programming Questions / Re: I2C communication between arduinos on: September 19, 2014, 11:45:33 pm
http://www.gammon.com.au/i2c

That has an example of a master acting as a slave as well. Scroll down to "Communicating with other devices".
8  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 19, 2014, 11:43:36 pm
Code:
// volatile - variables that can change without notice
volatile int sequence;          // variable to contain the power sequence

...

// trigger button
unsigned long previousTriggerMillis = 0;    // previous time when the trigger fired.
...
// function button
unsigned long previousFunctionMillis = 0;   // previous time when the function fired.

...
/*** Trigger Interrupt ****************************************************************************/
    // Interrupt to save the time when the power button's state changed and what it's state is now.
    // Can't debounce inside the interupt as it is never called after the de-bounce delay
void interruptTrigger()
{
  previousTriggerMillis = millis();
 }    /*** Trigger Interrupt end *******************************************************************/


/*** Function Interrupt ***************************************************************************/
    // Interupt to save the time when the function button's state changed and it's state is now.
    // Can't debounce inside the interupt as it is never called after the de-bounce delay
void interruptFunction()
{
  previousFunctionMillis = millis();
  digitalWrite(output3LEDPin, !digitalRead(output3LEDPin));    // debug - toggle LED3
}    /*** Function Interrupt ***************************************************************************/

Why is sequence volatile? It isn't used in an interrupt. However previousTriggerMillis and previousFunctionMillis should both be volatile because they are used in an interrupt.


http://www.gammon.com.au/interrupts
9  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 19, 2014, 11:34:36 pm
My argument against const instead of #define -  how do you "define" stuff like
#define RAIN "if it rains you do not need to go swimming if you want to get wet"

Code:
const char * RAIN = "if it rains you do not need to go swimming if you want to get wet" ;
10  Using Arduino / Programming Questions / Re: Direct port manipulation - SPI CS Pin on: September 19, 2014, 07:38:19 pm
The digitalWriteFast turns into port manipulation if it can (ie. if the port is a constant and not a variable).

As for the timings, surely you won't read a byte at a time from the SD card? If you are, there is your bottleneck. Read something like 512 bytes. And even the TFT device should be able to be selected once and then a whole lot of SPI sent to it.
11  Using Arduino / Installation & Troubleshooting / Disconnecting USB causes IDE to use 100% CPU on Linux on: September 19, 2014, 06:51:21 pm
On the Ubuntu version of the Arduino IDE  1.0.5, disconnecting (unplugging) the USB cable from the PC to the Arduino Uno causes CPU usage for the Java IDE runtime to jump to 100%. What is it doing?

Code:
top - 09:43:14 up 1 day, 23:54,  4 users,  load average: 0.71, 0.60, 0.60
Tasks: 246 total,   1 running, 244 sleeping,   0 stopped,   1 zombie
%Cpu(s):  8.8 us,  5.7 sy,  0.0 ni, 85.5 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:  16376056 total, 16104688 used,   271368 free,   641840 buffers
KiB Swap: 31249404 total,        0 used, 31249404 free.  9019168 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
 2142 nick      20   0 6874812 163476  25408 S 100.1  1.0   0:27.44 java       
                                               ^^^^^

Steps to reproduce:

  • Compile and upload Blink sketch
  • Open Serial monitor
  • Run the "top" command in a console window
  • Unplug the USB cable from the Arduino
  • Note that the java process CPU climbs from around 7% to 100%
12  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.
13  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.
14  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?
15  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.
Pages: [1] 2 3 ... 1329