An Attiny problem, causing big headache!

Hi again all,

Sorry if this falls in the wrong place but it is a sensor related issue I think :o …

I have a small sketch that detects EMF energy/electricity in the air given off by appliances,
It works super on my Uno and pro mini…but not my Attiny Digispark 16hz 5v board.

I followed all the instructions regarding the pin outputs needing to be different for the analouge read and pin mode output, but still it does nothing I expect it to.
here is the code could someone help please?..

[code]#define sample 300                    //this is how many samples the device takes per reading
                                                        //more information for #define 
int inPin = A3;                                  //analog 5
float val;                                         //where to store info from analog 5
int pin12 = 1;                              //output of red led
int pin5 = 5;
int array1[sample];                      //creates an array with number of elements equal to "sample" 
                                                       //more information about arrays

unsigned long averaging;         //the program uses this variable to store the sum of each array it makes

void setup() {
  pinMode (5,OUTPUT);


void loop() {
for(int i = 0; i < sample; i++){              //this code tells the program to fill each element in the array we made with
   array1[i] = analogRead(inPin);       //information from the antenna wire coming out of the Arduino
   averaging += array1[i];                      //more information about for loops
}                                                               //the averaging line is simply saying: add averaging to whatever is in array position i
                                                                //averaging += array[i] is the same as averaging = averaging + array[i]
                                                                //for more information about +=

val = averaging / sample;                   //here the program takes the sum of all numbers in array1, and divides by the number of elements "sample"

val = constrain(val, 0, 100);               //this constrains the variable value to between two numbers 0 and 100                                       
    val = map(val, 0, 100, 0, 255);      //for more information about constrain   
    analogWrite( pin12, val);
    analogWrite( pin5, val);//the map statement tells the program to map out 0-100 to 0-255, 255 is 
                                                              //the threashold of analogWrite for more information about map
    averaging = 0;                                 //this line of code sets averaging back to zero so it can be used again


Hope someone can help me?, sorry if this is in the wrong place on the Forum .[/code]

You use 600+ bytes of RAM. tiny85 got 512

Oh gosh, yes but how could i make it less than 512?,

If you're going to constrain val to 100, a byte variable would hold it instead of a 2 byte int, try making

int array1[sample];
byte array1[sample];

that would save 300 bytes.

Yes, thank you , so int is something to look out for!, could byte be substituted for cons int?

As long as const is less than 256. Also you may need to change:

array1[i] = analogRead(inPin);


array1[i] = lowByte(analogRead(inPin));

You dont need the array to do the 300 readings/average. ->Accumulate while reading