int beats[] = {1, 0, 1, 0, 1, 0, 1, 0 };
int pinOut = 11;
int arrElem = 8;
void setup() {
int i;
for (int i = 0; i < arrElem; i++) {
pinMode(pinOut, OUTPUT);
}
}
void loop() {
for (int i = 0; i < arrElem; i++) {
if (beats[i] = 1)
{
digitalWrite(pinOut,HIGH);
}
else
digitalWrite(pinOut,LOW);
}
}
if (beats = 1)[/quote] !!
compare... ==
```
*byte beats[] = {1, 0, 1, 0, 1, 0, 1, 0 };
int pinOut = 11;
int arrElem = sizeof(beats[]);
void setup()
{
pinMode(pinOut, OUTPUT);
}
void loop()
{
for (int i = 0; i < arrElem; i++) digitalWrite(pinOut,beats[i]);
// delay(555); // to see 'blink'
}*
```
Next time you screw up your post, just edit it, instead of creating a brand new thread.
drumbotz:
int beats[] = {1, 0, 1, 0, 1, 0, 1, 0 };
int pinOut = 11;
int arrElem = 8;
void setup() {
int i;
for (int i = 0; i < arrElem; i++) {
pinMode(pinOut, OUTPUT);
}
}
void loop() {
for (int i = 0; i < arrElem; i++) {
if (beats[i] = 1)
{
digitalWrite(pinOut,HIGH);
}
else
digitalWrite(pinOut,LOW);
}
}
You're using 16 bit ints to hold 1 bit of information.
I know that examples show using int for almost every number but they were written by PC programmers.
It doesn't make a difference now but the habit can screw you badly later.
UNO has 2048 bytes for everything including function call return addresses and parameters going on the stack, and loop returns and non-static local variables, all of which can really add up, errrr, down since the stack usually starts at the top of RAM and works down while the heap with your global and static variables starts at the bottom and works up. If the two cross, the crash may be quickly apparent (reset) or cause hard to trace bugs.
Good habits are like washing your hands to cut down on getting a cold or the flu or worse. People will say all kinds of things to justify not "washing their hands".
int is a signed 16 bit integer, good for -32768 to 32767
word is an unsigned 16 bit integer, good for 0 to 65535
short is a signed 8 bit integer, good for -128 to 127
byte is an unsigned 8 bit integer, good for 0 to 255 --- which covers most for-loops and array indexes
There are also the 32 bit long and unsigned long, good for billions.
millis() and micros() used for timing return unsigned long
For your own good, pick the smallest type you need and get out of using "automatic int" for every small number you use.
Beyond that, if you ever need to store/use a lot of 0 or 1 values then learn and use bits.
http://playground.arduino.cc/Code/BitMath
Compress the data into bytes:
byte beats [] = {0b10101010, 0b11001100, 0b11011101, };
byte maxBeats = 2; // 0, 1, 2 in this example, track which data is used
byte beatsMask[] = {0b0000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000,};
or
byte beatsMask[] = {0x80, 0x40, 0x20,0x10, 0x08, 0x04, 0x02, 0x01,};
byte beatbit; // track which bit is being looked at: 7,6,5,4,3,2,1,0
Then mix in with your time checks:
for ( beatElement = 0; beatElement<maxbeatElement; beatElement = beatElement +1) {
for (beatBit = 7; beatBit >=0; beatBit = beatBit -1){
if (( beats[beatElement] & beatsMask[beatBit] ) != 0{ // !=0 is 0x80, 0x40, x020, x010, 0x08, 0x04, 0x02, 0x01
// bit 7,6,5,4,3,2,1,0 of beats[0], then beats[1], then beats[2]
// are checked to see if they are 1 or 0
// if so, make a noise
} // otherwise, next bit
} next element
However.... this code will flash the led 4 times on and off so fast you won't see it.
So how about that?
drumbotz:
int beats[] = {1, 0, 1, 0, 1, 0, 1, 0 };
int pinOut = 11;
int arrElem = 8;
void setup() {
int i;
for (int i = 0; i < arrElem; i++) {
pinMode(pinOut, OUTPUT);
}
}
void loop() {
for (int i = 0; i < arrElem; i++) {
if (beats[i] = 1)
{
digitalWrite(pinOut,HIGH);
}
else
digitalWrite(pinOut,LOW);
}
}
to "GoForSmoke".. His got that from reply 1.
and other replier explains a lot. This was a good lessor, I think.
I got the part about
if (beats = 1)!!
compare... ==
but that is a little confusing and then the example you gave did not use that!
In his code, true it should be
if (beats[i] == 1)
How about this? It is ready to add serial input of new pattern, or a pot to adjust beat period or both or different even while the beats still run.
byte ledPin = 13; // Arduino onboard led -- that I do have.
word beats = 0b1010101001001000;
byte beatElem = 16;
byte beatIndex = beatElem - 1; // reading bits high to low order
// time variables
unsigned long beatStart, beatPeriod = 555UL;
void setup()
{
pinMode( ledPin, OUTPUT );
}
void loop()
{
if ( millis() - beatStart >= beatPeriod )
{
beatStart += beatPeriod;
digitalWrite( ledPin, bitRead( beats, beatIndex-- ));
}
if ( beatIndex == 0 )
{
beatIndex = beatElem - 1;
}
}
looks great., now add your pattern and speed
knut_ny:
looks great., now add your pattern and speed
I want to see what drumbotz has to say. I already have pot code from earlier today, so I throw it in.
/*
Circuit is a potentiometer with outside legs to 5V and GND, middle to A0
*/
byte ledPin = 13; // Arduino onboard led -- that I do have.
word beats = 0b1010101001001000;
byte beatElem = 16;
byte beatIndex = beatElem - 1; // reading bits high to low order
const byte potPin = A0;
word potVal;
// time variables
unsigned long beatStart, beatPeriod = 555UL;
void setup()
{
pinMode( ledPin, OUTPUT );
pinMode( potPin, INPUT );
potVal = analogRead( potPin );
beatPeriod = (unsigned long) potVal;
}
void loop()
{
if ( millis() - beatStart >= beatPeriod )
{
beatStart = millis();
beatPeriod = (unsigned long) potVal;
digitalWrite( ledPin, bitRead( beats, beatIndex-- ));
}
if ( beatIndex == 0 )
{
beatIndex = beatElem - 1;
}
potVal = analogRead( potPin );
}
try :beatperiod = portval ( allows for 0,,1023)
..or any function like : beatperiod= min_number + facktor*portval
Try running what I posted. I did. It works.