Show Posts
Pages: 1 ... 15 16 [17] 18 19 ... 196
241  Using Arduino / Programming Questions / Re: Problem debouncing multiple buttons on: February 27, 2014, 02:55:32 pm
Those delay even though they are small, they will slow the code down incorrectly. Look into the Blink without Delay sketch to replace those delays.

As for the debounce code.
Code:
/*
 Debounce
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const byte MAX_PINS = 4; // how many buttons to look for
const byte buttonPin[MAX_PINS] = {A0,A1,A2,A3}; // Number of buttons
const byte ledPins[MAX_PINS] = {13,12,11,10}; // the number of the LEDs
const long debounceDelay = 50; // the debounce time; increase if the output flickers

byte counter=0;
boolean currentState[MAX_PINS]; // Initial storage for the state of the button
boolean buttonState[MAX_PINS]; // the current state from the input pin
boolean lastButtonState[MAX_PINS] = {LOW,LOW,LOW,LOW}; // the previous state from the input pin
long lastDebounceTime[MAX_PINS] = {0,0,0,0}; // the last time the output pin was toggled

void setup()
{
  for(byte i = 0; i <= MAX_PINS; i++)
  {
    pinMode(buttonPin[i], INPUT); //set button states
    pinMode(ledPins[i], OUTPUT); // set LED states
  }
}

void loop()
{
  counter < MAX_PINS? counter++ : counter = 0;

  currentState[counter] = digitalRead(buttonPin[counter]);// look at button input and record it.
 
  if (currentState[counter] != lastButtonState[counter]) // Check to see if the state has changed, indicating button is not held
    lastDebounceTime[counter] = millis();

  if ((millis() - lastDebounceTime[counter]) > debounceDelay) // check debounce time
    buttonState[counter] = currentState[counter];
 
  digitalWrite(ledPins[counter], buttonState[counter]); // light specific LED
 
  lastButtonState[counter] = currentState[counter]; // save current state to last state
  //Serial.print("counter: ");
  //Serial.print(counter);
  //Serial.println();

}
This can be made into a function like how you have yours, then all you need to return is "buttonState[counter]"
242  Using Arduino / Project Guidance / Re: 2.4" TouchScreen Shield in Arduinouno on: February 27, 2014, 11:43:41 am
What library does it use and see what available functions it has already. I'm sure it has some example sketches that came with it, like a demo of its features.
243  Using Arduino / Programming Questions / Re: Help with Science Fair Project Code. on: February 27, 2014, 03:11:26 am
You are aware that the only time you get measurement is when your not sending anything over Bluetooth. Make the "ping" section a function that you can call when you need it, instead of how you have it now.

Also if your currently using the serial monitor to test the code, then why are you checking if the Bluetooth is sending something when your reading from the serial monitor?
Quote
if (BTSerial.available() > 0) // Check and see if there is serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
{ <= unneeded bracket, get rid of it and its closing counterpart.
244  Using Arduino / Project Guidance / Re: Control DC motors using Arduino on: February 27, 2014, 01:03:27 am
Quote
I need help to control the DC speed motors speeds

Easy enough, the analogWrite function is what you need to use. What are you going to use as a motor controller? Make sure you take into consideration the power to weight ratio. The sum of the combined weights must be less than what the motors can provide, otherwise it will not get off the ground.

Quote
make my quadcopter hover at a certain position above the earth.
This will require a GPS module.
245  Using Arduino / Programming Questions / Re: Problem debouncing multiple buttons on: February 27, 2014, 12:57:23 am
You have to use arrays to hold everything, button pins, last states and previous times. Have a counter based on the number of buttons you have ie 2. Use an IF statement to increment the counter if it is less then the number of buttons, and reset counter to 0 if it goes over the number of buttons.

You can use the same debounce sketch you have for the one button, but now make everything arrays.
246  Using Arduino / Project Guidance / Re: Robot arm problems with joysticks and base on: February 26, 2014, 10:11:02 pm
I understand the sweep example very well, but the way your doing it is incorrect. You want to move one way and stop where you want it, then if you go the other way it move accordingly. What you need to do is record the last position then take the difference of new position and the previous position.

Let me ask this, does your joystick or pot spring back to a fixed position or hold it's position when let go?
247  Using Arduino / Project Guidance / Re: Project to make my dog stop peeing on my bathroom on: February 26, 2014, 09:29:34 pm
You could maybe attach a small RFID tag on his collar, and if s/he enters the bathroom it triggers a loud noise to play or something. Just like what is used in stores to prevent shoplifters.

Im not sure where you would be able to get the components for something like that, I know normal RFID tags need to be within a certain range for them to be read.

Look into it.
248  Using Arduino / Project Guidance / Re: Robot arm problems with joysticks and base on: February 26, 2014, 09:18:44 pm
You dont need a for loop to do that, just tell it where to go and unless the value changes, the servo will stay the same.
249  Using Arduino / Project Guidance / Re: Robot arm problems with joysticks and base on: February 26, 2014, 07:41:21 pm
It should be
Quote
for (clawpos = 0; clawpos < 450; clawpos += 1);
for (clawpos = 0; clawpos > 750; clawpos -= 1);
Because you never increment or decrement onex in the for loops.

But upon closer inspection, onex is a reading from the analog input A0, so why are you using FOR loops at all and not mapping the value?

clawpos = map( onex, 0, 1023, 0, 180);
250  Using Arduino / Programming Questions / Re: While loop exit condition on: February 26, 2014, 04:23:58 am
Check your brackets, your not reading the button in the while loop.
251  Using Arduino / Programming Questions / Re: While loop exit condition on: February 26, 2014, 03:58:39 am
You need to read the button again, this time in the while loop.
252  Using Arduino / Programming Questions / Re: Need help with PWM Motor on: February 26, 2014, 01:38:48 am
Actually, if the motor is to be on forever when the button is pressed, then all you need is.

if(digitalRead(2) )  analogWrite(motor pin, 200); //no brackets needed for one line of code.
The motor will now always be on until commanded otherwise.
253  Using Arduino / Programming Questions / Re: Need help with PWM Motor on: February 26, 2014, 01:33:36 am
You need to add boolean latch = false; at the top with the rest of your pins. Another thing, if the motor is to stay on forever when the button is pressed, then why have an else statement that makes it turn off?

To answer your problem, you need another if statement that looks to see if latch is true, then if it is, motor goes on.
254  Using Arduino / Programming Questions / Re: Need help with PWM Motor on: February 26, 2014, 01:16:54 am
If button 2 is pressed and nothing else changes "latch", then yes, latch will always be true.
Quote
if(digitalRead(2) == HIGH)
{
  latch = true;
}
255  Using Arduino / Programming Questions / Re: Need help with PWM Motor on: February 26, 2014, 01:01:27 am
Yes, but will you be using that same button to then turn it off too?

To use the same button to turn it on/off, you need a latch. 

Example pseudo code:
Code:
boolean latch = LOW; // declaired at top of code

// inside the loop() or a function
if(digitalRead(2) == HIGH)
// you will need another variable to block this IF statement if the button is held down ie. Prev_state.
// Also debouncing may be needed, but you have an example code under Examples with the Arduino software
{
  latch = ! latch;
}

To do it with two different buttons.
Code:
if(digitalRead(2) == HIGH)
{
  latch = true; // digital/analogWrite( motor pin, set speed)
}
else if(digitalRead(3) == HIGH)
{
  latch = false; // digital/analogWrite( motor pin, 0 or LOW)
}
Pages: 1 ... 15 16 [17] 18 19 ... 196