Show Posts
Pages: 1 ... 23 24 [25] 26 27 ... 231
361  Using Arduino / Programming Questions / Re: How do I add 2 buttons with example sketch? on: March 04, 2014, 04:43:36 pm
Try Auto Format on the code.  It will give you a clue.  The error is not actually on the line that compiling the code reports.

Putting each curly brace on its own line would help prevent the mistake that you have made.
362  Using Arduino / Programming Questions / Re: how to latch a value on: March 04, 2014, 04:38:02 pm
Look at the code you posted in reply #10.
Can you read it ?

Why don't you use code tags as requested in the stickies of this forum ?
363  Using Arduino / Programming Questions / Re: Using different control structures in main loop on: March 04, 2014, 03:40:38 pm
Code:
  delay(5000); 
  buttonState = digitalRead(buttonPin);
The delay() prevents the button pin being read for 5 seconds.  As a result you have to be pressing the button at just the right time for it to be noticed.  How long are you holding the button down and do you really want/need that delay() ?
364  Using Arduino / Programming Questions / Re: how to latch a value on: March 04, 2014, 11:19:36 am
Code:
int octave=5 ;
Sets octave to 5 each time through the loop() function.  Either make octave a global variable or declare it as a static local variable.
365  Using Arduino / Programming Questions / Re: Pausing a void-function with timer interrupt ? on: March 04, 2014, 11:07:29 am
Thank you for your answers.

Your suggestions are very helpful in another code of mine.


Okay, I have another question to the main issue:
Maybe I am slow on the uptake at the moment but when I use your suggestion:

Quote
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

How can I save the value of my pointers for the next loop iteration to continue at the same point so as to avoid a new counting from beginning ? Or should I count how much steps the steppers do in the interval, write new arrays like A1[], A2[]..and B1[], B2[], for each interval...and let the pointers for example once iterate through the arrays but then I need two new array pointers, but this is very inconvenient, isn't it? 
Do one small motor move for each elapsed interval keeping a count of how many moves have been made.  Then, if the movement is interrupted you will know where you left off and can resume from the same place.
366  Using Arduino / Programming Questions / Re: storing array for randomseed on: March 04, 2014, 03:52:37 am
Try this
Code:
char numbuf[9];                 //set array to having 8 + 1 digit

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

void loop()
{
}

void password()
{
  {
    Serial.println("Generating Password");
    randomSeed(analogRead(0));
    int randNumber;

    for(int i=0; i<=7; i++)
    {
      randNumber = random(10);
      Serial.println(randNumber);
      numbuf[i] = (char)(48 + randNumber);
    }
    numbuf[8] = '\0';
    Serial.println(numbuf);
  }
}
367  Using Arduino / Programming Questions / Re: Pausing a void-function with timer interrupt ? on: March 04, 2014, 03:18:15 am
It seems like you want i to go from 0 to 230 in the same time as j goes from 0 to 225.  Is that right ?If so you can do something like this.
Code:
void setup()
{
  Serial.begin(115200);
  float conversionFactor = 225.0 / 230.0;
  for (int i = 0; i <= 230; i++)
  {
    Serial.print( i );
    Serial.print("\t");
    int j = conversionFactor * i;
    Serial.println( j );
  }
}

void loop()
{
}
Note, however, that both i and j cannot both increment in steps of one for obvious reasons.
368  Using Arduino / Programming Questions / Re: got compile error on input_t on: March 04, 2014, 02:36:59 am
For those of us without the libraries to test the code could you please give details of the error ?
369  Using Arduino / Programming Questions / Re: Ubuntu; Arduino serial port not found on: March 03, 2014, 05:22:06 pm
Well, did you choose the right serial port ?
370  Using Arduino / Programming Questions / Re: Pausing a void-function with timer interrupt ? on: March 03, 2014, 05:15:55 pm
Code:
  for (i = 0, j = 0; i <= 230, j <= 225; i++, j++)
That for loop will stop when the value of j reaches 225 and in the meantime i and j will have the same value.  i will never reach 230.
371  Using Arduino / Programming Questions / Re: Arduino UNO Controller speed on: March 03, 2014, 01:49:14 pm
Just how hard did you search ?
http://arduino.cc/en/Main/ArduinoBoardUno#.UxTQquN_s1I
372  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);     
  }
}
373  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 ?
374  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.
375  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 .......
Pages: 1 ... 23 24 [25] 26 27 ... 231