Go Down

Topic: Not funny bug (Read 2924 times) previous topic - next topic

apfelmann

Jul 05, 2011, 01:29 am Last Edit: Jul 05, 2011, 01:49 am by apfelmann Reason: 1
Hi Guys,

I'm getting crazy..

The Blink-Example doesn't work: Either the led shines all the time or it is off.
But when I put
> Serial.begin(9600);
in setup() it works.

Now I compile my sketches on my own (avr-gcc and send with avrdude) and the Blink-Example works.

I made a new sketch with my Wii-Nunchuk, so I have to use the Wire.h library.
Now my Serial-connection is buggy:

Code: [Select]

Serial.print("h"); // works

-----------------

Serial.println("h");
Serial.println("i"); // Doesn't work

------------------

Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");
Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");                          // works

-----------------

Serial.print("h"); Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");
Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");Serial.print("h");  // doesn't work... (?)



Doesn't work means, that the programm won't start (my LEDs don't shine) and there's no serial connection (blinking light).

I use the Arduino Uno and my distro is Gentoo

Any ideas?
Thanks for your help!

cmiyc

Use "code" not "quote" to post code.

For the best results, post the entire code you are using that does and doesn't work.  Snippets are difficult (to impossible) to debug.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

apfelmann

#2
Jul 05, 2011, 02:03 am Last Edit: Jul 05, 2011, 08:19 pm by apfelmann Reason: 1
Code: [Select]

#include <Wire.h>

struct nunchuk {
byte id;
byte joy_x, joy_y;
int accu_x, accu_y, accu_z;
bool z, c;
};

struct nunchuk *nun;

void nun_update(){
byte h;
Wire.requestFrom(0x52, 6);
for( int i=0; i<6 && Wire.available(); i++) {
h = (Wire.receive() ^ 0x17) + 0x17; // magic..
if( i == 0 ) nun->joy_x = h;
if( i == 1 ) nun->joy_y = h;
if( i == 2 ) nun->accu_x = h << 2;
if( i == 3 ) nun->accu_y = h << 2;
if( i == 4 ) nun->accu_z = h << 2;
if( i == 5 ) {
nun->accu_x |= (h >> 2)&3;
nun->accu_y |= (h >> 4)&3;
nun->accu_z |= (h >> 6);
h&1 ? nun->z = true : nun->z = false;
h&3 ? nun->c = true : nun->c = false;
}
}
Wire.beginTransmission(0x52);
Wire.send(0x00);
Wire.endTransmission();
}

void nun_init(byte id, struct nunchuk *nun){
nun->id=id;
Wire.begin();
Wire.beginTransmission(id);
Wire.send(0x40);
Wire.send(0x00);
Wire.endTransmission();
}


void setup(){
Serial.begin(9600);
nun = (struct nunchuk *) malloc(sizeof(struct nunchuk));
nun_init(0x52,nun);
}

void loop() {
nun_update();
Serial.print("x:"); Serial.print(nun->accu_x);
Serial.print(" y:"); Serial.print(nun->accu_y);
Serial.print(" z:"); Serial.print(nun->accu_z);
Serial.print(" b_c:"); nun->c ? Serial.print("X") : Serial.print("_");
Serial.print(" b_z:"); nun->z ? Serial.print("X") : Serial.print("_");
Serial.print(" j_x:"); Serial.print(nun->joy_x);
Serial.print(" j_y:"); Serial.print(nun->joy_y);
delay(100);
}



-- doesn't work
Code: [Select]


void loop() {
nun_update();
Serial.print("x:"); Serial.print(nun->accu_x);

delay(100);
}



-- works



Without nun_update() and nun_init() it works too...


/* Edited, forgot to malloc space  */

Coding Badly


Two things...

1. This is the wrong section of the forum for this sort of question.

2. nun is an uninitialized pointer which is always trouble.  While not a good solution, the following changes should make the sketch more stable...

Code: [Select]
struct nunchuk nunstorage;

void setup( void )
{
  nun = &nunstorage;


apfelmann

I just changed something and forgot to malloc space.
Why is this the wrong section? Where should I go?

Coding Badly

Quote
Why is this the wrong section?


Installation & Troubleshooting
For problems with Arduino itself, NOT your project

Quote
Where should I go?


http://arduino.cc/forum/index.php/board,4.0.html

apfelmann

Read my post..
The Blink example doesn't work.
And I have problems with my Serial connection.

This is not about my project.


Coding Badly

This is not about my project


http://arduino.cc/forum/index.php/topic,65689.msg480989.html#msg480989
...I cannot find that code in any of the examples so I assumed it was your code.  Which example are you using?

apfelmann

You'll find the Blink example in 1.Basic->Blink

http://arduino.cc/forum/index.php/topic,65689.msg480989.html#msg480989
-- is my code.
BUT I ment I don't want to debug my Code.  James C4S wanted to see my code and I gave it to him.

I initialized a Wire with no funktions. And my serial connect didn't work.
Blink doesn't work without serial connection in setup().
I think this is troubleshooting.

I'm sorry for missunderstandings, but I don't post often in forums
By the way my english is very bad, I think.


cmiyc

I think I understand what you are asking.

Are you saying this code:
Code: [Select]

void setup() {               
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}


Does not blink pin 13 unless you change setup() to say:

Code: [Select]

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);     
}


?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

apfelmann


apfelmann

Code: [Select]

void setup() {               
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

The LED shines all the time..

But this one works too..
Code: [Select]

void setup() {               
  pinMode(13, OUTPUT);     
}

int i = 0;
void loop() {
   i++;
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}



cmiyc

Ahhh... Sounds like delay() is broken, which was a known issue.

What version of the Arduino IDE are you using?

You might be interested in the last couple of posts on this thread.  A couple of posts on updating avr-gcc and binutils to get Gentoo working.

http://arduino.cc/forum/index.php/topic,49900.15.html
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

apfelmann

Sounds to match ;)
Thanks!

Go Up