PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : tvout +i2c



simonprein
20.03.2014, 21:50
hi

ich habe volgendes programm und folgende fehlermeldung . wo liegt das problem? die variablen sind doch initialisiert

#include <TVout.h>
#include <fontALL.h>
#include <Wire.h>

TVout TV;

void setup()
{
TV.begin(_PAL, 128, 96);
TV.clear_screen();
TV.select_font(font6x8);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output

int control;
int a;
}

void loop()
{
TV.clear_screen();
TV.print(10,10, a );
int b == a ;
if ( b == a )
{delay (100);}
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
a = x;
}

slave_receiver.ino: In function 'void loop()':
slave_receiver:23: error: 'a' was not declared in this scope
slave_receiver:24: error: expected initializer before '==' token
slave_receiver:25: error: 'b' was not declared in this scope
slave_receiver.ino: In function 'void receiveEvent(int)':
slave_receiver:40: error: 'a' was not declared in this scope

Sisor
20.03.2014, 22:33
Stichwort Sichtbarkeit von Variablen / Stack.
Wenn du Variablen in einer Funktion (z.B. void setup()) initialisierst, sind diese von außen nicht sichtbar.
Anders ausgedückt: Nach dem Durchlaufen einer Funktion werden alle dort deklarierten Variablen wieder gelöscht.

Hab mir mal erlaubt den Code auf das Wesentliche zu kürzen (bin mir aber nicht sicher, ob ich den Zweck des Programms damit erschlagen habe):

#include <TVout.h>
#include <fontALL.h>
#include <Wire.h>

TVout tv;
int incoming = 0;

void setup()
{
tv.begin(_PAL, 128, 96);
tv.clear_screen();
tv.select_font(font6x);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
if (incoming)
{
// display the number 100ms then delete screen again
tv.print(10,10, incoming);
delay (100);
tv.clear_screen();
incoming = 0;
}
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
incoming = Wire.read(); // receive byte as an integer
Serial.println(incoming); // print the integer
}