Pages: [1] 2   Go Down
Author Topic: Programming ATTiny24 (and 44/84 of course)  (Read 4552 times)
0 Members and 1 Guest are viewing this topic.
London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino IDE is wonderfully easy to work with and is to be praised as highly as possible for opening up the world of embedded programming to a wider audience – even as a well-seasoned bit basher who relishes the difficult stuff, I love the simplicity of the Arduino IDE.

Of course, it is designed for the 328P, MEGA2560 and now SAM3XAE processors in the Uno, Mega and Due respectively. Note the absence of anything "tiny" in that list. It's easy to fix – some very clever folk have added the tiny variants to the IDE, all you need to do is download a new “boards.txt” file (and some other stuff) from https://github.com/damellis/attiny/archive/master.zip and put it in the right place.

Hmm...note the absence of anything “24” in that new list...add the following to the newly installed boards.txt file:

attiny24.name=ATtiny24 (internal 1 MHz clock)
attiny24.bootloader.low_fuses=0x62
attiny24.bootloader.high_fuses=0xdf
attiny24.bootloader.extended_fuses=0xff
attiny24.upload.maximum_size=2048
attiny24.build.mcu=attiny24
attiny24.build.f_cpu=1000000L
attiny24.build.core=arduino:arduino
attiny24.build.variant=tiny14

attiny24-8.name=ATtiny24 (internal 8 MHz clock)
attiny24-8.bootloader.low_fuses=0xe2
attiny24-8.bootloader.high_fuses=0xdf
attiny24-8.bootloader.extended_fuses=0xff
attiny24-8.upload.maximum_size=2048
attiny24-8.build.mcu=attiny24
attiny24-8.build.f_cpu=8000000L
attiny24-8.build.core=arduino:arduino
attiny24-8.build.variant=tiny14

attiny24-20.name=ATtiny24 (external 20 MHz clock)
attiny24-20.bootloader.low_fuses=0xfe
attiny24-20.bootloader.high_fuses=0xdf
attiny24-20.bootloader.extended_fuses=0xff
attiny24-20.upload.maximum_size=2048
attiny24-20.build.mcu=attiny24
attiny24-20.build.f_cpu=20000000L
attiny24-20.build.core=arduino:arduino
attiny24-20.build.variant=tiny14

Now you need a hardware programmer – something to plug the chip into so that the IDE can talk to it. Those funky Arduino people have even made that easy too – connect your Uno / Mega / Due and load the “Arduino as ISP” sketch. Next get a breadboard and wire the '24 (or 44 or 84) as follows:

ATTiny24
Name / Physical Pin

VCC / 1 => Arduino 5v
GND / 14 =>Arduino GND
RESET / 4 => Arduino pin 10
MOSI / 7 => Arduino pin 11
MISO / 8 => Arduino pin 12
SCK / 9 => Arduino pin 13

Some folk will tell you you need a 10uF capacitor between RESET and GND or various resistors here and there, but it works perfectly on my desk without either. Those 6 wires above are all you need.

Next select the appropriate board as the target, upload your sketch and off you go. It's really easy. Of course the 24 has severely restricted hardware compared with "proper" Arduinos, so don't expect a lot of the libraries to work (especially Serial!) but it's brilliant for bit-banging with more legs than either a 25/45/85 or a spider.

Should be able to see a video of it in action http://youtu.be/A-JMPvDHwXI or read more about it on my philipbowles.com/blog (when I get round to posting!)

Enjoy

Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi BareMetal, I think this is the topic I was looking for. I would like to use my ArduinoDUE as a ATtiny programmer, but all tutorial I found are about ArduinoUNO or other previous boards. I have some questions and I hope you can help me out :
  • Should I use 3.3V  Vcc instead of 5V?
  • May I use the same six wires connection as for ArduinoUNO?
  • Instead of using those 6 pins to implement SPI, may I use the already integrated SPI connector on DUE board?
  • Which IDE version are you using?
Thank you for your time
« Last Edit: March 26, 2013, 02:39:45 pm by goan » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi can you tell me where the additional files need to be placed?

I also added the data you posted to the boards.txt file but didn't seem to do anything.
Logged

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The changes won't take effect until you restart the ide.

Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're not already aware, y'all should check out:
http://code.google.com/p/arduino-tiny/
http://hlt.media.mit.edu/?p=1695

There are several others but those are the two I've tried.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

think this will work on an 85? i just picked up a couple of these at tayda ( http://www.taydaelectronics.com/ic-integrated-circuits/microcontrollers/attiny85-attiny85-20pu-8-bit-20mhz-microcontroller-ic.html )  I will try it on the morn. seems like these would be *perfect* for building a fleet of sensors with an extra kick, smart switches, automation/security features... 6 pins...hmm... 6 pins.. how to use them...

so glad there is already a known way to use these things, seems like at that price they open up all sorts of new possibilities.... muwahahahhaa 
Logged

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Los Angeles
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
Thank you for this thread.

I'm having trouble uploading to my ATTiny24A
I did everything listed.

I got this message (below)... But a different post said, " [...]don’t worry, the upload should have worked anyway."
Code:
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
^This says 'ATtiny85', but I made the hardware boards.txt changes above.


Are my pins right? What pins do I use on the ATTiny24A when it is programmed?
Please Help.
Here is my code:
Code:
int photoRPin = 0;
int LedOne = 8;
int minLight;
int maxLight;
int lightLevel;
int adjustedLightLevel;
int oldLightLevel;
int setPoint = 14;    // Trigger value
int piezzoPin = 6;  // set the speakerPin to 2 for buzzer


void setup() {
 
  //Setup the starting light level limits
  lightLevel=analogRead(photoRPin);
  minLight=lightLevel-70;
  maxLight=lightLevel;
  oldLightLevel=lightLevel;
  // declare pin 9 to be an output:
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop(){
   lightLevel=analogRead(photoRPin);
   delay(100);
 
  //auto-adjust the minimum and maximum limits in real time   
   if(minLight>lightLevel){
     minLight=lightLevel;
   }
   if(maxLight<lightLevel){
     maxLight=lightLevel;
   }
   
   //Map the light level to produce a result between 1 and 28.
   adjustedLightLevel = map(lightLevel, (minLight+20), (maxLight-20), 1, 28);
   adjustedLightLevel = constrain (adjustedLightLevel, 1,28);
   
   /*Only send a new value to the Serial Port if the
     adjustedLightLevel value changes.*/
   if(oldLightLevel==adjustedLightLevel){
     //do nothing if the old value and the new value are the same.
   }else{
     //Update the oldLightLevel value for the next round
     oldLightLevel=adjustedLightLevel;
   }
      if(adjustedLightLevel<setPoint) {
    analogWrite(8, 1200);   // turn the LED on (HIGH is the voltage level)
    analogWrite(6, 1000);   // turn the PIEZZO on (HIGH is the voltage level)
  delay(200);               // wait for a x-milliseconds
  digitalWrite(LedOne, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite (piezzoPin, LOW); // turn the PIEZZO off by making the voltage LOW

  delay(2000);               // wait for a second
  }
    else
{
    digitalWrite(LedOne, LOW);       // It is light again, turn the LED off.
    digitalWrite (piezzoPin, LOW);

}     
}

Thank you in advance,
-A


* Screen Shot 2014-01-14 at 12.08.18 AM.JPG (45.23 KB, 507x197 - viewed 21 times.)
Logged

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

avrdude is uploading to an 85. either your boards.txt is wrong or yoh selected wrong board. while you will get those errors and can ignore them, 85 code wont run on a 24 ...all the pin numbers will be wrong for a start. check boards.txt
Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Los Angeles
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the reply! I really appreciate it.

Maybe the modified code is incorrect?
I placed the 'hardware' directory in my Arduino Sketchbook Location it looks like this: /Users/Me/Documents/Arduino/hardware/attiny
Inside 'attiny' is the 'variants' directory and the boards.txt file that I modified (pasted my modified code on the end of page).

Selected: tools>board>ATtiny24 (external 20 mhz clock).



I am using: ATTINY24A-PU Desc.: 8-bit Microcontrollers - MCU 20MHz, 1.8-5.5V

Programmer is set to: 'Arduino as ISP'

Software version is 1.0.5
Using arduino UNO


Modified board.txt code:
Code:
attiny45.name=ATtiny45 (internal 1 MHz clock)
attiny45.bootloader.low_fuses=0x62
attiny45.bootloader.high_fuses=0xdf
attiny45.bootloader.extended_fuses=0xff
attiny45.upload.maximum_size=4096
attiny45.build.mcu=attiny45
attiny45.build.f_cpu=1000000L
attiny45.build.core=arduino:arduino
attiny45.build.variant=tiny8

attiny45-8.name=ATtiny45 (internal 8 MHz clock)
attiny45-8.bootloader.low_fuses=0xe2
attiny45-8.bootloader.high_fuses=0xdf
attiny45-8.bootloader.extended_fuses=0xff
attiny45-8.upload.maximum_size=4096
attiny45-8.build.mcu=attiny45
attiny45-8.build.f_cpu=8000000L
attiny45-8.build.core=arduino:arduino
attiny45-8.build.variant=tiny8

attiny45-20.name=ATtiny45 (external 20 MHz clock)
attiny45-20.bootloader.low_fuses=0xfe
attiny45-20.bootloader.high_fuses=0xdf
attiny45-20.bootloader.extended_fuses=0xff
attiny45-20.upload.maximum_size=4096
attiny45-20.build.mcu=attiny45
attiny45-20.build.f_cpu=20000000L
attiny45-20.build.core=arduino:arduino
attiny45-20.build.variant=tiny8

attiny85.name=ATtiny85 (internal 1 MHz clock)
attiny85.bootloader.low_fuses=0x62
attiny85.bootloader.high_fuses=0xdf
attiny85.bootloader.extended_fuses=0xff
attiny85.upload.maximum_size=8192
attiny85.build.mcu=attiny85
attiny85.build.f_cpu=1000000L
attiny85.build.core=arduino:arduino
attiny85.build.variant=tiny8

attiny85-8.name=ATtiny85 (internal 8 MHz clock)
attiny85-8.bootloader.low_fuses=0xe2
attiny85-8.bootloader.high_fuses=0xdf
attiny85-8.bootloader.extended_fuses=0xff
attiny85-8.upload.maximum_size=8192
attiny85-8.build.mcu=attiny85
attiny85-8.build.f_cpu=8000000L
attiny85-8.build.core=arduino:arduino
attiny85-8.build.variant=tiny8

attiny85-20.name=ATtiny85 (external 20 MHz clock)
attiny85-20.bootloader.low_fuses=0xfe
attiny85-20.bootloader.high_fuses=0xdf
attiny85-20.bootloader.extended_fuses=0xff
attiny85-20.upload.maximum_size=8192
attiny85-20.build.mcu=attiny85
attiny85-20.build.f_cpu=20000000L
attiny85-20.build.core=arduino:arduino
attiny85-20.build.variant=tiny8

attiny44.name=ATtiny44 (internal 1 MHz clock)
attiny44.bootloader.low_fuses=0x62
attiny44.bootloader.high_fuses=0xdf
attiny44.bootloader.extended_fuses=0xff
attiny44.upload.maximum_size=4096
attiny44.build.mcu=attiny44
attiny44.build.f_cpu=1000000L
attiny44.build.core=arduino:arduino
attiny44.build.variant=tiny14

attiny44-8.name=ATtiny44 (internal 8 MHz clock)
attiny44-8.bootloader.low_fuses=0xe2
attiny44-8.bootloader.high_fuses=0xdf
attiny44-8.bootloader.extended_fuses=0xff
attiny44-8.upload.maximum_size=4096
attiny44-8.build.mcu=attiny44
attiny44-8.build.f_cpu=8000000L
attiny44-8.build.core=arduino:arduino
attiny44-8.build.variant=tiny14

attiny44-20.name=ATtiny44 (external 20 MHz clock)
attiny44-20.bootloader.low_fuses=0xfe
attiny44-20.bootloader.high_fuses=0xdf
attiny44-20.bootloader.extended_fuses=0xff
attiny44-20.upload.maximum_size=4096
attiny44-20.build.mcu=attiny44
attiny44-20.build.f_cpu=20000000L
attiny44-20.build.core=arduino:arduino
attiny44-20.build.variant=tiny14

attiny84.name=ATtiny84 (internal 1 MHz clock)
attiny84.bootloader.low_fuses=0x62
attiny84.bootloader.high_fuses=0xdf
attiny84.bootloader.extended_fuses=0xff
attiny84.upload.maximum_size=8192
attiny84.build.mcu=attiny84
attiny84.build.f_cpu=1000000L
attiny84.build.core=arduino:arduino
attiny84.build.variant=tiny14

attiny84-8.name=ATtiny84 (internal 8 MHz clock)
attiny84-8.bootloader.low_fuses=0xe2
attiny84-8.bootloader.high_fuses=0xdf
attiny84-8.bootloader.extended_fuses=0xff
attiny84-8.upload.maximum_size=8192
attiny84-8.build.mcu=attiny84
attiny84-8.build.f_cpu=8000000L
attiny84-8.build.core=arduino:arduino
attiny84-8.build.variant=tiny14

attiny84-20.name=ATtiny84 (external 20 MHz clock)
attiny84-20.bootloader.low_fuses=0xfe
attiny84-20.bootloader.high_fuses=0xdf
attiny84-20.bootloader.extended_fuses=0xff
attiny84-20.upload.maximum_size=8192
attiny84-20.build.mcu=attiny84
attiny84-20.build.f_cpu=20000000L
attiny84-20.build.core=arduino:arduino
attiny84-20.build.variant=tiny14

attiny24.name=ATtiny24 (internal 1 MHz clock)
attiny24.bootloader.low_fuses=0x62
attiny24.bootloader.high_fuses=0xdf
attiny24.bootloader.extended_fuses=0xff
attiny24.upload.maximum_size=2048
attiny24.build.mcu=attiny24
attiny24.build.f_cpu=1000000L
attiny24.build.core=arduino:arduino
attiny24.build.variant=tiny14

attiny24-8.name=ATtiny24 (internal 8 MHz clock)
attiny24-8.bootloader.low_fuses=0xe2
attiny24-8.bootloader.high_fuses=0xdf
attiny24-8.bootloader.extended_fuses=0xff
attiny24-8.upload.maximum_size=2048
attiny24-8.build.mcu=attiny24
attiny24-8.build.f_cpu=8000000L
attiny24-8.build.core=arduino:arduino
attiny24-8.build.variant=tiny14

attiny24-20.name=ATtiny24 (external 20 MHz clock)
attiny24-20.bootloader.low_fuses=0xfe
attiny24-20.bootloader.high_fuses=0xdf
attiny24-20.bootloader.extended_fuses=0xff
attiny24-20.upload.maximum_size=2048
attiny24-20.build.mcu=attiny24
attiny24-20.build.f_cpu=20000000L
attiny24-20.build.core=arduino:arduino
attiny24-20.build.variant=tiny14


Thanks,
aldo
Logged

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Los Angeles
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

removed all other configs in the 'boards.txt' except for:

Code:
attiny24-20.name=ATtiny24 (external 20 MHz clock)
attiny24-20.bootloader.low_fuses=0xfe
attiny24-20.bootloader.high_fuses=0xdf
attiny24-20.bootloader.extended_fuses=0xff
attiny24-20.upload.maximum_size=2048
attiny24-20.build.mcu=attiny24
attiny24-20.build.f_cpu=20000000L
attiny24-20.build.core=arduino:arduino
attiny24-20.build.variant=tiny14

now I get:
Code:
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny24
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny24

no cigar. smiley-confuse
Logged

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

good. we have lost the tiny85 confusion. next question is...do you have an external 20mhz crystal in your tiny24 circuit? if you dont, the fuse settings in boards.txt will set the mcu to expext an external crystal for its timing. if there is none, I honestly dont know wwhat will happen, but it wont be good. the -20 on th chip doesnt mean that what the chip rjns at, its the max that it CAN run at, but anythkng over 8mhz will require external clocking.

out of the box, they are set to 8mhz internal clock with clkdiv/8 ie an internal prescaler that divides the raw clock by 8 giving an actual speed of 1mhz. try it again with the 1mhz internal clock option. the ide takes the correct fuse settkngs and will set the mcu back to 1mhz...if you copied them correctly!

if it still doesnt work I will look at your code in detail, but it aint going to work set to 20mhz and no external clocking.
Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ps that 20mhz even with externak xtal is only possible with vcc at 5v. if u have 3.3v you wilonly get to 8 or 10...cant remember whic, you need to check the datasheet. anyway, 1mhz is plenty for anyone unless u are building a space shuttle, life support system or massive hadron collider. try everything at 1mhz till you get more of a handle on things or jntil you have circuit that needs some sort of respone in less than a millionth of a second.....none of mine does!
Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Los Angeles
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

RIght on. Thank you so much with all this help. I'm a student and this really really helps.

I missed the 'external' part!
I went ahead and put these lines into 'board.txt' in:

Code:
attiny24-8.name=ATtiny24 (internal 8 MHz clock)
attiny24-8.bootloader.low_fuses=0xe2
attiny24-8.bootloader.high_fuses=0xdf
attiny24-8.bootloader.extended_fuses=0xff
attiny24-8.upload.maximum_size=2048
attiny24-8.build.mcu=attiny24
attiny24-8.build.f_cpu=8000000L
attiny24-8.build.core=arduino:arduino
attiny24-8.build.variant=tiny14

Seemed to upload fine; that is if the PAGEL and BS2 message can be ignored:
Code:
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny24
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny24

I must be doing something wrong with how I am wiring the pins on the ATtiny24.
The sketch is working fine on the UNO.
It reads the ambient light with the Photoresistor... when it is set with a bright light and back to ambient it will blink an LED and sound a piezo when the Photoresistor is shined on again.


I am using
pin 13 - Photoresistor
pin 10 - Piezo
pin 11 - Led

I revised pins this is the new code:
Code:

int photoRPin = 0;
int LedOne = 2;
int minLight;
int maxLight;
int lightLevel;
int adjustedLightLevel;
int oldLightLevel;
int setPoint = 14;    // Trigger value
int piezzoPin = 3;  // set the piezo pin


void setup() {
 
  //Setup the starting light level limits
  lightLevel=analogRead(photoRPin);
  minLight=lightLevel-70;
  maxLight=lightLevel;
  oldLightLevel=lightLevel;
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(0, INPUT);
}

void loop(){
   lightLevel=analogRead(photoRPin);
   delay(100);
 
  //auto-adjust the minimum and maximum limits in real time   
   if(minLight>lightLevel){
     minLight=lightLevel;
   }
   if(maxLight<lightLevel){
     maxLight=lightLevel;
   }
   
   //Map the light level to produce a result between 1 and 28.
   adjustedLightLevel = map(lightLevel, (minLight+20), (maxLight-20), 1, 28);
   adjustedLightLevel = constrain (adjustedLightLevel, 1,28);
   
   /*Only send a new value if the
     adjustedLightLevel value changes.*/
   if(oldLightLevel==adjustedLightLevel){
     //do nothing if the old value and the new value are the same.
   }else{
     //Update the oldLightLevel value for the next round
     oldLightLevel=adjustedLightLevel;
   }
      if(adjustedLightLevel<setPoint) {
    analogWrite(2, 1200);   // turn the LED on (HIGH is the voltage level)
    analogWrite(3, 1000);   // turn the PIEZZO on (HIGH is the voltage level)
  delay(200);               // wait for a x-milliseconds
  digitalWrite(LedOne, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite (piezzoPin, LOW); // turn the PIEZZO off by making the voltage LOW

  delay(2000);               // wait for a second
  }
    else
{
    digitalWrite(LedOne, LOW);       // It is light again, turn the LED off.
    digitalWrite (piezzoPin, LOW);

}     
}

Thanks,
aldo
« Last Edit: January 14, 2014, 09:44:14 pm by aldo_oner » Logged

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

petty sure your pin numbers are wrong. you need to check the datasheet. I dont have on right now as im on the train but will check tonight and reply. aalso not sure why you reserve an int for the pin number then use an absolute value in pinmode...the usual way is #define mypin 5 or whatever and then use mypin everywhere. this method saves space as it doesnt reserve memory for the int, its just a textual shorthand for the compiler..ie replace mypin with 5 before compiling.
Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

London, UK
Offline Offline
Jr. Member
**
Karma: 9
Posts: 86
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. This is from pins_arduino.h under the tiny14 subdirectory:
Code:
// ATMEL ATTINY84 / ARDUINO
//
//                           +-\/-+
//                     VCC  1|    |14  GND
//             (D 10)  PB0  2|    |13  AREF (D  0)
//             (D  9)  PB1  3|    |12  PA1  (D  1)
//                     PB3  4|    |11  PA2  (D  2)
//  PWM  INT0  (D  8)  PB2  5|    |10  PA3  (D  3)
//  PWM        (D  7)  PA7  6|    |9   PA4  (D  4)
//  PWM        (D  6)  PA6  7|    |8   PA5  (D  5)        PWM
//     
The D numbers are the arduino conceptual digital pin numbers, so physical pin 11 on the chip is D2 for example
So,  based on your last:
pin 13 - Photoresistor
pin 10 - Piezo
pin 11 - Led
your code should look like

Code:
#define PHOTO 0 // Physical pin 13 = tiny24 pin 0
#define PIEZO 3 // see above
#define LED 2 // same
...
// later
  pinMode(PIEZO, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(PHOTO, INPUT);
// but you really should do this BEFORE calling any pin function e.g. analogRead!!!


The main problem you have though is your analogue writes. analogWrite uses PWM functionality on the pin. From the arduino docs:

"On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. "

Those datasheets will show which pins have PWM functionality. On the tiny24 (see above) its chip pins 5,6,7,8 ie digital pins 5,6,7,8 (altho confusingly not in the same order!)

So, if you have to pulse PIEZO and LED, they need to be on one of those pins. On the UNO through what a poster on here calls "dumb luck"  smiley  10 and 11 support PWM so your sketch works (though you were lucky that calling analogRead before setting the pinmode also happened to work...the chip boots up with pins as inputs!)

so:
#define PIEZO 5
#define LED 6
 should sort that. Next we go back to the doc for analogWrite:

"Parameters

pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on).
"

you say:
    analogWrite(2, 1200);   // turn the LED on (HIGH is the voltage level)
    analogWrite(3, 1000);   // turn the PIEZZO on (HIGH is the voltage level)

I'm not sure why you chose those values, and I dont want to go into 2s-complements maths on 8-bit values, but that code just aint going to do what you think it does. E.g. 1200 is 0x4B0 since you are stuffing into 8 bits, you lose the 4, giving 0xB0 which is 176 if its treated as unsigned, or -80 if its a signed value. Neither of them are close to what you thought you meant!

176/255 = 69% full brightnes (or squeak volume) so (luckily) your dodgy code will actually cause what LOOKS like a flash and a squeak. If all you want to do is turn the LED on or off, use digitalWrite (also consider the tone library for squeaking the buzzer)

To make your code clean, you need to map your value to 0-255 and also say:
analogWrite(PIEZO....
digitalWrite(LED...
for portability and ease of modification / rewiring...all you do if you move a wire is change the #define and the rest of the code "just works" without search and replace of all "2," for e.g. "11," which can change of a lot of wrong stuff accidentally if you arent careful!

A final note, UPPERCASE is usually used for such constants (just a style thing) and analog pins have special names in the pins_arduin.h file, e.g. analog pin 0 is A0.

Using A0 instead of 0 makes your code portable, ie on a UNO, A0 will be mapped to the correct pin for you, and its one less thing to change. if the framework had similar definitions for e.g. PWM pins such as PWM1, PWM2 and digital pins like DIGITAL1, DIGITAL2 etc, we wouldn't be having this conversation at all, as it just would have worked on any variant if you had said:

#define PIEZO PWM1
#define LED DIGITAL1
#define PHOTO A0

.and put the wires in the right holes (aprt from the 1200 vs 255 discussion of course!)

Hope this helps

Phil







Logged

See more 'duino / lego / robotics / electronic stuff on my blog http://philipbowles.com/blog

Pages: [1] 2   Go Up
Jump to: