Address

Author Topic: Arduino Sketch help  (Read 1135 times)

Soarman

  • Newbie
  • *
  • Posts: 7
    • View Profile
Arduino Sketch help
« on: November 17, 2014, 11:10:08 AM »
Hello all, so, as a pre-cursor, I am not a software programmer. What I learned about C and programming the Arduino, I did over this weekend and the 101 and 102 classes last week at Familab in Longwood so please excuse the neophyte approach to the attached. I know for a fact that the approach I have taken is very simplified and I also know it doesn't meet one of the functional requirements I have (see 1 below) but I have no idea how to go about correcting my approach. I don't have the experience. I'm hoping someone would be able to provide me guidance in this area - I'm open to any and all suggestions.
 

The hardware system is fairly simple, an ACME screw transports a platform over 180mm in about 5 to 6 seconds via a geared 6V motor. There is a limit switch (edfUpSw and edfDnSw) at each end of travel to stop the motor. The input signal to drive action is a digital output from a RC receiver - txSw (I use Jeti Model systems that can configure receiver outputs as a standard servo pin, a digital input or a digital output). The Arduino also controls an RC ESC for another motor in the system. This ESC is only enabled on one end of the platform travel (edfUP). Additionally, the first time the system is deployed, the ESC is only powered to 70% for the first 5 seconds and then switches to 100% (spin70_100 and spin100). All subsequent ESC engagement is directly to 100% once the system is deployed (edfUP). I've attached the .ino file.
 

Here are the areas I know I have not been able to cater for and would greatly appreciate any guidance. I am aware of OOP as it applies to eliminating the delay() function but at this time, I am simply not experienced enough to implement it yet I believe, this may be the answer to my issues. So,
 

1). I want to monitor the state of the txSw at all times. Any time this input changes, I want the appropriate course of action to occur. The way I have this structured tells me that I cannot achieve this. This switch could change at any time, even during the spin70_100, spin100, edfUP and edfDN functions so I need a way to structure the code to manage that. If the txSw goes to 1, the drive motor drives the unit up, if it is 0, it drives it down. Any advise would be appreciated
 

2). I am concerned about the effect of switch noise from the two limit switches. Should I be? If so, what is the best course of action for me on this?
 

3). As I noted above, I am vaguely familiar with the use of the millis() function as a timer vs using delay() which ties the processor up 100%. I'd like to get this system "multi-tasking" so that I can better cater for the situation I've noted in 1 above.
 

Other than these few items, I think I have the code doing what I want it to do. I've also attached my written overview of the functionality I'm trying to achieve as a reference.
 

As I said, any help would be greatly appreciated.
 

Tony Elliott
 
 

Iggy

  • FamiLAB Member
  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Arduino Sketch help
« Reply #1 on: November 17, 2014, 02:54:49 PM »
Hi Tony,

  You've done the heavy lifting so I'll try to answer your questions as briefly as possible.

1. I would use SerialEvent for this.
2. I don't think noise will be a problem. If you want to swim in the deep end, you could look at how one of the open source RepRap firmwares handle limit switches. In fact, you might be able to re-use the motion control software for your purposes.
3. I think you're looking for  timer interrupts. Similar to a serial event above, you can use a timer interrupt to schedule actions. For example, turn the beeper on then use the timer to turn it off 1 second later instead of asking the Arduino to spin while the buzzer sounds. A better alternative might be to check for both beeperOn and beeperOff condition in your main loop instead of sounding the beeper for 1 second while the beeperOn condition is met. Either way, it is good practice to try to avoid delay() calls where possible.

  I hope that points you in the right direction.

Cheers,
~Iggy

Soarman

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Arduino Sketch help
« Reply #2 on: November 17, 2014, 03:40:46 PM »
Iggy, many thanks for your response.
 
I think I have crossed the boundaries of two subjects - that of the receiver used in radio control model airplanes and boats and the receiver port on the Arduino!. The receiver I am referring to above is not the receiver port on the Arduino and therefore, I don't think serialEvent is the solution I'm looking for but that's maybe because I don't fully understand what this function is doing.
 
My apologies - I think I mis-directed everyone on this one.
 
So, just to re-iterate, the receiver I mention above is the one that sits inside a radio controlled model and outputs signals to run servos on things like ailerons and elevators or a throttle to an engine. It receives its data over the radio waves via 2.4Ghz spread spectrum or the old 72MHz FM systems. The receiver I reference above is fairly unique in that it has the ability to output digital logic levels as well as the RC system PWM signal that is used to drive RC servos (20ms period, 1.0ms to 2.0ms pulse width). I can configure it to output a digital high or low based on the position of a switch on my radio transmitter. This is the output I'm using to control the system and its the switch that can change at any time.
 
If however, you are suggesting I use the Arduino serial port for this logic input, then I'm definitely over my head on this one. Do you have any further links that could help me through this if this was in fact where you were headed with your suggestion? As I said, I am a neophyte!
 
Regarding the beeper, yes, I am looking at using the millis() values as the base for the timer and am working through that now - appreciate the guidance on this one.
 
I'll look into RepRap
 
Tony
« Last Edit: November 17, 2014, 03:42:37 PM by Soarman »

Iggy

  • FamiLAB Member
  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Arduino Sketch help
« Reply #3 on: November 17, 2014, 05:43:21 PM »
Tony,

  Ah! My mistake, that's what I get for reading too quickly. You will still use an interrupt but you'll have to research the best option. If I understand correctly, you only care about the state of txSw (no data) in which case you could probably implement the pin change interrupt example from the link above.

  The basic idea of using millis() for delay is to short circuit your main loop until X time has passed. This is different from using delay() which tells the processor to go to sleep for a rough number of clock cycles. I think you should do something different. Like this:

bool beeperOn = false;
int beeperCount = 0;
int beeperMax = 30; // This number represents the number of loops the beeper will sound once it is turned on.

main()
{
  if (beeperOn) {
   beeperCount += 1;
    if (beeperCount > beeperMax) {
      beeperOn = false;
      // Turn the beeper off.
    }
  }
  else {
    if (pon0 == 0 && txSw == 1) {
      beeperOn = true;
      beeperCount = 0;
      // Turn the beeper on.
    }
  }
}

You could use millis() and an interval instead of a running count but the result is the same.

Soarman

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Arduino Sketch help
« Reply #4 on: November 17, 2014, 09:47:02 PM »
Iggy, thanks for the update.
 
The link to the interrupt is where I'll dig next. I appreciate you including that link.
 
I'm screwing around right now with the timer, I'll ping back when I feel I have something that works for me. Again, thanks for taking the time to give guidance.
 
Tony

Soarman

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Arduino Sketch help
« Reply #5 on: December 02, 2014, 06:54:14 PM »
So, after being on the road for the last week, I finally got the solution for the controller. There is a timer routine using the millis() function that is in the general domain but I could not get it to work properly. In order to get it to work I had to initialize one of the variables (previousMillis) every time the timer was initiated, something that wasn't in the original code. After solving that, everything is now working as planned. The interrupts are working and all is well. Thanks for the assistance Iggy
Tony