Show Posts
Pages: 1 ... 5 6 [7] 8 9 ... 284
91  Using Arduino / Programming Questions / Re: When to use interrupt? on: August 08, 2014, 04:48:13 am
Gotcha, thanks.
Bear in mind that is only my take on it.

There is no reason, for instance, why an interrupt should not be used to respond to a button press even if it can be done by simply polling the input.  In fact, turning Devil's advocate, I could make a case to encourage people to use interrupts more often as long as they understand what they are doing.  It's just a shame that there are not more interrupts available (pin change interrupts excepted) on most Arduinos.

Stands back, puts on flack jacket and awaits comments.

92  Using Arduino / Programming Questions / Re: When to use interrupt? on: August 08, 2014, 03:07:11 am
Quote
What sort of circumstances typically warrant the use of an interrupt?
When the action to be reacted to could happen at any time and needs to be noticed without delay or could itself be so transient as to possibly be missed otherwise.

An example in my case is reading the input from encoders on robot wheels to count the number of turns of the wheel.  These inputs are used to ensure that each wheel is running at the same speed by adjusting one or both if a mismatch is detected.  Meanwhile the robot is scanning for obstructions by rotating a servo, flashing LEDs to indicate its current state etc. 

The encoder signal could arrive at any time and is by its nature is transient so could be missed.  Instead the ISR flags receipt of the input, increases a counter and exits.  The value of the counters is compared next time through loop(), during which time it is possible that they have been incremented several times, and the wheel speed(s) are adjusted.

If the encoders were polled in loop() then they could be in one of 2 states but the ISR ensures that the number of state transitions are counted correctly.
93  Using Arduino / Programming Questions / Re: Teach me what I'm doing wrong (again) on: August 08, 2014, 02:56:12 am
Using a FSM the system stays in a state until it is changed either by time elapsing of by an input changing.  So, yes, you can have the system stay in a state for an indefinite period of time by not using elapsed time as an exit condition for the state.

Here is some psuedo code to illustrate the principle.  It aims to wait with an LED off until a button is pressed then to blink the LED until the button is pressed again.  It can of course be extended to do other things and have more states.
Code:
state = 0
start of loop()
  switch state
    case 0
      wait for a button press
      if button A becomes pressed
        state = 10
        save start time
        turn on LED A
      end if
     
      case 10
        if period has elapsed
          state = 11
          save start time
          turn off LED A
        end if
       
        if button A becomes pressed
          turn off LED A
          state = 0
        end if
       
        case 11
        if period has elapsed
          state = 10
          save start time
          turn on LED A
        end if
       
        if button A becomes pressed
          state = 0
        end if
       
  end switch
end of loop() 
It is, of course, not real code but should show you the shape of what you are aiming for.
94  Using Arduino / Programming Questions / Re: How Arduino know which pins from a potentiometer connected on: August 08, 2014, 02:28:46 am
Code:
#define no_samples 44100
uint16_t nSineTable[no_samples];//storing 12 bit samples in 16 bit variable.
What Arduino is this being run on ?
95  Using Arduino / Programming Questions / Re: Teach me what I'm doing wrong (again) on: August 08, 2014, 02:23:14 am
Quote
Essentially what I want the code to do is to continually repeat this code

I just saw your latest post and see that it is the flashing LEDs that you want to repeat.  The for loop will do that but will extend the delay time by 5 so the program will become unresponsive for longer.  If you want to have the LEDs flash continuously until interrupted by an input then look at using the timing principle used in the BlinkWithoutDelay example in the IDE.

Save a start time and check frequently whether the required time period has elapsed.  If so then do whatever is required next.  If not then do something else such as reading an input and reacting to it.  At any point in time your program will be in a known state so the BWD principle lends itself well to being used as part of a Finite State Machine, which sound scary but isn't.

When the timing period finishes or input is detected change a state variable to a new value.  Then in loop() use if statements or switch/case to execute the required commands for that state.  Start by writing out all the states on paper, the conditions needed to exit each state and the state number to move to on exit.  The program then almost writes itself.
96  Using Arduino / Programming Questions / Re: Serial port communication keeps disconnecting for unknown reason on: August 08, 2014, 01:34:08 am
Quote
So its actually 3 different serial ports that use the same baud rate.
How many serial ports on the Arduino are they connected to and how ?
97  Using Arduino / Programming Questions / Re: Adafruit Motor Shield 2 Motors MATLAB on: August 07, 2014, 02:52:47 pm
A suggestion for you.  Forget about using MATLAB to control the motors for now and write a program to do it on the Arduino, perhaps as a series of actions to be taken each time through loop()
98  Using Arduino / Programming Questions / Re: Serial port communication keeps disconnecting for unknown reason on: August 07, 2014, 01:03:59 pm
Quote
For communications from Processing to Arduino, processing opens 3 serial ports, with the same port (9600)
Are you sure about this ?  I am not familiar with Processing but 9600 sounds suspiciously like the baud rate to me and it matches the baud rate in the Arduino code.  How are the three serial ports in Processing connected to the single one on the Arduino ?
99  Using Arduino / Programming Questions / Re: Adafruit Motor Shield 2 Motors MATLAB on: August 07, 2014, 12:58:56 pm
Sorry for getting your knickers in a twist. You might want to take that stick out a few inches....

Did I say for you to download it? Hmmm, no. 

This is a forum where people look for help. I, myself, am looking for help. There are bound to be people who encountered Adafruit motor shields and can understand a little about them and the MATLAB side of things. If you don't want to try help then don't, but there is no need for being an absolute douche for someone wanting help. By the way, my mystery code is a common piece of code within a MATLAB GUI which I stated I was using. It is to run a series of events when a button is pressed, in this case to begin the motors.

Thanks, I hope you have a horrible dinner  smiley
I am with Paul on this.  You have provided no details of the program running on MATLAB nor the program running on the Arduino apart from the cryptic snippets in your first post.  If you had given details of the Arduino library I was going to check whether the stepperStep() method was blocking and maybe suggest ways round it but you have made that impossible.

Sorry, but I feel that I can be of no more help with your problem but I am intrigued as to whether MATLAB is controlling the motors directly or via the Arduino.
100  Using Arduino / Programming Questions / Re: Serial port communication keeps disconnecting for unknown reason on: August 07, 2014, 11:02:14 am
Does your Processing program open and close the serial ports or do they remain open ?  Opening a serial port attached to most Arduinos will cause them to reset and begin the program again.  You can see this behaviour when you open the Serial monitor from the IDE.
101  Using Arduino / Programming Questions / Re: Adafruit Motor Shield 2 Motors MATLAB on: August 07, 2014, 10:58:02 am
What stepper motor library are you using on the Arduino ?
102  Using Arduino / Programming Questions / Re: Sketch compiling perfectly on one computer - but not on another on: August 07, 2014, 10:47:11 am
Can you please post the library files here or give a link to them.
103  Using Arduino / Programming Questions / Re: Looking for help with upload multiple ino files to Arduino Nano on: August 07, 2014, 07:47:50 am
All I did was to put the 3 files on separate tabs of the IDE and compile them.
104  Using Arduino / Programming Questions / Re: Looking for help with upload multiple ino files to Arduino Nano on: August 07, 2014, 07:36:08 am
It will be no consolation to you. I am sure,  but it compiles OK for me using 1.5.6-r2 and 1.0.5
105  Using Arduino / Programming Questions / Re: 3 BUTTON CODE NOT WORKING on: August 07, 2014, 07:23:45 am
How are the buttons wired ?
Have you got any pull down resistors on the input pins or are they just at a floating voltage until a button is pressed ?
Pages: 1 ... 5 6 [7] 8 9 ... 284