Author Topic: An Attiny problem, causing big headache!  (Read 2922 times)

lokey123

  • Newbie
  • *
  • Posts: 2
An Attiny problem, causing big headache!
« on: May 02, 2016, 10:39:40 pm »
Hi all,

I am new to programming the arduino code, but have managed to get what I wanted to do working on an Uno..yay for me!..

But sadly I am unable to get it to work on a digispark attiny 16hz 5v board..

The program should detect EMF or any electricity around an antenna and sound an alarm,it works on uno but I get nothing from the Attiny, ..here is the code..

Code: [Select]
#define sample 300                    //this is how many samples the device takes per reading
                                                        //more information for #define http://arduino.cc/en/Reference/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 http://arduino.cc/en/Reference/Array


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


void setup() {
 
  Serial.begin(9600);
  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 http://arduino.cc/en/Reference/For
}                                                               //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 += http://arduino.cc/en/Reference/IncrementCompound
 Serial.println(val);


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 http://arduino.cc/en/Reference/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 http://arduino.cc/en/Reference/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 .

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: An Attiny problem, causing big headache!
« Reply #1 on: May 03, 2016, 09:27:59 am »
If you are talking about the Digispark, have you seen the information provided here re: analog inputs:
http://digistump.com/wiki/digispark/tutorials/basics

Look at this section and see if it helps:
Code: [Select]
Analog Read:

int sensorValue = 0;

void setup() {
    //You need not set pin mode for analogRead - though if you have set the pin to
    //output and later want to read from it then you need to set pinMode(0,INPUT);
    //where 0 is the physical pin number not the analog input number.
    //
    //See below for the proper pinMode statement to go with each analog read.
}

void loop() {
    // The analog pins are referenced by their analog port number, not their pin
    //number and are as follows:

    sensorValue = analogRead(1); //Read P2
    //To set to input: pinMode(2, INPUT);
    //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.

    //sensorValue = analogRead(2); //Read P4
    //To set to input: pinMode(4, INPUT);
    //THIS IS P4, P4 is analog input 2, so when you are using analog read, you refer to it as 2.

    //sensorValue = analogRead(3); //Read P3
    //To set to input: pinMode(3, INPUT);
    //THIS IS P3, P3 is analog input 3, so when you are using analog read, you refer to it as 3.

    //sensorValue = analogRead(0); //Read P5
    //To set to input: pinMode(5, INPUT);
    //THIS IS P5, P5 is analog input 0, so when you are using analog read, you refer to it as 0.
}

lokey123

  • Newbie
  • *
  • Posts: 2
Re: An Attiny problem, causing big headache!
« Reply #2 on: May 03, 2016, 10:37:55 am »
Yes that is what I looked at, also someone pointed out to me that my sketch took 668 dynamic bytes of memory , i changed things just a little and now it is within the 512 limit,,but sadly it still does not work for me.