Show Posts
Pages: 1 [2] 3 4 ... 12
16  Using Arduino / Programming Questions / Re: Waiting for Key pressed (keypad) on: November 22, 2011, 08:22:19 pm
Did you get an infinite loop because you didn't check the status of the keys inside the loop?
If you did then you should probably post the code.

Pseudocode:
Code:
boolean NO_KEY = true;

while(NO_KEY)
{
      if(any keys are pressed)
      {
           NO_KEY = false;
      }
}
17  Using Arduino / Programming Questions / Re: 11 LED Counter Help Needed on: November 21, 2011, 09:32:22 pm
If you think the speed is too fast, just increase the delay time between HIGH and LOW. The Solenoids/LEDs may take a while to respond, so a higher delay is probably what you seek.

int pins[] = {2,3};  
With only two, you get 00, 01, 10, and 11.

So when the for loop gets an "h" of value 3, and passes 3 into sendNumbersToPins, which then makes pins[3] HIGH, nothing will ever happen because the arduino has no idea what value is held at position 3 of the array pins. The are only two positions; 0 and 1. In fact, you should get a NullPointerException.

Each for loop starts at 0, and for every loop the counter goes up by one until the condition is false. The condition is that it has to be less than 10. This way we get 0-9. Putting loops inside of loops gets us 00-99.

Mr. Gammon's code is quite fun. You can add serial communication yourself. Serial.begin(9600); at the start of setup(). And just have fun inserting Serial.println("I am here in the code_1"); into parts of the code at a time.

http://arduino.cc/en/Reference/HomePage
18  Using Arduino / Programming Questions / Re: Programming Question... / Help with Messenger function... on: November 21, 2011, 07:09:20 pm
Let me guess, you downloaded the messenger library, extracted it, and placed it inside:

\Arduino-0022\libraries

Well, more work needs to be done. The Messenger file is NOT the library you want to place inside.
Messenger.h: No such file or directory
Tells me that the Arduino cannot find it because its hidden under piles of folders.

So instead of
\Arduino-0022\libraries\messenger\firmware\Messenger\Messenger.h

Take the important folder called Messenger from inside the messenger folder and place THAT inside your libraries. Everything else in the messenger folder is just useful tools the author included, but it's in the way of the compiler.

It should now look like this
\Arduino-0022\libraries\Messenger\Messenger.h
\Arduino-0022\libraries\Messenger\Messenger.cpp
\Arduino-0022\libraries\Messenger\keywords.txt
\Arduino-0022\libraries\Messenger\examples\...etc

Hope that helps.
19  Using Arduino / Programming Questions / Re: 11 LED Counter Help Needed on: November 21, 2011, 06:29:05 pm
The code is generally correct, just had a few misspellings. Now that I'm on a computer....

The capital S in setup()
The missing s in sendNumbersToPins()
The wierd int[] pins = {...};       
Should intead be int pins[] = {...};

Anyone know why int[] pins is unacceptable and int pins[] is acceptable? (at least on my compiler)

Code:
int sendNumberToPins = (h,i); // is incorrect, its like making a bucket of numbers and putting fish in it :D
sendNumbersToPins(h, i); //is correct, here you are calling a function defined later in the code, and passing the values of h and i into it

pinMode(pins, OUTPUT); // is incorrect, pins is an array, so when you say this the compiler is like...wait...which pin?
pinMode(pins[0], OUTPUT); //is correct, pins[0], pins[1]...pins[9]

This code works. Use serial to see whats going on. The arduino main page has a nice reference if you're confused about any syntax here.
Code:
int pins[] = {
  0,1,2,3,4,5,6,7,8,9}; 
//set up your 10 pins here IN THE CORRECT PIN ORDER!
//So if pin "4" is led "0" then 4 is the first number

void setup()
{
  Serial.begin(9600); //This starts up serial

  for(int i = 0; i < 10; i++) // This sets up each LED pin as output without writing 10 lines...
  {
    pinMode(pins[i], OUTPUT);
  }

  for(int h = 0; h < 10; h++)
  {
    for(int i = 0; i < 10; i++)
    {
      for(int j = 0; j < 10; j++)
      {
        for(int k = 0; k < 10; k++)
        {
          Serial.print(h);
          Serial.print(i);
          Serial.print(j);
          Serial.println(k);
          //sendNumberToPins(h,i,j,k);        //uncomment in order to send the digits to pins 
        }
      }
    }
  }

  Serial.println("Done"); //When the code gets here it will have gone to 9999.
}

void loop()
{
  //Do what you want over and over and over and over....here.
}

void sendNumberToPins(int a, int b, int c, int d)
{
  digitalWrite(pins[a],HIGH);
  delay(100);
  digitalWrite(pins[a],LOW);

  digitalWrite(pins[b],HIGH);
  delay(100);
  digitalWrite(pins[b],LOW);

  digitalWrite(pins[c],HIGH);
  delay(100);
  digitalWrite(pins[c],LOW);

  digitalWrite(pins[d],HIGH);
  delay(100);
  digitalWrite(pins[d],LOW);
}
20  Using Arduino / Programming Questions / Re: 11 LED Counter Help Needed on: November 21, 2011, 07:11:54 am
It will output 0001, 0002...0009, 0010...

Use Serial.println() to see whats going on. Each for loop represents a digit. With two loops you get 2 digits from 00 to 99.

My code did have a few errors. Setup should be lowercase "setup".
The for loop should go up to 10 otherwise you only get 0-8, when you really want 0-9.
for(int i = 0; i < 10; i++)

Let me know of that helps.
21  Using Arduino / Programming Questions / Re: Noob Help! Simple counter using LEDS on: November 19, 2011, 11:51:08 pm
You made two forum threads about the same thing.   
http://arduino.cc/forum/index.php/topic,79596.0.html

And this looks very similar:
http://arduino.cc/forum/index.php/topic,78001.0.html

Try to keep all your help under one thread.

You're going to have to do some work yourself. Here's 90% of it.
Code:
int[] numbers = {0,1,2,3,4,5,6,7,8,9};
int[] pins = {0,1,2,3,4,5,6,7,8,9};  //set up your 10 pins here in the correct order

void Setup()
{
//too lazy. you can code this. Just set up your pins.
}

void loop()
{
for(int h = 0; h < 9; h++)
{
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
for(int k = 0; k < 9; k++)
{
sendNumberToPins(h,i,j,k);
}
}
}
}
}
void sendNumbersToPins(int a, int b, int c, int d)
{
digitalWrite(pins[a],HIGH);
delay(50);
digitalWrite(pins[a],LOW);

digitalWrite(pins[b],HIGH);
delay(50);
digitalWrite(pins[b],LOW);

digitalWrite(pins[c],HIGH);
delay(50);
digitalWrite(pins[c],LOW);

digitalWrite(pins[d],HIGH);
delay(50);
digitalWrite(pins[d],LOW);
}
22  Using Arduino / Programming Questions / Re: charAt() on: November 08, 2011, 10:41:09 pm
It's zero indexed.
Your program will not do what you want it to, but you have the right idea.
So when you ask for spot 4, you think you'll get an "s", but rather you'll get a space because it counts starting at 0 and index 4 is a " ".

This may be what you meant:
Code:
String string1 = "This is string1";
String string2 = "This is string2";
String prompt = "The 4th Char in string1 is ";
Char charAt4;
//String charAt4;

void setup()
{
  Serial.begin(9600);
  Serial.println(string1);
  Serial.println(string2);
  Serial.print(prompt);
  charAt4 = string1.charAt(3); //Prints 4th spot, the s
  //charAt4 = (String)string1.charAt(3);
  Serial.println(charAt4);
}

void loop()
{
}
23  Using Arduino / Programming Questions / Re: Having trouble debugging my LED/ shaft encoder code on: November 07, 2011, 07:03:05 pm
Just as Paul said; Serial is helpful for debugging.
I rewrote the code a little bit and nothing seems to be wrong. It is going inside the for loop when turning LEDS off.

Perhaps you missed the latch(); in that else statement?

The rewritten code for debugging.
Code:




// Example code to use with LED strip with 1606 controller chip
// Code by Ben Moyes, bliptronics.com November 2010
// Please give credit or buy some LEDs from me!

// *       L2ggrrbb
// *       00 - LED off
// *       01 - LED on (max bright)
// *       10 - LED fade up   (start at min bright)
// *       11 - LED fade down (start at max bright)

// Set which pins you will use to connect to the LED strip, you need 4 digital pins.
#define SIPIN 2
#define DIPIN 3
#define CLKPIN 4
#define LATCHPIN 5

#define LEDS 16    //Set this to the number of LEDs in your strip
#define CLOCK_DELAY (LEDS * 250)/1000  //This is the required delay between sending a bit and the clock line being fired.
//You can tweak this value lower until you find LEDs on the end of your chain misbehaving
//In fact, if you are running less than a few hundred LEDs you can get away with zero here.
//250nSec per LED is the specified value for the chip.

const int  encoderPin = 8;    // the pin that the encoder is attached to
int encoderState = 0;         // current state of the encoder.  encoder wheel has 40 slots and 40 solid spaces between slots
// a "0" means photointerrupter is not blocked... "1" means photointerrupter is being blocked by encoder wheel
int prevEncoderState = 0;     // previous state of the encoder

// These are the instructions that can be loaded for each LED in the strip.
// You must OR a Command or Commandx2 with any combination of the color operations.
// eg Command | BlueOn | RedDown will make an LED fad from Purpple to Blue.
// eg Commandx2 | GreenUp will make an LED fad UP to Green at double speed.
/* #define Command B10000000
 #define Commandx2 B11000000 // Use this one to make dimming twice as fast. this code was commented out of code because it is not being used in this sketch
 #define BlueOff B00000000   // it was left here as reference to get the binary codes to drive the led's
 #define BlueOn B00010000
 #define BlueUp B00100000
 #define BlueDown B00110000
 #define RedOff B00000000
 #define RedOn B00000100
 #define RedUp B00001000
 #define RedDown B00001100
 #define GreenOff B00000000
 #define GreenOn B00000001
 #define GreenUp B00000010
 #define GreenDown B00000011
 */

int encoderStep = 0;

void setup()
{
  //Set up our pins to connect to the LED strip.
  pinMode(encoderPin, INPUT);
  pinMode(SIPIN, OUTPUT);
  pinMode(DIPIN, OUTPUT);
  pinMode(CLKPIN, OUTPUT);
  pinMode(LATCHPIN, OUTPUT);
  digitalWrite(SIPIN, LOW);
  digitalWrite(DIPIN, LOW);
  digitalWrite(CLKPIN, LOW);
  digitalWrite(LATCHPIN, LOW);
  Serial.begin(9600);


void loop()
{
  encoderState++;
  //Serial.println(encoderState);
  //Serial.println(encoderStep);
  delay(1000);
  if (encoderState != prevEncoderState){ //Is there a change from High to LOW or from LOW to HIGH in the encoder output???
    if ( encoderStep==0) {// encoderstep 0 is the first step in encoder wheel.  If the encoder is at "0" then light up all 16 LED's
      for(int ledNumber=0;ledNumber < 16; ledNumber++) {
        SendByte(B10000000|B00000001); 
        latch();
      }
      encoderStep=encoderStep+1; // step encoder position counter by 1
    }
    else { //if encoderStep doesnt equal 0 then turn off all LED's
      for(int ledNumber=0;ledNumber < 16; ledNumber++) {
        SendByte(B10000000|B00000000);
        Serial.println(ledNumber);
      }
      encoderStep=encoderStep+1;  //step encoder position counter by 1
    }
  }
  if (encoderStep==80) { //check to see if encoder has done a full turn (80) if yes, then reset the counter for the next rotation
    encoderStep=0;
  }
  prevEncoderState = encoderState;  //update the previous value of the encoder with the current one   
}


void SendByte(unsigned char it)
{
  //Send out one byte, don't forget to LATCH it by calling latch()
  //Note that for LARGE number of LEDs you may need to slow things down a little here. 
  digitalWrite(CLKPIN, LOW);

  char x;
  for(x=0;x < 8; x++)
  {
    if(B10000000 & it)
      digitalWrite(DIPIN, HIGH);
    else
      digitalWrite(DIPIN, LOW);
    it = it<<1;
    // Wait here needs to be 250 x Number of LEDs nano seconds to allow data to propagate before being clocked in
    delayMicroseconds(CLOCK_DELAY);
    digitalWrite(CLKPIN, HIGH);
    digitalWrite(CLKPIN, LOW);

  }
}

void latch()
{
  digitalWrite(LATCHPIN, HIGH);
  delayMicroseconds(1);  // spec sheet specifies minimum latch pulse of 1us
  digitalWrite(LATCHPIN, LOW);
}

24  Using Arduino / Programming Questions / Re: i2c byte data to char on: November 07, 2011, 05:47:58 pm
I'm curious to know also.

My first try would be type casting.
Does this work?

char test = (char)datastring;

25  Using Arduino / Programming Questions / Re: Help with arrays and code simplification on: November 02, 2011, 08:45:32 pm
Here is what I understand: You have the centimeters part down and good to go, you just want to convert the information into 7 pins, which are each connected to 4 leds. From what you've stated, your problem with the "stuck" is due to the while loop.

You are stuck inside this loop because nothing will change the cm variable. To fix this, just call that method inside the loop. Like so.
Code:
while (cm < 46)
{
    for (int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      digitalWrite(ledPins[thisPin], HIGH);   
      delay(100);                 
      digitalWrite(ledPins[thisPin], LOW);   
    }
    cm = microsecondsToCentimeters(duration);

I assume from the video, you are showing me the result of the while loop.
Quote
Each color group should both be on at the same time as each other, but they aren't in this video.
To make the color groups on at the same time, you need to call the next ledPin. So this is the next step.
Code:
while (cm < 46)
{
    for (int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      digitalWrite(ledPins[thisPin], HIGH);   
      digitalWrite(ledPins[thisPin+1], HIGH;
      delay(100);                 
      digitalWrite(ledPins[thisPin], LOW);
      digitalWrite(ledPins[thisPin+1], LOW);   
    }
    cm = microsecondsToCentimeters(duration);

This will however, mess with pins 0 and 1. It may or may not matter for you.
As for elegance, just study what the more experienced programmers do, understand it, and emulate it. If it works within your goals, it works. smiley
26  Using Arduino / Programming Questions / Re: Help with Binary Clock program on: October 30, 2011, 09:01:02 pm
See where it says
Code:
if (hour >=12)
{
  hour=0;
  minute = 0; // reset minutes to zero
}

I'm guessing it's a 12 hour clock, reseting at the 12th hour.
My quick add would be to declare this at the top:
boolean AM = true;

and then modify the code at the 12 hour part here to look like this
Code:
if (hour >=12)
{
  hour=0;
  minute = 0; // reset minutes to zero
  if(AM)
  {
     AM = false;
  }
  else
  {
     AM = true;
  }
}

What this does is alternates the boolean variable "AM" between true and false everytime the hour hits 12.
Now you just have to do something with this variable to make pin 13 light up.

Somewhere in your loop add something along this line.
if(AM)
   pin 13 = HIGH;
else
  pin 13 = LOW;

If you gave your clock a preset time, say 10:54 AM, then pin 13 ON would mean AM. If you gave your preset time as 10:54 PM, then pin 13 ON would mean PM. The lighting indication simply depends on how you set your clock.
27  Using Arduino / Programming Questions / Re: Receiving Data on: August 19, 2011, 08:07:24 pm
I have a new problem. I decided to let the above code run and I turned off my radio, turns out A and B still equaled each other. I unplugged the audio jack, and analogRead went haywire as it should. I plugged the audio jack back in, A = B. I took out the batteries, A = B. I unplugged the jack, A != B.

Is the 1 Mhz crystal oscillator causing analog pin 0 to fluctuate?

I narrowed it down to this wierd circuit, whereby A = B.


Original Circuit
28  Using Arduino / Programming Questions / Receiving Data on: August 19, 2011, 07:32:10 pm
Hello
I have a crystal oscillator and an AM radio set up so the crystal transmits and the radio recieves. The crystal's high and low is controlled by pin 9. The AM radio goes through an envelope detector and then to analog pin 0. When analog pin 0 goes past a certain value variable A = 1, or if it goes under then variable A = 0.

My goal is to set up a system whereby I can transmit a message and receive it.

My system could probably only handle two bits per second.

One way for me to transmit data would be in binary. Change the state of pin 9 every half second to reflect the binary data.
My problem is that I'm unsure how to receive that data. I clearly see variable A changing states. I just can't put all those changing 0's and 1's back together to make a message.

Code:
int A = 0;
int B = 0;
long interval = 500;
long previousMillis = 0;
int crystalPin = 9;
int crystalState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(crystalPin, OUTPUT);
}

void loop() {
  //This part makes the crystal go High then Low every 0.5 seconds
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;  
    if (crystalState == HIGH) {
      crystalState = LOW;
      B = 0;
    }
    else {
      crystalState = HIGH;
      B = 1;
    }
  }
  digitalWrite(crystalPin, crystalState);
  
  //This part receives the Highs and Lows, 1 = High, 0 = Low
  if(analogRead(A0) > 10) {
    A = 1;
  }
  else {
    A = 0;
  }
  
  Serial.print(A); //A is the value of what is received
  Serial.print(" ");
  Serial.println(B); //B is the value of what is transmitted.
 //A and B should be equal
}

The code above was just a test. According to the Serial, variable A and B have the same states all the time, which is good.

If you have a solution, a hint, or another method, please let me know.
Thanks!
29  Using Arduino / Programming Questions / Re: Better Coding Technique? on: August 16, 2011, 10:26:41 pm
In case any of you were wondering, I solved my button problem. Here is the flow chart:


And here the important part of the code. (The entire code is huge and unrelated)
Code:
void buttons() {
  switch (chuckBit) {           //chuckBit is the result of AWOLs excellent recommendation. Returns a number for every combo.
    case 1:
      comboCheck();
      if(switch_01) {            //switch as seen in flowchart
        wait = false;             //wait is boolean
        lcd.clear();
        lcd.print("Case 1");
        delay(100);
      }
      switch_01 = true;         //This is needed or else multiple presses wont work
      break;
    case etc...
}

void comboCheck() {
  unsigned long comboTime = millis();
  unsigned long comboWait = millis() + 500;
  while((comboTime < comboWait) && wait) {           //loop this whole while code for 500ms
    comboTime = millis();
    chuckdata();                                                    //refresh the incoming wii nunchuck data
    if(lastBit != chuckBit) {                                      //if the data is different, means a new button has been pressed or released
      switch_01 = false;                                           //do not execute the original plan
      break;                                                          // lets get outa this while loop immediately!
    }
    else {
      lcd.setCursor(0,0);
      lcd.print("Ready");                                          //same thing that was said on lcd before any buttons were pressed
    }
  }
}
30  Using Arduino / Programming Questions / Re: Better Coding Technique? on: August 16, 2011, 05:33:45 pm
I wasn't sure what you meant and was even more confused when I looked it up. Someone should add examples to the reference page. With guessing I managed to figure it out! This new code is much much more user-friendly! Thanks!

Code:
  lastTest = test;
 
  bitWrite(test,0,chuck.cPressed());
  bitWrite(test,1,chuck.zPressed());
  bitWrite(test,2,chuck.rightJoy());
  bitWrite(test,3,chuck.leftJoy());
  bitWrite(test,4,chuck.upJoy());
  bitWrite(test,5,chuck.downJoy());
 
  if(lastTest != test) {
    lcd.clear();
  }
  lcd.setCursor(0,0);
  lcd.print(test);

It returns a unique number for every combo. The basic buttons are 1,2,4,8,16,32. The combos are the addition of them. With that, I could possibly use the simple switch case for designating what to do for each combo.

As for the last part, "wait for 1 second to check for more button presses", I haven't figured out a good method yet. I'm considering designating button C as the combo activator. Meaning that I have to hold down C to pause bitWrite and press all the botton combos and then release C to run bitWrite. That would prevent it from immediately executing one of the button's funtions when I haven't finished pressing my combos.

The bitWrite function certainly is interesting!
Thanks alot!
Pages: 1 [2] 3 4 ... 12