my actual code is kinda long, messy and commented in french, but here it is anyway.
#include <Wire.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#undef int
#include <stdio.h>
#include <Servo.h>
Servo patind;
Servo pating;
byte outbuf[6];
byte *bufpoint=&outbuf[0];// stockage des données du Nunchuck
byte zero=0x00;
int deg=0;
int degf=0;
int count=0;
boolean cd=1; // vérifie le contact des patins avec le sol
boolean cg=1; // cd:droit cg:gauche
long mil[2];
byte pente=2;
void setup ()
{
mil[1] = 0;
pinMode(52, OUTPUT);
digitalWrite(52, HIGH);
// TCCR1B=13;
// TIFR1=0x00;
// TIMSK1=2;
// OCR1A=174;
// TCCR3B=13;
// TIFR3=0x00;
// TIMSK3=2;
// OCR3A=1024;
patind.attach(50);
pating.attach(51); // Liaision de la pin 2 avec le contrôleur du moteur
Wire.begin (); // Initialisation du I2C pour le nunchuck
nunchuck_init (); // Initialisation du Nunchuck
Serial.begin (19200);
Serial.print ("Finished setup\n");
TCNT2=0x00;
}
//ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
//{
// while (0b11 & outbuf[5]==0b11)
// {
// if (outbuf[1]<100 && pente>0)
// pente+=1;
// else if (outbuf[1]>160 && pente<60)
// pente-=1;
//
// }
//}
void loop()
{
byte caliss=nunchuck(); // Appel de la fonction pour recevoir la valeur du joystick
deg= map(outbuf[1], 131, 230, 0, 180); // Étalonnage des données pour avoir une valeur entre 0 et 180
mil[0]=millis();
if (deg>degf && mil[0]-mil[1]>=pente)
{
degf+=1;
mil[1]=mil[0];
}
else if (deg<degf && deg>0)
degf=deg;
else if (deg<0)
degf=0;
if (cd==1)
patind.write(degf); // Écris la valeur étalonnée dans le PPM de sortie pour le moteur
if (cg==1)
pating.write(degf);
Serial.println(degf);
// if (millis()>1000 && millis()<2000)
// count+=1;
//else
// Serial.println (count);
}
code used for acquiring data from a nunchuck
void nunchuck_init ()
{
Wire.beginTransmission (0x52); // transmet 0x52 au Nunchuck
Wire.write (0x40); // envoie l'adresse de mémoire
Wire.write (zero); // envoie un zéro
Wire.endTransmission (); // arrête la transmission
}
void send_zero ()
{
Wire.beginTransmission (0x52); // transmet 0x52 au Nunchuck
Wire.write (zero); // envoie un zéro
Wire.endTransmission (); // arrête la transmission
}
byte nunchuck ()
{
int cnt = 0;
Wire.requestFrom (0x52, 6); // Demande les 6 bytes de données du Nunchuck
while (Wire.available ())
{
*(bufpoint+cnt) = nunchuk_decode_byte (Wire.read ()); // Reçoit un byte de données du Nunchuck
cnt++;
}
// Serial.println(outbuf[1]);
send_zero ();
return 0; // Envoie la demande pour recevoir les prochains bits
// byte joy=outbuf[1]; // Stock la variable du Joystick y dans joy et retourne la valeur
//// Serial.println(joy);
// return joy;
}
char nunchuk_decode_byte (char x) // Décodage des valeurs du Nunchuck
{
x = (x ^ 0x17) + 0x17;
return x;
}
You should always use { } even when you only have one statement after "if" or "else", and learn to properly indent your code. Hit CTRL-T in the Arduino IDE. Subtle bugs might lie in "misindented" code.
Thanks, but I feel my question was not clear enough: If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends. If I call that same function, but declare it an int or byte or whatever and I let it return a useless variable, the array is correctly modified. Why does that happens? A void function can't change a value that lasts after it ends?
mxmcharbonneau:
Thanks, but I feel my question was not clear enough: If I call a void function that change an array declared elsewhere with a pointer, the array resets when the function ends. If I call that same function, but declare it an int or byte or whatever and I let it return a useless variable, the array is correctly modified. Why does that happens? A void function can't change a value that lasts after it ends?
There is no void function, nor int function, nor anything like that. There are functions that return data, and one must declare what type that data is, and there are functions that don't return anything, and one tells that to the compiler by using the special type void.
The data type returned by a function, be it void or anything else, has no influence whatsoever on what happens to the variables used inside that function. That depends only on the function code. What you are observing is definitely not a direct effect of what type of data that function returns.
"Post the code" in this context means two things:
copy-n-paste into a reply the entire code where you use the "void function"
copy-n-paste into a reply the entire code where you use the "int function"
Highlight the differences between the two codes and how they differ in their behaviour.