Go Down

Topic: Stepper Moves, IR Flashes, Repeat... How? (Read 1 time) previous topic - next topic

PaulS

Quote
One thing though. Try using pin 3 for the button instead of pin 2 as pin 2 is the main serial port.

No, it isn't. Pins 0 and 1 are.

OP: You have Serial.begin() and Serial.println() in setup(), but no Serial.print() or Serial.println() statements in loop. Why not? Add a Serial.println() statement to tell you when the switch is pressed. (Also tell us how the switch is wired). Add Serial.print() and Serial.println() statements to output what CountVariable and NumberOfPictures are. (Work on that name, too. Of course CountVariable is a variable. That part of the name tells you nothing. Of course, CountVariable is being used to count something. But, there is no clue as to what is being counted. A name like NumberOfPhotosTaken would be better, in my opinion. It's quite clear what that is counting.)

IanMcRV

#11
Feb 10, 2013, 05:51 pm Last Edit: Feb 10, 2013, 05:54 pm by IanMcRV Reason: 1
Oh yeah  :smiley-red:

Absolutely right PaulS.

Lets get the basics up and running first, here's a sketch that I did actually try out on my Arduino :)
Code: [Select]
int ledPin =  13;    // LED connected to digital pin 13 You will need to change this.
int NumberOfPictures = 5;  // How many pictures should we take?
int NumberOfPicturesTaken = 5;  // I'll use this to keep a tab on how many pictures we've taken so far. I've set this to 5 already so nothings going to happen until its reset to 0
int ButtonPin = 3; // you'll need to change this.

void setup()   {
 Serial.begin(9600);           // set up Serial library at 9600 bps
 Serial.println("Start!");  
 pinMode(ledPin, OUTPUT);
 pinMode(ButtonPin, INPUT_PULLUP);
}

void loop() {
 if (digitalRead(ButtonPin) == LOW) NumberOfPicturesTaken = 0; // the magic line, if the button is pressed then the count is set to 0 so it all begins.
 if (NumberOfPicturesTaken >= NumberOfPictures) return; // Stop now as NumberOfPicturesTaken indicates that we have taken the correct NumberOfPictures
 
 NumberOfPicturesTaken ++;  
 Serial.print("NOPT = ");
 Serial.println(NumberOfPicturesTaken);
 
 digitalWrite(ledPin, HIGH);
 delay(500);
 digitalWrite(ledPin, LOW);
 delay(500);
}  // at this point it will go to the top of the loop at the void loop() line and start again.

// everything below this is nicely hidden away :)

IntelliTom

HI Ian

Sorry for the delay in replying, life has a habit of getting in the way.

I have loaded your code onto the arduino.  The serial monitor prints 'Start!' then cycles 'NOPT = 1' endlessly.  If I press the button and hold it, the Serial monitor prints 'NOPT = 1' then 2, 3, 4 & 5 and then stops.  When I release the button it continues to cycle NOPT = 1 endlessly

It's late here and I'm not firing on all cylinders, what was the goal of this code?  Just to show the button press?  It seems to be working in reverse of what I need.  The code need to wait for a single press of the button and then cycle through the specified number of pictures/routines.

I'll spend some more time on this tomorrow.  Thanks for taking the time to help

IanMcRV

Hi Tom, that's weird.

Can you post your code please.

(worked on mine).


PeterH


I have loaded your code onto the arduino.  The serial monitor prints 'Start!' then cycles 'NOPT = 1' endlessly.  If I press the button and hold it, the Serial monitor prints 'NOPT = 1' then 2, 3, 4 & 5 and then stops.  When I release the button it continues to cycle NOPT = 1 endlessly


This code is intended to reset the count when the button input state is LOW.
Code: [Select]

if (digitalRead(ButtonPin) == LOW) NumberOfPicturesTaken = 0; // the magic line, if the button is pressed then the count is set to 0 so it all begins.


It seems that your input is wired differently, so that the input is normally LOW and goes HIGH when you press the button. Given that the software enables the internal pullup resistor, you must have overridden this with another external pull-down resistor. You can either rewire your switch so it reads HIGH in the normal position and LOW when it is pressed, or change the LOW to HIGH in the code above.

(Changing the code would be easier, but IMO it would be better to lose the external pull-down resistor and change the switch wiring so that the switch connects the pin to ground when it is closed.)
I only provide help via the forum - please do not contact me for private consultancy.

Go Up