Show Posts
Pages: 1 ... 8 9 [10] 11 12 ... 236
136  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.
137  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++;
  }
}
138  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
139  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.
140  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;
}
141  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.
142  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 ?
143  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.
144  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; 
  }
}
145  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.
146  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.
147  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 ?

148  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.
149  Using Arduino / Programming Questions / Re: Buttoning Up this button code: Increment Buttons with Interrupt Service Routine on: April 02, 2014, 02:20:34 am
Quote
That’s why I couldn't use Blink Without Delay option. The on millis are the same as the off millis.
There is no reason why you cannot use the BWD technique with asymmetrical on/off periods.
Code:
const int ledPin = 13;
int ledState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 1000;

void setup()
{
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;   
    ledState = !ledState;
    digitalWrite(ledPin, ledState);

    if (interval == 1000)
    {
      interval = 100;
    }
    else
    {
      interval = 1000;
    }
  }
}


As to the error in your current code,  you have inserted the debounce function in the wrong place
Code:
void setup()
boolean debounce (boolean last)
Where is the { which starts the setup() function code ?
150  Using Arduino / Programming Questions / Re: Timing... So Confused... on: April 01, 2014, 12:33:03 pm
The code I suggested in reply #11 turns an LED on for a tenth of a second then off for one second.  That sounds very close to what you want to do, albeit with longer/different intervals.  Did you try the code ?
Pages: 1 ... 8 9 [10] 11 12 ... 236