Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 231
61  Using Arduino / Programming Questions / Re: High speed photogate readings and hardware interrupts (Help Needed) on: April 03, 2014, 10:48:00 am
Will you please post all of your code so that what you are doing can be seen in context of the data types and sensor reading techniques being used ?  Details, a link maybe, of the 'photogate' that you are using and how things are wired would be helpful too.
62  Using Arduino / Programming Questions / Re: LED fade out when button pressed on: April 03, 2014, 06:01:50 am
You presumably mean
Code:
if(digitalRead(buttonPin) == HIGH)
  {
    //fading code goes here
  }
63  Using Arduino / Programming Questions / Re: PLC pulse command equivalence on: April 03, 2014, 05:56:21 am
@UKHeliBob that was the previous version I did that to not the last one I posted
The message I quoted, including the code, is the last code you posted as far as I can see.
64  Using Arduino / Programming Questions / Re: stop loop on: April 03, 2014, 02:00:51 am
Or don't use a for loop at all
Code:
byte n = 0;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  while (n < 5)
  {
    Serial.println(n);
    n++;
  }
}
65  Using Arduino / Programming Questions / Re: Need help saving user input to read back at a later time (array?) on: April 03, 2014, 01:54:37 am
Agreed.  The end of sequence marker is not necessary, but I quite like the idea as it is a very understandable technique, and the OP will encounter C style strings where a similar marker is absolutely necessary
66  Using Arduino / Programming Questions / Re: PLC pulse command equivalence on: April 03, 2014, 01:34:12 am
@ UKHeliBob, thanks so much for your help. I did a quick read of case instructions and changed the code as follows:

Code:
const int photoPin = 2; //byte or int for digital input???
const byte buzzerPin = 12;
byte state = 0;
unsigned long buzzerStart = 0;
unsigned long waitFiveMinsStart = 0;
unsigned long buzzerPeriod = 1000UL;
unsigned long fiveMinutePeriod = 10000UL; //changed to 10sec for testing

void setup()
{
  Serial.begin(115200);// Delete?? Not required??
  pinMode(buzzerPin, OUTPUT);
  pinMode(photoPin, INPUT); // added to read photoPin 2
}

void loop()
{
switch (state)
{
 
  case 0:                              //do nothing until the beam is broken
    if (photoPin, HIGH);   //adjust value or change to digitalRead if appropriate
    {
      state = 1;
      digitalWrite(buzzerPin, HIGH);   //turn on the buzzer
      buzzerStart = millis(); 
    }
   break;

  case 1:                              //buzzer sounds for one second
    if (millis() - buzzerStart >= buzzerPeriod)
    {
      state = 2;
      digitalWrite(buzzerPin, LOW);    //turn off the buzzer
      waitFiveMinsStart = millis();
    }
   // break;

  case 2:                              //do nothing until five minutes has elapsed
    if (millis() -  waitFiveMinsStart >= fiveMinutePeriod)
    {
      state = 0;
    }
   // break; 
  }
}

It doesn't work properly I have added comments, but this looks like I am not far from achieving my goal.
Why did you comment out the breaks ?  They are important to stop the case statement falling through to the next case.
67  Using Arduino / Programming Questions / Re: What's the best button debounce method for this code? on: April 03, 2014, 01:29:51 am
Your code with my comments on problems
Code:
boolean  lastButtonOn  = LOW;
boolean  currentButtonOn = LOW;
boolean  lastButtonOff = LOW;
boolean  currentButtonOff = LOW;
volatile int  buttonPinOn = 0;          //arduino 2   // the pin that the ON pushbutton is attached to
volatile int  buttonPinOff = 1;         //arduino 3   // the pin that the OFF pushbutton is attached to


void setup()              //start of the setup() function
                          //where is the code for setup() ?
boolean debounce1(boolean last)  //start of the debounce1() function
{
  boolean current = digitalRead(buttonPinOn);
  if (last != current);    //semi-colons on the end of if stops them working
}                          //is this really the end of the debounce1 function ?
{                          //what is this the start of ?
  delay(5);
  return current;
}
boolean debounce2(boolean last)
{
  boolean current = digitalRead(buttonPinOff);
  if (last != current);
}
{
  delay(5);
  return current;
}


{                              //this is the body of the setup() function
  pinMode(buttonPinOn, INPUT);
  pinMode(buttonPinOff, INPUT);

}

void loop(){
  currentButtonOn= debounce1(lastbuttonOn);   //wrong variable name
  currentButtonOff= debounce2(lastbuttonOff); //wrong variable name
  if (lastButtonOn==LOW&&currentButtonOn==HIGH);
  if (lastButtonOff==LOW&&currentButtonOff==HIGH);
  lastButtonOn= currentButtonOn;
  lastButtonOff= currentButtonOff;
}
Your code with amended so that it at least compiles
Code:
boolean  lastButtonOn  = LOW;
boolean  currentButtonOn = LOW;
boolean  lastButtonOff = LOW;
boolean  currentButtonOff = LOW;
volatile int  buttonPinOn = 0;          //arduino 2   // the pin that the ON pushbutton is attached to
volatile int  buttonPinOff = 1;         //arduino 3   // the pin that the OFF pushbutton is attached to

boolean debounce1(boolean last)  //start of the debounce1() function
{
  boolean current = digitalRead(buttonPinOn);
  if (last != current)   //removed semi colon
    //}                        //commented out
  {                          //start of code for if == true
    delay(5);
    return current;
  }                          //end of code for if == true
}                            //end of function

boolean debounce2(boolean last)
{
  boolean current = digitalRead(buttonPinOff);
  if (last != current)    //removed semi colon
    //}            //commented out
  {                //start of code for if == true
    delay(5);
    return current;
  }                //end of code for if == true
}                  //end of function

void setup()              //start of the setup() function
                          //now the code for setup()
{                              //this is the body of the setup() function
  pinMode(buttonPinOn, INPUT);
  pinMode(buttonPinOff, INPUT);
}

void loop(){
  currentButtonOn= debounce1(lastButtonOn);    //corrected variable name
  currentButtonOff= debounce2(lastButtonOff);  //corrected variable name
  if (lastButtonOn==LOW&&currentButtonOn==HIGH);
  if (lastButtonOff==LOW&&currentButtonOff==HIGH);
  lastButtonOn= currentButtonOn;
  lastButtonOff= currentButtonOff;
}
68  Using Arduino / Programming Questions / Re: Need help saving user input to read back at a later time (array?) on: April 02, 2014, 05:13:10 pm
Declare a byte array big enough to hold the total number of button presses expected and an index variable to point at the next array entry to be used.
Code:
byte buttonPresses[40];
byte arrayIndex = 0;
Each time a button is pressed save the button number to the next array entry and increment the index variable and put a marker in the array to indicate where the end of the sequence is.
Code:
if (digitalRead(button1) == LOW)
{
  buttonPresses[arrayIndex] = 1;
  arrayindex++;
  buttonPresses[arrayIndex] = 99;
}
Do the same for the other two buttons, storing 2 or 3 as appropriate.

When the fourth button is pressed you can replay the sequence of stored numbers
Code:
arrayIndex = 0;
while (buttonPresses[arrayIndex] != 99)
{
  Serial.println(buttonPresses[arrayIndex]);
  arrayIndex++
}
There is still work for you to do to check the stored numbers as they are read from the array and to do whatever you want depending on the number but I suggest that you start by simply printing the numbers as above.
69  Using Arduino / Programming Questions / Re: What's the best button debounce method for this code? on: April 02, 2014, 04:54:56 pm
I told you why you get the error in your other thread.

Code:
void setup()

boolean debounce1(boolean last)
Where does the code block for the setup() function start ?
70  Using Arduino / Programming Questions / Re: Serial Communications Problems on: April 02, 2014, 02:15:15 pm
Code:
Input = Serial.read();
In your code Input is a String whereas Serial.read() returns an int (just one)

So, even if the code above worked then
Code:
    if (Input == Password){
wouldn't

Look at http://gammon.com.au/serial to see how to read a string (not a String) from serial, character by character, then you can consider comparing it with another string.
71  Using Arduino / Programming Questions / Re: PLC pulse command equivalence on: April 02, 2014, 09:56:16 am
Quote
@ UKHeliBob, there is no input for the relay contacts to connect to.
You obviously have at least 1 pin free because you intend to use it for an interrupt, or is that not what you meant ?

Take a look at this.  It is not tested because I don't have the same hardware as you, whatever that is, so I have made some assumptions.
Code:
const byte photoPin = A0;
const byte buzzerPin = 8;
byte state = 0;
unsigned long buzzerStart = 0;
unsigned long waitFiveMinsStart = 0;
unsigned long buzzerPeriod = 1000UL;
unsigned long fiveMinutePeriod = 300000UL;

void setup()
{
  Serial.begin(115200);
  pinMode(buzzerPin, OUTPUT);
}

void loop()
{
  switch (state)
  {
  case 0:                              //do nothing until the beam is broken
    if (analogRead(photoPin) <= 512)   //adjust value or change to digitalRead if appropriate
    {
      state = 1;
      digitalWrite(buzzerPin, HIGH);   //turn on the buzzer
      buzzerStart = millis(); 
    }
    break;

  case 1:                              //buzzer sounds for one second
    if (millis() - buzzerStart >= buzzerPeriod)
    {
      state = 2;
      digitalWrite(buzzerPin, LOW);    //turn off the buzzer
      waitFiveMinsStart = millis();
    }
    break;

  case 2:                              //do nothing until five minutes has elapsed
    if (millis() -  waitFiveMinsStart >= fiveMinutePeriod)
    {
      state = 0;
    }
    break; 
  }
}
72  Using Arduino / Programming Questions / Re: Play a Melody using the tone() function (help) on: April 02, 2014, 08:29:06 am
Please help yourself by posting the code correctly in code tags.  Look at the code in your original post.  Does it really contain
Code:
noTone(smiley-cool;
As it stands nobody but you knows what your code really looks like.
73  Using Arduino / Programming Questions / Re: Timing... So Confused... on: April 02, 2014, 08:24:53 am
Code:
interval = (interval == 1000) ? 100 : 1000;
can be re-written
Code:
if (interval == 1000)
{
  interval = 100;
 }
else
{
  interval = 1000;
}
Which is how it was in my code.  The ternary operator version is faster to type but the longer version is easier to understand and I would hope that the compiler produces the same optimal code either way.
74  Using Arduino / Programming Questions / Re: PLC pulse command equivalence on: April 02, 2014, 03:03:02 am
Quote
that can't be done with the blink without delay.
Why not ?

75  Using Arduino / Programming Questions / Re: How to run code while a delay is in place on: April 02, 2014, 02:46:09 am
If you mean that you want to do things whilst doing something else for 5 minutes look at the BlinkWithoutDelay example in the IDE.  The technique that it uses allows you to check periodically whether the time is up and if not go and do something else.
Pages: 1 ... 3 4 [5] 6 7 ... 231