Show Posts
Pages: 1 ... 80 81 [82] 83 84 ... 231
1216  Using Arduino / Project Guidance / Re: Double press button for on/off function with debounce on: December 11, 2013, 10:33:34 pm
Try this sketch.

Code:
byte ButtonPin = 2; //digital pin 2

int button = LOW;
int lastButtonState = LOW;
int type,lastType=0;
long time=0;
int flagged = 0;
long newT=0;
int output = 0, lastOut;
int cnt=0;

long lastDebounceTime = 0; 
long debounceDelay = 50;



void setup() {
  //pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  button = digitalRead(ButtonPin);
  if (button != lastButtonState) {
    lastDebounceTime = millis();
    lastButtonState = button;
    time = millis();
  }

  while( millis() - time < 1500){
    type = check(time);
  }

  if(type != lastType){
    lastType = type;
    //Serial.println(type);
    switch(type){
    case 0: 
      break;
    case 1:
      Serial.println("pressed 1 time(s)");
      break;
    case 2:
      Serial.println("pressed 2 time(s)");
      break;
    case 3:
      Serial.println("pressed 3 time(s)");
      break;
    }
  }
  type = 0;
}
 
int check(long time){
  newT=0;
  if ((millis() - lastDebounceTime) > debounceDelay) {  // debounce button
    newT=(millis() - time);

    if(newT < 500) // check if button is pressed within half a second
    {
      button = digitalRead(ButtonPin);

      if(button == HIGH)
      {
        if(!flagged) // latch + lockout  that prevents cnt to increase when button is held
        {
          cnt++;
          flagged = 1;
          output = cnt;
        }
      }
      else {
        flagged = 0;
      }
    }
    else {
      cnt=0;
      if(output != lastOut) // prevents continuous stream of values, and only when the value changes, display it.
      {
        lastOut = output;
        return output;//Serial.println(cnt);
      }
    }
  }




1217  Using Arduino / Programming Questions / Re: String variabe array too large on: December 11, 2013, 10:22:28 pm
The Arduino has issues with Strings, so we try to steer people away from using them, and get them into the habit of using char arrays instead.
1218  Using Arduino / Programming Questions / Re: Need some array help on: December 11, 2013, 10:16:40 pm
Your problem lies in your IF statement. if(temperaturearray[ i ] > hottest) Your storing the first element of the array into hottest "hottest = temperaturearray[0]; " and your telling your code to go through and see which is the highest value, but your not telling it what to do when the highest value is at temperaturearray[0]. So by changing "if(temperaturearray[ i ] > hottest)" to "if(temperaturearray[ i ] >= hottest)", it does what you want it to.

The following is how I tested your code.
Code:
float temperaturearray[]  = {480.0, 30.0, 500.0, 74.0, 80.0}; // 2
float temperaturearray1[]  = { 74.0, 80.0,480.0, 30.0, 500.0}; // 4
float temperaturearray2[]  = {500.0, 74.0, 80.0,480.0, 30.0}; // 0
/* greatest temperature stored in largest */
float hottest = 0;
int currentlargestindex = 0;
int previouslargestindex = 0;

void setup()
{
  Serial.begin(9600);
  highestTemperature(temperaturearray);
  highestTemperature(temperaturearray1);
  highestTemperature(temperaturearray2);
 
}

void loop() { }

float highestTemperature(float temperaturearray[])
{
  int arraylength = 5;
  hottest = temperaturearray[0];
  previouslargestindex = currentlargestindex;
  Serial.print("previous largest index = ");
  Serial.println(previouslargestindex);
 
  for(int i = 0; i < arraylength; i++) //loops through the array
  {
    if(temperaturearray[i] >= hottest) //looks for any numbers larger then the current largest
    {
     hottest = temperaturearray[i]; //if any numbers higher then current largest, it makes that vaule the new largest value
     currentlargestindex = i; //saves the index of the largest number for use in determining where to send the motor.
    }
  }
  Serial.print("current largest index = ");
  Serial.println(currentlargestindex);
}

Output:
Before.
if(temperaturearray[ i ] > hottest)
Quote
previous largest index = 0
current largest index = 2
previous largest index = 2
current largest index = 4
previous largest index = 4
current largest index = 4

After:
if(temperaturearray[ i ] >= hottest)
Quote
previous largest index = 0
current largest index = 2
previous largest index = 2
current largest index = 4
previous largest index = 4
current largest index = 0
1219  Using Arduino / Programming Questions / Re: Need some array help on: December 11, 2013, 09:28:52 pm
Post your full code please.
1220  Using Arduino / Programming Questions / Re: String variabe array too large on: December 11, 2013, 09:18:56 pm
Dont use Strings, use char pointer arrays.

Code:
char * data[5] = {
  "Hello ", "this ", "is ", "a ", "test."};

void setup()
{
  Serial.begin(9600);
  for(int i =0; i < 5; i++)
    Serial.print(data[i]);
}

void loop() { }
1221  Using Arduino / Programming Questions / Re: arduino to android on: December 11, 2013, 05:30:53 pm
If you can get your code to work with the serial monitor, then you can get it to work (most of the time) with an Android device.
Write a code that does exactly what you want and try to control it from the serial monitor.
1222  Using Arduino / Project Guidance / Re: Help with 4wd rc car on: December 11, 2013, 03:59:17 am
The jumper is the blue tab under the middle chip.
1223  Using Arduino / Project Guidance / Re: Help with 4wd rc car on: December 11, 2013, 03:25:19 am
That's a bad idea, don't have both battery power and the USB plugged in, it could damage the arduino. You might have a bad motor driver.

Added:
I did some reading on the adafruit website, and it turns out regular 9V batteries are no good, they are ok for the Arduino, but not the motor shield. You can use your other power supply to power the motors, just be sure to take off the jumper on the motor shield.
1224  Using Arduino / Project Guidance / Re: Help with 4wd rc car on: December 11, 2013, 02:50:33 am
Is that motor library up to date? Also set speed has a range from 0-255.
1225  Using Arduino / Project Guidance / Re: Help with 4wd rc car on: December 11, 2013, 01:53:26 am
Are you running it from the Arduino power port or the EXT port? Also what does your code look like?
1226  Using Arduino / Programming Questions / Re: Yet another question about strings to chars on: December 11, 2013, 01:18:34 am
Type cast (eeSETTINGS.charAt(i)) as an int.

Code:
String data = "ABCD";

void setup(){
  Serial.begin(9600);
  for(unsigned int i=0; i<4; i++)
  {
    Serial.print( data[i]);
    Serial.print("  ");
    Serial.println((int) data[i]);
  }
}

void loop() {
}

Output:
Quote

A  65
B  66
C  67
D  68
1227  Using Arduino / General Electronics / Re: Double RC circuit on: December 11, 2013, 12:22:08 am
Not only that, but when a cap is uncharged and the circuit is closed, both caps act as shorts. So once C1 starts to charge, slowly C2 begins to charge too. Now here is where is gets interesting. Once C2 is charging, C1 slowly discharges, adding more resistance to the circuit, and starts to split off the current to C2.

C2 will still charge, but now that both caps are charging at the same time, they are  acting as current dividers. Eventually (less than a second) the caps will equalize, but they will never reach the source voltage.This is because as Jack C pointed out they leak and want to refill.

The more stages you add, the lower the voltage on the caps will be.
Ideally both caps will equal the source voltage, but nothing is ideal.
1228  Using Arduino / Project Guidance / Re: Combining and sending joystick values using RF transmitter/reciever on: December 10, 2013, 06:17:17 pm
Look into the sprintf() function, it will help out a lot .
1229  Using Arduino / Project Guidance / Re: how to create a system which reads button presses on: December 10, 2013, 05:03:31 pm
Ok, simple. You can use the random() and randomSeed() functions to get the LEDs to blink.

Here is a simple button counter code.
Code:
const byte LED = 6;
const byte Button = 2;
byte ButtonState;
byte last = LOW;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);
 
  if(ButtonState && ButtonState != last) {
    analogWrite(LED, count += 5);// every time the button is pressed, count increases by 5. This can be changed to inc by 1
    Serial.println(count);
  }
 
  if(count > 255){
    digitalWrite(LED, LOW);
    count = 0;
  }
 
  last = ButtonState;
}
1230  Using Arduino / Project Guidance / Re: Help joining two pieces of code together (I think I need to know about 'case'?) on: December 09, 2013, 09:13:41 pm
Instead of cases, (which you should still learn ) make a simple button latch.

Here is a simple button latch you can use. How you implement it into your code, is for you to learn how to do.

Code:
byte buttonPin = 2;
byte LED = 13;

byte buttonState;
byte latch;
byte lastState;

void setup(){
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin); // read the state of the button

  if(buttonState && buttonState != lastState){ //See if the button state is HIGH, and does not equal the last state
    latch = !latch; // flip the state of the LED
    digitalWrite(LED, latch ? HIGH : LOW); // state of LED according to latch state
    Serial.println(latch); // visual of latch state
  }
  lastState = buttonState; // update last state
}
Pages: 1 ... 80 81 [82] 83 84 ... 231