Show Posts
Pages: [1] 2 3 ... 229
1  Using Arduino / Project Guidance / Re: Button Debounce on: Today at 09:20:20 am
This is ehh...Ok, there is a better way, but for now, its ok.
Quote
bcurrent = digitalRead(switch1);
    if (bcurrent != LOW)
    {
      delay(5);
      bcurrent = digitalRead(switch1);
    }
    return bcurrent;

This however is not.
Quote
if (track1 == HIGH)
    {
      ledtrack = !ledtrack;
      digitalWrite(led1, ledtrack);
      bcurrent = LOW;
    }
Assuming you want to make a latch, press once do something, press again undo, or do something else, then this part he is wrong.

You don't want to see if it is HIGH all the time, you want to see if it transitions from HIGH to LOW and if that is true AND it is high, then the IF statement is true. Also "bcurrent = LOW;" isn't doing anything.
2  Using Arduino / Programming Questions / Re: How to send and recieve data via 433MHZ module on: July 28, 2014, 08:11:59 pm
Its in the PDF at the bottom.

This is literally all you need, it does both transmit and receive. It couldn't be any simpler.

Bottom of PDF
Quote
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
   Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
//Future Electronics Egypt Ltd. (Arduino Egypt).
 mySerial.begin(4800);
 mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
 mySerial.write(Serial.read());
}
3  Using Arduino / Programming Questions / Re: int to byte conversion on: July 28, 2014, 06:32:39 pm
This is a little closer to what you wanted, conversion-wise, but speed-wise, I'm not sure.

Edit:
Code:
int data[9];
boolean Output[144];
byte counter = 0, i = 0, j = 1, k = 0;
boolean done = false, shown = false;
long time[9];
void setup()
{
  Serial.begin(115200);
  delay(1);
  setArrays();
  showArrays();
  i = 14;
}

void loop()
{
  while(done == false)
  {
    time[k] = micros();
    for(byte counter = 0; counter < 9; counter++)
    {
     Output[i] = bitRead(data[k], counter);
     i++;
    }
    if(k < 9)
      k++;
    else
      done = true;
  }
  showCompletedOutput();
}

void setArrays()
{
  for(byte tmp = 0; tmp < 9; tmp++)
    data[8-tmp] = random(255, 5000);

  for(byte tmp2 = 0; tmp2 < 144; tmp2++)
    Output[tmp2] = 0;
}

void showArrays()
{
  Serial.print("Data: ");
  for(byte tmp = 0; tmp < 9; tmp++)
  {
      Serial.print((data[8-tmp] & 0x1FF), BIN);
      Serial.print("|");
  }
  Serial.println();

  Serial.print("__________Output: ");
  for(byte tmp2 = 0; tmp2 < 72; tmp2++)
    {
      Serial.print(Output[tmp2],BIN);
     if((tmp2 % 8) == 0)
        Serial.print("|");
    }
}

void showCompletedOutput()
{
  if(shown == false)
  {
    Serial.println();
    Serial.print("Completed Output: ");
    for(byte tmp2 = 0; tmp2 < 72; tmp2++)
    {
      Serial.print(Output[71-tmp2],BIN);
      if((tmp2 % 8) == 0)
        Serial.print("|");
    }
    Serial.println();
    for(byte t= 0; t < 9; t++)
    {
      Serial.print(time[t]);
      Serial.print("|");
    }
    shown = true;
  }
}

Quote
Data: 100001011|110001101|1110110|11010|10110011|111101111|111010100|11110|100001000|
__________Output: 0|00000000|00000000|00000000|00000000|00000000|00000000|00000000|00000000|0000000
Completed Output: 0|11000001|10100101|10011111|10111111|10101000|00011110|10000100|00000000|0000000
12704|12732|12760|12792|12820|12848|12884|12912|12944|
4  Using Arduino / Programming Questions / Re: int to byte conversion on: July 28, 2014, 02:00:07 pm
You can give this a shot, I did kinda just threw it together, so if it doesn't work, i'm sorry.

Code:
int data[9];
byte output[12];
byte counter = 0, i = 0, j = 0, k = 0;
boolean done = false, shown = false;

void setup()
{
  Serial.begin(115200);
  delay(1);
  setArrays();
  showArrays();
  i = 7;
}

void loop()
{
  while(done == false)
  {
    for(byte counter = 0; counter < 9; counter++)
    {
      bitWrite(output[j], i, bitRead(data[k], counter));
      i++;
      if(i > 7)
      {
        j++;
        i = 0;
      }
    }
    if(k < 9)
      k++;
    else
      done = true;
  }
  showCompletedOutput();
}

void setArrays()
{
  for(byte tmp = 0; tmp < 9; tmp++)
    data[tmp] = random(255, 5000);

  for(byte tmp2 = 0; tmp2 < 12; tmp2++)
    output[tmp2] = 0;
}

void showArrays()
{
  Serial.print("Data: ");
  for(byte tmp = 0; tmp < 9; tmp++)
  {
    Serial.print(data[tmp]);
    Serial.print(", ");
  }
  Serial.println();

  Serial.print("Output: ");
  for(byte tmp2 = 0; tmp2 < 12; tmp2++)
  {
    Serial.print(output[tmp2]);
    Serial.print(", ");
  }
  Serial.println();
}

void showCompletedOutput()
{
  if(shown == false)
  {
    Serial.print("Completed Output: ");
    for(byte tmp2 = 0; tmp2 < 12; tmp2++)
    {
      Serial.print(output[tmp2]);
      Serial.print(", ");
    }
    Serial.println();
    shown = true;
  }
}

5  Using Arduino / Programming Questions / Re: int to byte conversion on: July 28, 2014, 11:57:00 am
You may need to use an array for the 12 bytes, this way you can use bitRead and the module operator. The first Int gets stored at 0x80 of the first byte, so you will need to start a counter at say 7. Increment that counter from 0 - 8, and use the module operator to increment the index when the counter is 7, to change which byte the int value(bit) gets stored into.

If I had my Nano, with me I could do a test, but unfortunately, I left it home. Sorry.
6  Using Arduino / Programming Questions / Re: A timer that counts down based on how many times a button is pressed on: July 28, 2014, 09:17:15 am
How to debounce a button(wired properly and not floating), how to blink an LED without the uses of the delay() function and how to keep track of time with the millis() function.

All of which are provided under your Arduino Example sketches.

I should also add incrementing and resetting a variable.
7  Using Arduino / Programming Questions / Re: Very strange LED cube error on: July 27, 2014, 12:29:23 am
Other than using more RAM, I don't see anything wrong with the code. You said you were powering the LEDs from the arduino, and that only one was on at a time, right? What about when the cube is fully lit? I still think its the amount of current being drawn which could be the probable issue. That or you have a memory leak somewhere that I don't see.

Code:
int row[4] = {
  A5,A4,A3,A2};
int col[16] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};
int time = 1;
int mtime = 300;
int counter = 5;
#define DEBUG false

//==============================================================
#if false
unsigned int l[160] = {
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632
};
#endif
unsigned int l[444] = {1,0,0,0,3,0,0,0,7,0,0,0,15,0,0,0,143,0,0,0,2191,0,0,0,34959,0,0,0,51343,0,0,0,59535,0,0,0,63631,0,0,0,63887,0,0,0,63903,0,0,0,63935,0,0,0,63999,0,0,0,65023,0,0,0,65535,0,0,0,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535};

//==========================================================

void setup()
{
  for(int i=0;i<16;i++)
    pinMode(col[i],OUTPUT);
#if DEBUG
  Serial.begin(115200);
  delay(1);
  Serial.print("FreeRam remaining: ");
  Serial.println(freeRam());
  Serial.println(sizeof(l) /2);
#endif
}

int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval==0?(int)&__heap_start:(int)__brkval);
}

void loop()
{
  for(int i=0; i < sizeof(l)/2; i += 4)
  {
    for(int q = 0; q < 4 ; q++)
    {
      conv(l[i+q],(i+q) % 4);
      delay(time);
    }
    delay(100);
  }
}

void conv(unsigned int num, int k)
{
  //word w=word(num);
  static unsigned int leder;
  for(int i=15; i >= 0; i--)
  {
    if(bitRead(num,i) != 0)
    {
      leder = (bitRead(num,i) * i + (16 * k));
      #if DEBUG
      bitRead(num,i)? Serial.print("1") : Serial.print("0");
      #endif
      led(leder);
    }
    else
      led(-1);
  }
  #if DEBUG
  Serial.println();
  #endif
}

/*void reset(){
 for(int i=0;i<16;i++){
 digitalWrite(col[i],LOW);
 }
 for(int i=0;i<4;i++){
 pinMode(row[i],INPUT);
 }
 }
 */
 
void led(int leds)
{
  if(leds != -1)
  {
    for(int i=0; i<4; i++)
    {
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],OUTPUT);
        digitalWrite(row[i],LOW);
      }
    }
    digitalWrite(col[leds % 16],HIGH);
  }
  delayMicroseconds(mtime);

  if(leds != -1)
  {
    for(int i=0; i<4; i++)
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],INPUT);
      }
    digitalWrite(col[leds % 16],LOW);
  }
}
8  Using Arduino / Programming Questions / Re: Need help with code for autonomous rc car on: July 26, 2014, 04:46:58 pm
Im amazed you never learned what a state machine.

Take a look at the following link.
http://www.perl.com/pub/2004/09/23/graphics/final.jpg
9  Using Arduino / Programming Questions / Re: Very strange LED cube error on: July 26, 2014, 04:30:33 pm
Try this,
Code:
int row[4] = {
  A5,A4,A3,A2};
int col[16] = {
  2,2,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};
int time = 1;
int mtime = 300;
int counter = 5;

//==============================================================
unsigned int l[160] = {
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  65535,65535,65535,65535,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  1632,65535,65535,1632,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  0,1632,1632,0,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632,
  1632,28662,28662,1632
};
//==========================================================

void setup()
{
  for(int i=0;i<16;i++)
    pinMode(col[i],OUTPUT);
  Serial.begin(115200);
  delay(1);
  Serial.print("FreeRam remaining: ");
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval==0?(int)&__heap_start:(int)__brkval);
}

void loop()
{
  for(int i=0; i < sizeof(l) /2; i += 4)
  {
    for(int q = 0; q < 4 ; q++)
    {
      conv(l[i+q],(i+q) % 4);
      delay(time);
    }
  }
}

void conv(unsigned int num, int k)
{
  //word w=word(num);
  unsigned int leder;
  for(int i=15; i >= 0; i--)
  {
    if(bitRead(num,i) != 0)
    {
      leder = (bitRead(num,i) * i + (16 * k));
      //Serial.println(leder);
      led(leder);
    }
    else
      led(-1);
      delay(500);
  }
}

/*void reset(){
 for(int i=0;i<16;i++){
 digitalWrite(col[i],LOW);
 }
 for(int i=0;i<4;i++){
 pinMode(row[i],INPUT);
 }
 }
 */
 
void led(int leds)
{
  if(leds != -1)
  {
    for(int i=0; i<4; i++)
    {
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],OUTPUT);
        digitalWrite(row[i],LOW);
      }
    }
    digitalWrite(col[leds % 16],HIGH);
  }
  delayMicroseconds(mtime);

  if(leds != -1)
  {
    for(int i=0; i<4; i++)
      if((16*i) <= leds && leds < (16*(i+1)) )
      {
        pinMode(row[i],INPUT);
      }
    digitalWrite(col[leds % 16],LOW);
  }
}

I don't have the LEDs to make  4x4x4 cube, but could you send me the array that is causing the problems?
10  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 26, 2014, 12:11:56 pm
I know what bouncing a button is, but that code doesn't need it.
11  Using Arduino / Programming Questions / Re: Very strange LED cube error on: July 26, 2014, 12:38:16 am
Please do
12  Using Arduino / Programming Questions / Re: Very strange LED cube error on: July 25, 2014, 11:59:36 pm
How are the LED powered, from the arduino or external source?

Can you post a picture?

Also as long as you are not using the serial monitor, you can use pins 0 and 1.
13  Using Arduino / Programming Questions / Re: Very strange LED cube error on: July 25, 2014, 11:38:01 pm
Here's your problem, you want unsigned int
Quote
int l[] = {65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,65535,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,1632,65535,65535,1632,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,0,1632,1632,0,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632,1632,28662,28662,1632};

Edit: counter -> q
Added: you could also do this:
Code:
for(int q = 0; q < counter; q++){ /* make counter = 4, and not 5 */
 conv(l[i + q],(i + q) % 4);
 delay(time); // is this really needed?
  }
14  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 25, 2014, 10:58:48 pm
How big of a delay? I can tell you your serial monitor is not going to work correctly because your using digital pin 1. Start at pin 2 instead.

Added:
Should be, for(int i = 0; i < 16; i++) and not, for(int i = 0; i<= 16; i++)
15  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 25, 2014, 10:04:38 pm
:c
You were doing so umm... good with your arrays, why did you stop?
Quote
boolean state1, lastState1 = LOW;
boolean state2, lastState2 = LOW;
boolean state3, lastState3 = LOW;
boolean state4, lastState4 = LOW;
boolean state5, lastState5 = LOW;
boolean state6, lastState6 = LOW;
boolean state7, lastState7 = LOW;
boolean state8, lastState8 = LOW;
boolean state9, lastState9 = LOW;
boolean state10, lastState10 = LOW;
boolean state11, lastState11 = LOW;
boolean state12, lastState12 = LOW;
boolean state13, lastState13 = LOW;
boolean state14, lastState14 = LOW;
boolean state15, lastState15 = LOW;
boolean state16, lastState16 = LOW;

These here can go into FOR loops,
Quote
boolean buttonPress[16]; // No need for zeros here, and should be boolean, unless you are looking for a value between 0 -1023, in which case you would be using type INT and analogRead().
 
long previousMillis[48]; // again no zeros needed here

In your setup() function, you use the FOR loops and set the arrays to zero, just like you did with your pinModes.

In your loop() function, you could use either a FOR loop or IF/ELSE statements and go through each array and change their indexes at the same time.
Pages: [1] 2 3 ... 229