Show Posts
Pages: 1 2 [3] 4 5 ... 115
31  Using Arduino / Programming Questions / Re: prototype function error on: June 30, 2014, 03:20:11 am
Code:
stringoutput=(stringoutput+letters[outputdata]);
I think above code line is also wrong, because we are assigning char stringoutput[] to stringoutput*, which is incompatible.

Correct, and to add to the problem, stringoutput[] has been declared as a pointer to an array of length 1 - the initialiser sets the length if the length is not specified and in this case the initialiser is an empty string ("") which is just a null terminator.

You can't just concatenate strings like that without using realloc()/malloc()/dealloc() to change the amount of room in the array. Alternatively you can declare an array with enough space for your longest message to begin with and then use strcat() to append characters.

On top of that, you really shouldn't be putting all that code in the header. Put only a prototype in the header and add the rest of the code into a separate .cpp file which #includes the header.
32  Using Arduino / Programming Questions / Re: Must have more files! (Datalogging to SD card) on: June 26, 2014, 03:58:22 pm
Code:
SREG = SREG | B10000000; // Enable gobal interrupts. They should
    // already be enabled but I like to do this out of good measure.
A bit unrelated, but still good for you to know. That line of code is equivalent to:
Code:
sei();


EDIT:
With regards to naming the files, the simplest way would be to store a value in the EEPROM of the number of the last file used. Then next time a file is created, simply name it with that number+1 (and store the number to the EEPROM). That way you always know which filename to use, even if files are removed from the SD card.
33  Using Arduino / Programming Questions / Re: abs() becomes negative? on: June 26, 2014, 10:45:00 am
Only on the Due/Arm boards.

For 8bit Arduino boards:
a byte = 0 to 255
a char = -128 to 127
an int = -32768 to 32767
a word = a short = unsigned int = 0 to 65536


If you want to know the size of something, just run this sketch:
Code:
void setup (){
  Serial.begin(9600);
  Serial.print(sizeof(int));// change the type inside sizeof() to any of your choosing, e.g. sizeof(byte)
}
void loop(){}
34  Using Arduino / Programming Questions / Re: abs() becomes negative? on: June 26, 2014, 10:03:52 am
You've declared 'lastShot' as a signed 16bit integer which means it can take values of -32768 to 32767.

If you try and store a value of say 32768 (one more than the highest positive), in future calculations it will be seen as -32768 as the two numbers have the same 16bit binary representation.

Either declare last shot as an 'unsigned int' or use a larger variable type.
35  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 22, 2014, 03:36:52 pm
There shouldn't be a pins_arduino.h file in the "hardware\arduino\cores\arduino" directory. If there is, then that one needs deleting!
All of the pins_arduino.h files should be in separate folders in the \hardware\arduino\variants\ directory.

This line in the boards.txt file identifies the folder name in the variants directory from which the header file should be found:
Code:
atmega162.build.variant=atmega162
But if there is one in the wrong place, like in the cores folder, that one will obscure the correct one.
36  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: June 22, 2014, 01:06:37 pm
Quote
8MHz implies the internal oscillator
No.

That is not what the OP said.

However if you want to make up a question and then answer it you are spot on.

Sure about that one?
when all Atmegas and Attnyies have a 8 Mhz internal clock?
37  Using Arduino / Microcontrollers / Re: Why 16 Mhz clock??? on: June 22, 2014, 01:03:45 pm
Everybody have told me that 16 Mhz is better if you working with time precision like a clock
That's not true.

I think both of you missed the point of why 16MHz is better than 8MHz for precision timing...

It has nothing to do with the frequency per-say, but what is generating it. 16MHz implies a 16MHz clock crystal which is nice and accurate (or to a lesser extent a resonator, which is a bit less accurate). 8MHz implies the internal oscillator (as stated in the original post), which is highly inaccurate unless tuned and even then it is very unstable when temperature and voltage fluctuations are added in.

Of course if you used an 8MHz crystal, then yes, the only real difference is that one allows you to do twice as much in the same time. Though if you are using timers, it lowers the maximum frequency of the timer.
38  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 22, 2014, 01:00:05 pm
Just for the sake of argument, add the following line to the top of the pins_arduino.h file for the Atmega162:

Code:
#error If you are seeing this, the correct pins_arduino.h file is included
39  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 22, 2014, 02:26:42 am
You probably have the wrong board selected:
This is in your .elf:
Code:
00000070 <port_to_mode_PGM>:
  70: 00 00 00 00 37 00 34 00 31 00                       ....7.4.1.

0000007a <port_to_output_PGM>:
  7a: 00 00 00 00 38 00 35 00 32 00                       ....8.5.2.

00000084 <port_to_input_PGM>:
  84: 00 00 00 00 36 00 33 00 30 00                       ....6.3.0.

This is what it should be:
Code:
00000070 <port_to_mode_PGM>:
  70: 00 00 3a 00 37 00 34 00 31 00 26 00                 ..:.7.4.1.&.

0000007c <port_to_output_PGM>:
  7c: 00 00 3b 00 38 00 35 00 32 00 27 00                 ..;.8.5.2.'.

00000088 <port_to_input_PGM>:
  88: 00 00 39 00 36 00 33 00 30 00 25 00                 ..9.6.3.0.%.
40  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 04:13:07 pm
You should see a line like this at the end of the verbose output:
Code:
C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.elf C:\Users\Thomas\AppData\Local\Temp\build1687262069962928034.tmp\sketch_jun21a.cpp.hex
Binary sketch size: 106 bytes (of a 1,472 byte maximum)
The .elf file named there is the one.
41  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 01:06:23 pm
Its possible. If you compile the sketch with the 'Verbose output' option in the IDE preferences, it will show you the directory in which the compiled sketch is placed. In that folder there will be a ".elf" file. Could you upload that here.
42  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 11:57:58 am
Having read that instructable, it seems it is just the code I wrote a couple of years ago. I have no problem with it. The mapping is nothing really like the ATMega328, they are completely different microcontrollers. I based the numbering scheme on what was easiest to remember for the specific application that I was using at the time.
43  Using Arduino / Microcontrollers / Re: ATmega162 - Need to correctly define pins on: June 21, 2014, 11:46:54 am
The mapping is in the pins_arduino.h file, you can change them in there if you want.
Code:
// ATMEL ATMEGA162 / ARDUINO
//
//                  +-\/-+
// PWM  (D 8) PB0  1|    |40  VCC
// PWM  (D 9) PB1  2|    |39  PA0 (D14)
//      (D19) PB2  3|    |38  PA1 (D15)
//      (D18) PB3  4|    |37  PA2 (D16)
// PWM  (D10) PB4  5|    |36  PA3 (D17)
//      (D11) PB5  6|    |35  PA4 (D31)
//      (D12) PB6  7|    |34  PA5 (D32)
//      (D13) PB7  8|    |33  PA6 (D33)
//            RST  9|    |32  PA7 (D34)
//      (D 0) PD0 10|    |31  PE0 (D28)
//      (D 1) PD1 11|    |30  PE1 (D29)
//      (D 2) PD2 12|    |29  PE2 (D30) PWM
//      (D 3) PD3 13|    |28  PC7 (D27)
// PWM  (D 5) PD4 14|    |27  PC6 (D26)
// PWM  (D 6) PD5 15|    |26  PC5 (D25)
//      (D 4) PD6 16|    |25  PC4 (D24)
//      (D 7) PD7 17|    |24  PC3 (D23)
//            XT2 18|    |23  PC2 (D22)
//            XT1 19|    |22  PC1 (D21)
//            GND 20|    |21  PC0 (D20)
//                  +----+
44  Using Arduino / Programming Questions / Re: can I call a function to replace a for statement? on: June 19, 2014, 05:36:56 pm
This would work:
Code:

#define count for (int i=0;i<4;i++)
    ...

     count
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

But it ranks highly on the list of the most AWFUL ways of coding anything. Stick to writing out the for loop.

(EDIT: Krupski beat me to it.)


What you could also do is this:
Code:
/*
3 shift registers in, 2 out.
 
 Drive 2 banks of 8 LED's with 2 banks of 8 switches (with 1 extra input bank added for future expansion).
 
 The circuit:
  Connected to 3 cd 4021's with the inputs via switches to 5v and with 10k resistors to 0v.
 Connected to 2 74LS595's with their outputs connected via resistors and LED's to 0v.
 
  Created 09.11.13
  By matelot
  Modified ------------
  By -----------
 set up CD4021 with
 pin1 - i/p 1      pin16 - vcc
 pin2 - n/c        pin15 - i/p 2     
 pin3 - data in    pin14 - i/p 3
 pin4 - i/p 5      pin13 - i/p 4
 pin5 - i/p 6      pin12 - n/c
 pin6 - i/p 7      pin11 - n/c
 pin7 - i/p 8      pin10 - clock
 pin8 - gnd         pin9  - ser/par switch (latch)
 bias inputs with resistors to gnd and put a switch on each input to vcc
 */
//connected to 2 cd4021's with the pin 11 on the first connected to pin 3 on the second.
const int latchPinin  =  5; //4021 pin 9
const int dataPinin   =  6;  //4021 pin 3
const int clockPinin  =  7; //4021 pin 10
/*
set up 74ls595 with
 pin1 - o/p 2      pin16 - vcc
 pin2 - o/p 3      pin15 - o/p 1     
 pin3 - o/p 4      pin14 - data i/p
 pin4 - o/p 5      pin13 - gnd
 pin5 - o/p 6      pin12 - latch
 pin6 - o/p 7      pin11 - clock
 pin7 - o/p 8      pin10 - vcc
 pin8 - gnd        pin9  - to pin 14 of next register
 connected to 2 47LS595's with the pin 9 on the first connected to pin 12 on the second.
 myinput bits are 1.WO1 2.WO2 3.WO3 4.WO4
 myoutput bits are 1.WO1red
 */
const int dataPinout  =  8;   //595 pin 14
const int latchPinout =  9;  //595 pin 11
const int clockPinout = 10; //595 pin 12
int myinput;
int myoutput = 136;
int myoutput2;
int a;
int i;
int shift1[] = {
  0,1,2,3};
int shift2[] = {
  4,5,6,7};
int shift3[] = {
  1,0,0,0};
int shift4[] = {
  0,1,0,0};
int shift5[] = {
  0,1,1,0};
int shift6[] = {
  0,0,0,1};

void setup()
{
  Serial.begin(9600);
  pinMode(dataPinout,  OUTPUT);
  pinMode(latchPinout, OUTPUT);
  pinMode(clockPinout, OUTPUT);
  pinMode(latchPinin,  OUTPUT);
  pinMode(clockPinin,  OUTPUT);
  pinMode(dataPinin,    INPUT);
}

void count(void (*fptr)(int, int), int* a, int* b)
{
  for (i=0;i<4;i++) {
    fptr(a[i],b[i]);
  }
}
void loop()
{
  delayMicroseconds  (20);
  digitalWrite       (clockPinin,HIGH);/*puts the clockpin high so the first
   bit is read before the register is clocked on*/
  digitalWrite       (latchPinin,1);  //set latch to 1 to collect parallel data
  delayMicroseconds  (20);                //pause to let digitalWrite finish
  digitalWrite       (latchPinin,0); //set latch to 0 to transmit data serially
  digitalWrite       (latchPinout, LOW);
  digitalWrite       (clockPinin,HIGH);
  shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput); //Send data
  //   shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput2); //Send data   
  digitalWrite       (latchPinout, HIGH);

  Serial.print    ("   ");       //sets a loop so the input can be bitread
  for (a=0;a<8;a++)              //so that leading 0's line up in the monitor
      Serial.print    (bitRead (myinput,(7-a))); //bit read the contents of input
  Serial.print    ("  ");
  Serial.println   (myinput);//print myinput in decimal

  myinput  = shiftIn (dataPinin, clockPinin, MSBFIRST);//read first register
  /*hct4021 register 1    bit 0 WO1       bit 1 WO2       bit 2 WO3        bit 3 WO4
   (myinput)     bit 4 TS12c     bit 5 TS12d     bit 6            bit 7
   74ls595   register 1    bit 0 WO1 red   bit 1 WO1 yel   bit 2 WO1 yel2   bit 3 WO1 grn
   (myoutput)    bit 4 WO2 red   bit 5 WO2 yel   bit 6 WO2 yel2   bit 7 WO2 grn
   74ls595   register 2    bit 0 WO3 red   bit 1 WO3 yel   bit 2 WO3 grn    bit 3 WO4 red
   (myoutput2)   bit 4 WO4 grn   bit 5 21  red   bit 6 21  yel    bit 7 21  grn
   
   */
  if (bitRead(myinput,0) == 1)//read bit WO1
  {
    count(&output,shift1,shift3);//WO1 to red
  }
  if (bitRead(myinput,1) == 1)//read bit WO2
  {
    for (i=0;i<4;i++)
    {
      output(shift1[i],shift4[i]);//WO1 to yel
      output(shift2[i],shift3[i]);//WO2 to red
    }
  }
  if (bitRead(myinput,2) == 1)//read bit WO3
  {
    if (bitRead(myoutput,1) == 1)//is WO1 yel
    {
      count(&output,shift1,shift5);//WO1 to yel yel
    }
    count(output,shift2,shift4);//WO2 to yel
    count(output2,shift1,shift3);//WO3 to red
  }
  if (bitRead(myinput,3) == 1)//read bit WO4
  {
    if (bitRead(myoutput,2) == 1)//is WO1 yel yel
    {     
      count(&output,shift1,shift6);//WO1 to grn
    }
    if (bitRead(myoutput,5) == 1)//is WO2 yel
    {
      count(&output,shift2,shift5);//WO2 to yel yel
    }
    count(&output2,shift1,shift4);//WO3 to yel
    count(&output2,shift1,shift6);//WO4 red
  }

  //     for (i=0;i<4;i++)
  //     {
  //       output(shift2[i],shift5[i]);//WO2 to yel yel
  //       output(shift1[i],shift4[i]);
  //     }

}
void output(int first,int second)//do void output using the first array in the
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);
  //    bitWrite(myoutput,1,0);//put WO1 yel  low
  //    bitWrite(myoutput,2,0);//put WO1 yel2 low
  //    bitWrite(myoutput,3,0);//put WO1 grn  low     
}
void output2(int first,int second)//do void output using the first array in the
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput2,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);     
}
Though that would be far far far less efficient that just using a for loop properly.
45  Using Arduino / Microcontrollers / Re: [Arduino UNO] How to disable pullup resistor ?? on: June 17, 2014, 07:59:45 pm
If you want to disable ALL internal pullups:
Code:
MCUSR |= _BV(PUD);
To enable again:
Code:
MCUSR &= ~_BV(PUD);


If they are external pullups, instead of connecting them to +5v, connect them all to one of the Arduino pins and use digitalWrite() to control them.
Pages: 1 2 [3] 4 5 ... 115