Pages: [1]   Go Down
Author Topic: Programming Help  (Read 650 times)
0 Members and 1 Guest are viewing this topic.
Boston, MA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a simple home made Photobooth project that I'm working on and want to add some simple control logic via an Arduino. I think it's fairly simple and someone could probably create a code for it in a few minutes. If anyone is willing to help me out, please let me know.
It involves 2 buttons and 3 LED's. Below is a link to the schematic and logic flow diagram. I would like the Arduino to signal the connected PC with an 'Enter' or 'Right Mouse Click', but I'm not sure how difficult that is. If easier, I'll just wire a relay to an output and solder connections to hardwired mouse.

http://img26.imageshack.us/img26/50/photobootharduinoprogra.jpg
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like the Arduino to signal the connected PC with an 'Enter' or 'Right Mouse Click', but I'm not sure how difficult that is.
'Enter' is a keyboard function. 'Right Mouse Click' is a mouse function. What application on the PC has focus when these events happen? There may be other (better) ways.

Which Arduino are you using/planning? A Leonardo can send keystrokes or mouse movement data to the PC via the USB cable.

What does "Flash Xxx LED N/Sec" mean? The Xxxx is clearly which LED, but does N/Sec mean n times per second or on for N seconds/off for N seconds? How many times?
Logged

Boston, MA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS, thanks for the response.
The PC (Win7, 32bit) will be running a Photobooth application and the process can be started with either a Enter or Right Click. The Arduino would be connected via USB.

I have an Uno that I'm trying to use for this application.

I'm tying to get the LED's to flash fast (2 per second) or slow (1 per second) depending on the current condition.

Any advice is appreciated.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd suggest that you remove the external resistors in the switch circuits. Connect one leg of the switch to ground and the other leg to a digital pin.

Then, use digitalWrite(pin, HIGH) after pinMode(pin, INPUT) to set the pin to INPUT and enable the internal pullup resistor.

Reading the switch states is then quite easy.
Code:
int startState = digitalRead(startPin);
int printState = digitalRead(printPin);

The value of startState will be HIGH if the start switch is not pressed, and LOW if it is.

You may need (it's hard to tell from your flowchart) to act only if the current state is not the same as the previous state, which will require you to keep track of the previous states of both switches.

Testing the value of counter, and incrementing it is trivial.

I still don't understand the LED situation. LEDs don't blink. They are either on or off. The Arduino can turn them on and off at predictable times, making the LEDs appear to blink. What isn't clear, though, is how long that blinking should occur (or how many times) or how often the state should change.

Anyway, the blink without delay example shows how to toggle an LED pin at appropriate times to make it blink. Setting the start time and interval is up to you. Making the pin stop toggling is a matter of setting the start time to 0 (and making sure that the start time is not 0 when deciding whether to turn the LED on).

The only challenging part of your project is sending the Enter to the PC. Sending Enter to the serial port is trivial, with any Arduino. Getting the application with focus to read the serial port is not.

GoBetwino might be useful, if the PC is running Windows. It might be able to make the Photobooth application act as though an enter was pressed.

Using a Leonardo or Micro with Keyboard support would make the task trivial.
Logged

Boston, MA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul, thanks again.
Your logic all makes sense, I just don't know how to implement it. The only programming I ever did was Basic in High School.

Here is my sketch, please don't laugh.
I think I need a debounce for the button,  as it appears to get the push intermittently, I entered some code and I couldn't seem to get it to work. So I commented it out.
The timing seems to be irregular, not sure if I'm doing something wrong or if it just not going to be that accurate. The 20000 seems to be 5-8 seconds.   

Code:
int StartPin = 1;
int StartledPin = 2;
int PrintPin = 3;
int PrintledPin = 4;
int StartState = LOW;
int PrintCount = 0;
int MaxPrint = 10;
int LastButton = HIGH;
int StartledBlink = LOW;
int PrintledBlink = LOW;
long previousMillis = 0;
long SpreviousMillis = 0;
long TpreviousMillis = 0;
long interval = 1000; //LED Start Flash Timer
long interval2 = 500; //LED Print Flash Timer
long timerINT = 20000; //Time for Photo Capturing (60000 = 1Min)

int counter = 0;       // how many times we have seen new value
int reading;           // the current value read from the input pin
int current_state = LOW;    // the debounced input value
long time = 0;         // the last time the output pin was sampled
int debounce_count = 5; // number of millis/samples to consider before declaring a debounced input


void setup ()
{
  pinMode (StartPin, INPUT);
  digitalWrite (StartPin, HIGH);
  pinMode (StartledPin, OUTPUT);
  pinMode (PrintPin, INPUT);
  pinMode (PrintledPin, OUTPUT);
}

void loop ()
/*{
  // If we have gone on to the next millisecond
  if(millis() != time)
  {
    reading = digitalRead(StartPin);

    if(reading == current_state && counter > 0)
    {
      counter--;
    }
    if(reading != current_state)
    {
       counter++;
    }
    // If the Input has shown the same value for long enough let's switch it
    if(counter >= debounce_count)
    {
      StartState = HIGH;
      counter = 0;
      current_state = reading;
      digitalWrite(StartledPin, !current_state);
    }
    time = millis();
  }
*/
{    // Wait for Start button
  if (digitalRead(StartPin) == LOW && LastButton == HIGH)
  {
    StartState = HIGH;
    PrintCount = PrintCount + 1;
  }
  else
  {
    LastButton = digitalRead(StartPin);
  }
 
  // Start LED Blink
  if (StartState == HIGH)
  {
    unsigned long currentMillis = millis();
     if (currentMillis - previousMillis > interval)
     {
      previousMillis = currentMillis;
   
      if (StartledBlink == LOW)
       StartledBlink = HIGH;
      else
       StartledBlink = LOW;
       digitalWrite(StartledPin, StartledBlink);
     }
  }
  // Stop after Photo Capture Time
  if (StartState == HIGH)
  {
    unsigned long timerMillis = millis();
      if (timerMillis - TpreviousMillis > timerINT)
      {
      TpreviousMillis = timerMillis;
      StartState = LOW;
      }
  }
/*  //Stop after Print Count Exceeded
  if (PrintCount > MaxPrint)
  {
   unsigned long timer2Millis = millis();
     if (timer2Millis - SpreviousMillis > interval2)
     {
      SpreviousMillis = timer2Millis;
   
      if (PrintledBlink == LOW)
       PrintledBlink = HIGH;
      else
       PrintledBlink = LOW;
       digitalWrite(PrintledPin, PrintledBlink);
     }
  }
   if (digitalRead(PrintPin) == LOW && LastButton == HIGH)
  {
    StartState = LOW;
    PrintCount = 0;
  }
  else
  {
    LastButton = digitalRead(PrintPin);
  }
*/
}
Logged

Pages: [1]   Go Up
Jump to: