sketch runs differently on ATTINY than ATMEGA

Hi there, I am working on running a sketch written for arduino on an ATTINY85v. Thanks to the help from Coding Badly in my previous thread, I was able to transfer the sketch to the ATTINY85v successfully. http://arduino.cc/forum/index.php?topic=108797.new;topicseen#new

My issue though is that the sketch does not do what it is supposed to do at all. I have 3 LED’s hooked up to physical pins 4,5,and 6 of the ATTINY, and yet only one of the LED’s light up, and it just either stays on all the time or it blinks. I thought that I was using code that was compatible with the ATTINY, but maybe I have overlooked something. I tried different MHz speeds for the ATTINY and still no luck.

Any idea why this is not working properly on an ATTINY?
some of the arrays have been removed due to exceeding the word limit for posts

long lastTime = 0;
const int RPIN = 0;
const int GPIN =1;
const int BPIN =4;
const int SWITCHPIN = 2;
int countArray[] = {200,125,200,18,23,72,8};
int maxSwitch = 9;
int currentPlace = 0;
int count = 0;
boolean pressed = false;


boolean rArray0[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean gArray0[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean bArray0[] = {255,255,255,255,100,100,100,100,100,255,255,255,255,100,100,100,100,100};
int hArray0[] = {0,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};


/*omit
boolean rArray5[] = {1,0,1,0,1,1,1,1,0,1,0,1,1,1};
boolean gArray5[] = {1,1,1,0,1,0,1,1,1,1,0,1,0,1};
boolean bArray5[] = {1,0,1,1,1,0,1,1,0,1,1,1,0,1};
int hArray5[] = {34,34,34,34,34,34,34,34,34,34,34,34,34,34};  
*/

/* omit
boolean rArray7[] = {1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0};
boolean gArray7[] = {0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1};
boolean bArray7[] = {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0};
int hArray7[] = {75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75};  
*/

boolean rArray3[] = {127,0,0,100,0,0,127,0,0,100,0,0,127,0,0,100,0,0};
boolean gArray3[] = {0,0,127,0,0,100,0,0,127,0,0,100,0,0,127,0,0,100};
boolean bArray3[] = {0,127,0,0,100,0,0,127,0,0,100,0,0,127,0,0,100,0};
int hArray3[] = {25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25};  

boolean rArray4[] = {0,127,0,100,100,100,0,0,0,100,100,0,0,127,0,0,127,0,100,100,100,0,100};
boolean gArray4[] = {0,0,127,100,0,0,100,100,0,127,0,100,100,0,100,100,100,100,100,0,100,100,100};
boolean bArray4[] = {100,100,100,0,127,0,0,100,100,100,100,0,100,100,0,127,0,127,0,100,100,0,127};
int hArray4[] = {20,50,25,20,25,25,30,50,20,20,50,32,25,20,35,50,30,25,30,25,20,30,50};

boolean rArray5[] = {100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0};
boolean gArray5[] = {0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100};
boolean bArray5[] = {0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0,0,100,0};
int hArray5[] = {0,250,250,250,250,250,250,250,250,250,250,250,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25};  


boolean rArray6[] = {100,127,0,0,0,100,0,127,100,0};
boolean gArray6[] = {0,0,0,100,127,0,0,100,0,0};
boolean bArray6[] = {100,0,127,0,100,0,127,0,100,127};
int hArray6[] = {500,500,500,500,500,500,500,500};

//boolean mRArray[] = {rArray1[],rArray2[]};    1+10+10+11=32 array0      
//boolean mGArray[] = {gArray1[],gArray2[]};
//boolean mBArray[] = {bArray1[],bArray2[]};




void setup(){
  Serial.begin(9600);
  pinMode(RPIN,OUTPUT);
  pinMode(GPIN,OUTPUT);
  pinMode(BPIN,OUTPUT);
  pinMode(SWITCHPIN,INPUT);
 // pinMode(SPEEDPOT,INPUT);
}



void checkBtn(){
 boolean switcher = digitalRead(SWITCHPIN);
 if(!pressed && switcher){
  pressed = true;
  currentPlace++;
  count = 0;
  lastTime = millis();
 }
 if(pressed && !switcher){
   pressed = false;
 }
}



void loop(){
 checkBtn();
  

  if(lastTime == 0){
  lastTime = millis();
 }
 
 //POTvalue = analogRead(SPEEDPOT);
 
///////////////////////////////// 
 
 if(lastTime + count < millis() && currentPlace == 0){ 
   hArray0[count];
   analogWrite(RPIN,rArray0[count]);
   analogWrite(GPIN,gArray0[count]);
   analogWrite(BPIN,bArray0[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 //////////////////////////
  if(lastTime + count < millis() && currentPlace == 1){  
   hArray1[count]; 
   analogWrite(RPIN,rArray1[count]);
   analogWrite(GPIN,gArray1[count]);
   analogWrite(BPIN,bArray1[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 /////////////////////////////////
  if(lastTime + count < millis() && currentPlace == 2){   
   hArray2[count]; 
   analogWrite(RPIN,rArray2[count]);
   analogWrite(GPIN,gArray2[count]);
   analogWrite(BPIN,bArray2[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 ///////////////////////////
 
 
 
  if(lastTime + count < millis() && currentPlace == 3){  
   hArray3[count]; 
   analogWrite(RPIN,rArray3[count]);
   analogWrite(GPIN,gArray3[count]);
   analogWrite(BPIN,bArray3[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 /////////////////////////// 
 
  if(lastTime + count < millis() && currentPlace == 4){   
    hArray4[count];
   analogWrite(RPIN,rArray4[count]);
   analogWrite(GPIN,gArray4[count]);
   analogWrite(BPIN,bArray4[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 ///////////////////////////
   if(lastTime + count < millis() && currentPlace == 5){   
   hArray5[count];  
   analogWrite(RPIN,rArray5[count]);
   analogWrite(GPIN,gArray5[count]);
   analogWrite(BPIN,bArray5[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 ///////////////////////////
   if(lastTime + count < millis() && currentPlace == 6){   
   hArray6[count];
   analogWrite(RPIN,rArray6[count]);
   analogWrite(GPIN,gArray6[count]);
   analogWrite(BPIN,bArray6[count]);
   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0; 
   }
 }
 
     if(currentPlace >= maxSwitch){
   currentPlace = 0;
  } 
  Serial.println (POTvalue); 
  
 }
const int RPIN = 11;
const int GPIN =10;
const int BPIN =9;
const int SWITCHPIN = A4;

This is pinnumbers from Arduino, you have to change them to the pinnumbers on the Attiny

Erni:
This is pinnumbers from Arduino, you have to change them to the pinnumbers on the Attiny

yes, yes, I do change them when I am uploading to ATTINY. sorry, I'm constantly playing with this code.

when it is uploaded to the ATTINY, I change it to:

const int RPIN = 0;
const int GPIN =1;
const int BPIN =4;
const int SWITCHPIN = 2;

I'll edit the original post to reflect this to avoid confusion.

Drc3p0:
some of the arrays have been removed due to exceeding the word limit for posts

Please upload the entire sketch as an attachment.

Drc3p0:
Any idea why this is not working properly on an ATTINY?
some of the arrays have been removed due to exceeding the word limit for posts

You understand that the ATtiny85 only has 512 bytes of RAM versus the 2048 bytes of the ATmega328.

How many arrays do you have and how large are they? It is very possible you've run out of RAM on the tiny.

Was just doing the maths... Just the arrays in the incomplete sketch are over 700 bytes.

sounds like that's the issue. I thought that when it compiled the sketch it would take the binary sketch size limit into consideration.

How do you calculate how many bytes a sketch will use? Do you know if there's a reference chart that states the size limitations of the different ATTINY's or ATmega's?

Drc3p0:
sounds like that's the issue. I thought that when it compiled the sketch it would take the binary sketch size limit into consideration.

There is a tool called avr-size that can help. In can tell the static space used. However, since RAM usage depends on the running program, it is really up to the programmer to track how much RAM their program is using and when.

Drc3p0:
Do you know if there's a reference chart that states the size limitations of the different ATTINY's or ATmega's?

The ATtiny datasheet.

Drc3p0:

[quote author=Jack Christensen link=topic=111386.msg836904#msg836904 date=1340489352]
Was just doing the maths... Just the arrays in the incomplete sketch are over 700 bytes.

sounds like that's the issue. I thought that when it compiled the sketch it would take the binary sketch size limit into consideration.
[/quote]

Program memory is separate from RAM, which is used for variables, stack, etc. The "Binary sketch size" reported by the IDE is the program size, not the amount of RAM used. The amount of RAM used cannot be determined at compile time, and indeed is not a single number, as memory can be allocated and released as the program runs. The program is stored in flash memory which is normally read-only during execution. RAM on the other hand, is read/write.

Drc3p0:
How do you calculate how many bytes a sketch will use? Do you know if there's a reference chart that states the size limitations of the different ATTINY's or ATmega's?

The datasheet is the reference, for example, Smart | Connected | Secure | Microchip Technology, memory sizes are listed on the first page.

How were you able to detect how much ram was being used in the sketch?

Drc3p0:

[quote author=Jack Christensen link=topic=111386.msg836904#msg836904 date=1340489352]
Was just doing the maths... Just the arrays in the incomplete sketch are over 700 bytes.

How were you able to detect how much ram was being used in the sketch?

[/quote]
Just from the source code. Booleans are one byte, ints are two. It's really not how much RAM the sketch is using, only a portion of it.

Do you think there would be any way to clean up this code enough that it would be able to fit on an attiny85v, or should I just opt for using a larger IC?

Drc3p0:
Do you think there would be any way to clean up this code enough that it would be able to fit on an attiny85v, or should I just opt for using a larger IC?

What does the code do? Maybe there’s a way to do the same thing without as many arrays.

It is an rgb light sequencer. There is a button that when pressed will cycle through the different patterns one at a time. It is meant as an addition to a photocell audio circuit, so the patterns you see directly influence the sounds heard. For each array, there is the r,g, and b array, and then the hArray controls how quickly it cycles through the rgbArrays. I tried to write it in this format so that it would be easier to edit the patterns.

Use PROGMEM to store the arrays in the program memory.

This should help you get started:
http://www.arduino.cc/playground/Main/PROGMEM

EDIT:

an example would be:
Declaration:

prog_uchar PROGMEM rArray3[] = {127,0,0,100,0,0,127,0,0,100,0,0,127,0,0,100,0,0}; //declare that array should be in program memory only.

Use:

analogWrite(RPIN,pgm_read_byte(&(rArray3[index])));

You can use:
prog_uchar instead of boolean
prog_uint16_t instead of int

to read back a prog_uchar it is:
boolean someVariable = pgm_read_byte(&(arrayName[index]));

to read back an prog_uint16_t it is:
int someVariable = pgm_read_word(&(arrayName[index]));

Without understanding the patterns and all in detail, it’s hard to say whether the array sizes could somehow be optimized. OTOH, the arrays could be stored in flash memory (using PROGMEM) instead of SRAM since they are read-only. (EDIT: Which I now see TCWORLD has already suggested. Brought to you by the Department of Redundancy Dept.)

BTW, what is intent of the statement indicated below? There are several similar statements throughout the code.

    if(lastTime + count < millis() && currentPlace == 0){ 
        hArray0[count];       // <--- WHAT DOES THIS DO?
        analogWrite(RPIN,rArray0[count]);
        analogWrite(GPIN,gArray0[count]);
        analogWrite(BPIN,bArray0[count]);
        lastTime = millis();
        count = count + 1;
        if(count >= countArray[currentPlace]){
            count = 0; 
        }
    }

Another question, in the code in the post above, what keeps count from overrunning the arrays, i.e. rArray has 18 elements, what ensures that count does not reach a value of 18 or greater?

If the patterns won't be changed during run time, you should store (and keep them stored) in PROGMEM.

I think the Flash Library ... Flash | Arduiniana ... works on ATtiny processors. It's a much easier way to access PROGMEM data.