Show Posts
Pages: 1 ... 78 79 [80] 81 82 ... 285
1186  Using Arduino / Programming Questions / Re: How do I add 2 buttons with example sketch? on: March 03, 2014, 01:02:02 pm
No.  Like this
Code:
byte buttonState1 = 0;         
const byte switch1Pin = 7;       
const byte buzzerPin = 13;

void setup()
{
  pinMode(buzzerPin, OUTPUT);     
  pinMode(switch1Pin, INPUT_PULLUP);   
}

void loop()
{
  buttonState1 = digitalRead(switch1Pin);

  if (buttonState1 == LOW)
  {         
    buzz(5);         
  }
}

void buzz(int numberOfTimesToBuzz)
{
  for (int i = 0; i < numberOfTimesToBuzz; i++)
  {
    digitalWrite(buzzerPin, HIGH);
    delay(300);               
    digitalWrite(buzzerPin, LOW);
    delay(300);     
  }
}
1187  Using Arduino / Programming Questions / Re: Functions returning data vs changing global variable. on: March 03, 2014, 12:26:39 pm
As others have said there are pros and cons of each method but functions that return a value can make code much easier to read.  Take the case of a function that determines which of 2 integers is largest and returns it to the calling program.  To call this function you would use something like
Code:
int largestNumber = findLargestNumber(x, y);
It is then obvious which variable holds the result. 
If you use a global variable the function call would be
Code:
findLargestNumber(x, y);
Now which variable is the largest number in ?
1188  Using Arduino / Programming Questions / Re: Help with a simple code on: March 03, 2014, 12:04:59 pm
The first thing to do is to stop using pin 1 as your input because it is used by the serial port.  This will prevent you printing any debugging information if/when you need to.

You need to refine what you mean by
Quote
but if it detects a button is pressed
Do you mean when the button is held down or do you you mean when the button is first pressed ?  If the latter, then look at the StateChangeDetection example in the IDE.  Basically you remember the previous state of the button and only act if has changed.

As to turning the buzzer on/off a number of times, look at for loops in the Arduino reference.  They allow you to do repetitive things a number of times without repeating code.  Putting the buzzer code in a function would be a good exercise, particularly if you pass it the number of times to sound the buzzer.

Have you got a pull up resistor in your button circuit ?
If not then use
Code:
  pinMode(switch1, INPUT_PULLUP);
so that the button pin is in a known state at all times.
1189  Using Arduino / Programming Questions / Re: Timer functions with variable duty cycle on: March 03, 2014, 11:27:40 am
I love that cool smiley in your code.

If only there were a way to post code here without it being mangled.  Perhaps there could be special tags to use before and after code to prevent the mangling and present the code in a scrolling window.  They could be called code tags and it would be useful if they could be inserted in the message editor by clicking on a button with a special symbol such as #.  Of course, users would need advice on how to post code properly and that advice would need to be in a sticky post like http://forum.arduino.cc/index.php?topic=97455.0

If only .......
1190  Using Arduino / Programming Questions / Re: Pausing a void-function with timer interrupt ? on: March 03, 2014, 11:08:14 am
Quote
Yes, i know. One variable is enough. But in one code I need different quantities of integers in both arrays.
Can you give an example of such a for loop ?

You cannot pause execution of code.  You can either do nothing for a while, using delay(), or check frequently whether it is time to do the next thing using millis() and in the meantime do something else.  BlinkWithoutDelay is an example of the latter. 

Code:
set timeLastMoved to millis()
start of loop
  if millis() -  timeLastMoved  >= interval
    move the motor a little
    save millis() as timeLastMoved
  end of if
  read input and react if required
end of loop

 
1191  Using Arduino / Programming Questions / Re: Pausing a void-function with timer interrupt ? on: March 03, 2014, 08:52:51 am
Code:
for (i = 0, j = 0; i <= 225, j <= 225; i++, j++){
   
   stepper2.runToNewPosition(*(pB + j));
   stepper1.runToNewPosition(*(pA + i));
  // **
  }
Nothing to do with your problem but I am intrigued as to why you have 2 variables in this for loop.
1192  Using Arduino / Programming Questions / Re: steering of a drawbar on: March 03, 2014, 07:31:58 am
Sorry, but that is obviously not the code.  Did you mess up copy/paste and why did you not put the code in code tags as requested in the stickies of this forum ?
1193  Using Arduino / Programming Questions / Re: Serial Commands on: March 03, 2014, 07:28:52 am
Code:
void loop()
{
  str1[1] = Serial.read();
  Serial.print(str1[1]);
  delay(2000);
}
This code does not wait until a byte is available but even when one is available it is always put into the second position in the str1 array then printed.

This page explains how to receive multiple bytes and put them into a null terminated array of chars (ie a C string).  Once the string has been constructed you can act upon what it holds to take whatever actions you want.

 http://gammon.com.au/serial
1194  Using Arduino / Programming Questions / Re: Is CharAt() the best method for me on: March 03, 2014, 02:25:12 am
Quote
Does this seem like a reasonable (and efficient) way to look at this desired behavior??  I have been staring at the Arduino reference site for quite a while.
Only if the data really is a string or String;  A more efficient and compact method would be to use the bits of an integer like this.
Code:
long testData = 0b10100011100011100011100011100011;

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

  for (int bitCount = 31; bitCount >= 0; bitCount--)
  {
    byte thisBit = bitRead(testData, bitCount);
    if (thisBit == 0)
    {
      zero();
    }
    else
    {
      one();
    }
  }
  Serial.println();
}

void zero()
{
  Serial.print(0);
}

void one()
{
  Serial.print(1);
}

void loop()
{
}
Obviously the program is very artificial as it stands but it demonstrates the principles involved and the operation of bitRead() is very obvious.
1195  Using Arduino / Programming Questions / Re: Arduino School Project on: March 03, 2014, 01:57:39 am
Code:
[code}  else{                //if there was no hole in the card, add a 0
    code += 0;
  }
Why bother ?
1196  Using Arduino / Programming Questions / Re: Determining when the hour has rolled over using Time.h library on: March 02, 2014, 11:06:03 am
Why is the delay() there in the first place ?
1197  Using Arduino / Programming Questions / Re: SOLVED: Storing Serial Characters in an Array on: March 02, 2014, 10:59:29 am
Quote
So, I was having a buffer overflow or something. That's fine, I'll just find a way to separate the data I need to send into two sets of 32 chars or something. Like so: (I also learned about the serialEvent routine)
If it was overflowing the buffer before you read even one byte then there must be quite a lot going on in your program because serial input is very slow compared to how fast Serial.read() can take each byte out of the buffer. 

If you are going to the bother of sending and reading two 32 byte sequences then you may as well do it properly and put start and end bytes on the data and stop messing around counting incoming bytes.  Just read when at least one byte is available, check that it is the start of message byte and carry on reading until you reach the end of message byte.

As to serialEvent, I could never see the point of it.  Sure, it runs automatically at the end of the loop() function if serial data is available but you can do that yourself and anyway, the end of the loop() function may not be the most appropriate place to check for serial data.
1198  Using Arduino / Programming Questions / Re: Running two different programs on arduino uno on: March 02, 2014, 10:39:51 am
Quote
im a bit confused about the function to used for 'when'. Can i get a small example?
What I had in mind was that you probably want to know when the button becomes pressed, not if it is continuously pressed.  If so, what you do is to to save the state of the button in a variable and compare it with the value the next time that you read the button. 

If it was HIGH last time and is now LOW then you know that it has just been pressed (assuming a pull up resistor is used).  If it was LOW last time and is still LOW then you know that it has remained pressed since the last check.  In which case you may not want to run the function again, but could if  you wanted to, of course.
1199  Using Arduino / Programming Questions / Re: Order in which two inputs change state on: March 02, 2014, 03:20:48 am
When each input changes state record the value of millis() or micros() in a variable.  When both have gone HIGH compare the 2 variables.  The input with the lowest value in its variable changed state first.

The code should be easy.  Where are you stuck, what have you tried ?
1200  Using Arduino / Programming Questions / Re: Storing Serial Characters in an Array on: March 02, 2014, 03:15:48 am
Quote
If 64 characters were sent through the serial port, shouldn't that little snippet set imageArray to them?
No.  The while loop will start when the first byte is available but there is no guarantee that there are any more bytes to be read.  Try changing the Serial.available() test to check that there are 64 characters to read.
Pages: 1 ... 78 79 [80] 81 82 ... 285