Digital Pin Commands

Hi All,

I'm looking to get an android app created that will instruct the Arduino to turn on a digital pin high or low with a duration - thats it. I have limited knowledge in C, is there a generic sketch out there that will do this? It doesn't seem too complex, I just need this last piece of the puzzle to get my project going.

Thank you in advance

The BlinkWithoutDelay example will do this.

How will the Android app communicate with the Arduino?

[quote author=James C4S link=topic=256331.msg1813282#msg1813282 date=1405987817] How will the Android app communicate with the Arduino? [/quote]

It will communicate via Ethernet. Tablet via wifi to router, router out ethernet port to Arduino Shield.

One option would be to use a web server example.

KeithRB: The BlinkWithoutDelay example will do this.

It looks like this will only do pin 13. I need something that will dynamically accept commands for all digital pins (except 0 and 1 of course)

eros:

KeithRB: The BlinkWithoutDelay example will do this.

It looks like this will only do pin 13. I need something that will dynamically accept commands for all digital pins (except 0 and 1 of course)

You can use variables in commands:

digitalWrite(whatEverPinTheAndroidTellsArduinoToUse, HIGH);

JimboZA:

eros:

KeithRB: The BlinkWithoutDelay example will do this.

It looks like this will only do pin 13. I need something that will dynamically accept commands for all digital pins (except 0 and 1 of course)

You can use variables in commands:

digitalWrite(whatEverPinTheAndroidTellsArduinoToUse, HIGH);

Thanks! Doesn't seem too difficult. How do I tell it a duration?

And can this multitask, say i set pin 3 to high for a duration of 10 minutes, and I sent another command from the Android to set pin 4 to high 3 minutes later...will pin 3's command be overwritten by pin 4's command?

And can this multitask, say i set pin 3 to high for a duration of 10 minutes, and I sent another command from the Android to set pin 4 to high 3 minutes later...will pin 3's command be overwritten by pin 4's command?

No it can't multitask in the strict sense of the word, but have a look at this video and this thread

JimboZA:

And can this multitask, say i set pin 3 to high for a duration of 10 minutes, and I sent another command from the Android to set pin 4 to high 3 minutes later…will pin 3’s command be overwritten by pin 4’s command?

No it can’t multitask in the strict sense of the word, but have a look at this video and this thread

Thanks, I was able to grasp some of the concepts but I’m still in the learning process of C so there are a few grey areas. Perhaps an explanation would be better. I am using the Android App as a door entry system and attendance logging. There will be a door strike to open the door, and 32 leds. I would like the app to send the necessary instructions to the Arduino to open the door once authentication has been verified (so pin 3 will be the door and set to HIGH for 5 seconds) and simultaneously light up a set of LED’s for 10 minutes, each set will be authentic per user.

So user A walks in and after authentication, pin 3 is set to high and the first 4 LEDS are turned on for 10 minutes. When user B walks in 3 minutes later, and is authenticated, pin 3 is set to high, and the first 8 LEDs are turned on restarting the 10 minute timer on the first 4 from the previous user. When user C comes in 8 minutes later, pin 3 set to high for the door for 5 sec, and the last 8 LEDs are turned on, retaining the first 8 pin’s duration, and the timer for the last 8 are on for 10 mins.

I guess I’m looking for something intelligent and dynamic to do some of the tasks mentioned above. Would this be a simple or difficult task?

I'd say this is a good opportunity to get your mind round the notion of the State Machine. Ignore the coding for a while and get the design clear: you seem pretty clear on what you want to do and converting that to an "official" state machine idea should not be hard.

The best descriptions that I have come across so far are Robert C Martin's, Grumpy Mike's and Nick Gammon's.

Don't cloud your thinking with code 8): get the design clear, and perhaps use pseudo-code at first. Then do the coding. I recently wrote a state machine sketch which apart from syntax errors :roll_eyes: worked almost as soon as I typed it, because I had the design very clear. I'll be posting that code soon once I tidy it up a bit.

JimboZA: I'd say this is a good opportunity to get your mind round the notion of the State Machine. Ignore the coding for a while and get the design clear: you seem pretty clear on what you want to do and converting that to an "official" state machine idea should not be hard.

The best descriptions that I have come across so far are Robert C Martin's, Grumpy Mike's and Nick Gammon's.

Don't cloud your thinking with code 8): get the design clear, and perhaps use pseudo-code at first. Then do the coding. I recently wrote a state machine sketch which apart from syntax errors :roll_eyes: worked almost as soon as I typed it, because I had the design very clear. I'll be posting that code soon once I tidy it up a bit.

I'm glad to know this idea had a name State Machine XD

I'll be interested to take a look at your code. I am in a bit of a time crunch on this project, I overestimated myself thinking I can learn C in a few days which wasn't the case. How long did it take you to write your state machine sketch? I'm curious because depending on the time frame I'm thinking I may resort to hire someone to do this while I catch up with the rest of you guys ;)

PM'd you a dropbox link to my code pro tem.

If you want to get someone to do it for you, might be an idea to post in the Gigs and Collaborations section of the forum.

http://forum.arduino.cc/index.php?board=26.0

JimboZA: PM'd you a dropbox link to my code pro tem.

If you want to get someone to do it for you, might be an idea to post in the Gigs and Collaborations section of the forum.

http://forum.arduino.cc/index.php?board=26.0

Got it, thanks for your help and insight, if anything you've advanced me a few days on the road to learning C :)

Key to understanding this whole State Machine thing, is realising that we look for changes in pins, not just their current setting.

It’s often not enough to know that (say) a switch is closed; it may be key to know that it just became closed. That’s why in code for these things you’ll see variables like lastSwitchReading for example. Each time through loop(), read the switch into say currentSwitchReading and compare the two. If they differ, it means they switch changed since you last looked. If it changed and it’s now open, do something (or nothing); if it changed and is now closed, do something else (or nothing). If it didn’t change, do nothing. Then update lastSwitchReading with currentSwitchReading, so next time round it’s comparing the new previous state.

Once you have that clear, it’s plain sailing…

So my code looks to see if the coin switch became pressed: that means a new coin was dropped. It’s no point looking to see if the coin switch is pressed: the milli seconds it’s held closed is a lifetime to Arduino so it would reflect multiple coins. Similarly it looks to see if the boom became closed (ie it had been open, now it’s closed), not just that it is closed.

Then my code you’ll see that I look for four scenarioes:

  • If it’s locked and a coin dropped, unlock it
  • If it’s not locked and a coin dropped, we scored a coin so say thanks
  • If it’s locked and someone passes, well that’s a forced entry so shout an alarm
  • If it’s unlocked and someone passes, that’s cool so say Hi and lock behind them

JimboZA: Key to understanding this whole State Machine thing, is realising that we look for changes in pins, not just their current setting.

It's often not enough to know that (say) a switch is closed; it may be key to know that it just became closed. That's why in code for these things you'll see variables like lastSwitchReading for example. Each time through loop(), read the switch into say currentSwitchReading and compare the two. If they differ, it means they switch changed since you last looked. If it changed and it's now open, do something (or nothing); if it changed and is now closed, do something else (or nothing). If it didn't change, do nothing. Then update lastSwitchReading with currentSwitchReading, so next time round it's comparing the new previous state.

Once you have that clear, it's plain sailing....

So my code looks to see if the coin switch became pressed: that means a new coin was dropped. It's no point looking to see if the coin switch is pressed: the milli seconds it's held closed is a lifetime to Arduino so it would reflect multiple coins. Similarly it looks to see if the boom became closed (ie it had been open, now it's closed), not just that it is closed.

Then my code you'll see that I look for four scenarioes:

  • If it's locked and a coin dropped, unlock it
  • If it's not locked and a coin dropped, we scored a coin so say thanks
  • If it's locked and someone passes, well that's a forced entry so shout an alarm
  • If it's unlocked and someone passes, that's cool so say Hi and lock behind them

I'm definitely starting to wrap my mind around the State Machine thing, I'm sure as I learn more it will start to soak in. I may give it a whirl and see if I can make a sketch. I may pick your brain again if I run into anything