Buongiorno a tutti , Ete ho preso in considerazione il tuo suggerimento e ho inserito nella prima parte dello sketch al posto del delay le due vaiabili una esisteva (timestep) e lo stato.Ma quando faccio la compilazione mi da errore dicendomi le parentesi non vanno prima del if. Non riesco a risolvere perchè tolgo rimetto non so cosa fare.Ti allego la prima parte con gli errori.Fammi capire cosa sbaglio e se il suggerimento è stato recepito da me.Grazie Giuseppe.
#include <Tone.h>
Tone tone1;
byte p5 = 32;
byte p4 =34;
byte p3= 36;
byte p2 = 38;
byte p6 = 40;
byte p7 = 42;
byte p8 = 44;
int RELAY_PIN1 = 2;
int RELAY_PIN2 = 7;
int RELAY_PIN3 = 12;
int RELAY_PIN4 = 22;
int RELAY_PIN5 = 24;
int RELAY_PIN6 = 26;
int RELAY_PIN7= 28;
int RELAY_PIN8 = 30;
byte contatore = 0;
byte current_pin = 0;
long test;
#define OCTAVE_OFFSET 0
int pinData= 11; // pin sensore ir
unsigned long time;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
unsigned stato;
int notes[] = { 0, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
#define isdigit(n) (n >= '0' && n <= '9')
#define pin_down 3
#define pin_sun 5
#define pin_sunset 6
#define pin_moon 9
#define pin_starsA 10
#define pin_pot A0
int oneminrome[4] = { 4194,22958,4195,28651 };
int oneminbtlm[4] = { 3668,25189,3668,27474 };
int timeStepOld = 0;
int timeStep = 0;
long timeCurr = 0;
byte fade[] = {255,250,245,240,235,230,225,220,215,210,205,200,195,190,185,180,175,170,165,160,155,150,145,140,135,130,125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0}; // 52 step
byte curr = 0;
void setup() {
Serial.begin( 9600 );
pinMode( pin_down ,OUTPUT );
pinMode( pin_sun ,OUTPUT );
pinMode( pin_sunset,OUTPUT );
pinMode( pin_moon ,OUTPUT );
pinMode( pin_starsA,OUTPUT );
timeStep=millis();
pinMode( pin_pot, INPUT );
analogWrite( pin_down ,LOW );
analogWrite( pin_sun ,LOW );
analogWrite( pin_sunset,LOW );
analogWrite( pin_moon ,LOW );
analogWrite( pin_starsA,LOW );
tone1.begin(13);
pinMode (RELAY_PIN1,OUTPUT);
pinMode (RELAY_PIN2,OUTPUT);
pinMode (RELAY_PIN3,OUTPUT);
pinMode (RELAY_PIN4,OUTPUT);
pinMode (RELAY_PIN5,OUTPUT);
pinMode (RELAY_PIN6,OUTPUT);
pinMode (RELAY_PIN7,OUTPUT);
pinMode (RELAY_PIN8,OUTPUT);
digitalWrite (RELAY_PIN1,HIGH);
digitalWrite (RELAY_PIN2,HIGH);
digitalWrite (RELAY_PIN3,HIGH);
digitalWrite (RELAY_PIN4,HIGH);
digitalWrite (RELAY_PIN5,HIGH);
digitalWrite (RELAY_PIN6,HIGH);
digitalWrite (RELAY_PIN7,HIGH);
digitalWrite (RELAY_PIN8,HIGH);
Serial.println("-- START --");
}
void loop() {
timeStep=millis();
timeCurr = oneminrome[curr];
int pot = analogRead( pin_pot );
if ( 128 <= pot && pot <= 255 ) { timeCurr = (timeCurr * 2 ); } // two min
if ( 256 <= pot && pot <= 382 ) { timeCurr = (timeCurr * 5 ); } // five min
if ( 383 <= pot && pot <= 510 ) { timeCurr = (timeCurr * 10 ); } // ten min
if ( 511 <= pot && pot <= 637 ) { timeCurr = (timeCurr * 15 ); } // fifteen min
if ( 638 <= pot && pot <= 765 ) { timeCurr = (timeCurr * 20 ); } // twenty min
if ( 766 <= pot && pot <= 892 ) { timeCurr = (timeCurr * 25 ); } // twenty five min
if ( 893 <= pot && pot <= 1023 ) { timeCurr = (timeCurr * 30 ); } // thirty min
timeStep = (timeCurr/52);
//Serial.print("Selettore: "); Serial.println(pot);
Serial.print("Current: "); Serial.println(curr);
Serial.print("Time current: "); Serial.println(timeCurr);
Serial.print("Time single step: "); Serial.println(timeStep);
// down
if ( curr == 0 ) {
Serial.print("Fase: "); Serial.println("DOWN");
for ( int i=0; i<52; i++) {
analogWrite(pin_down, fade[(51-i)]);
analogWrite(pin_sun, 0 );
analogWrite(pin_sunset, 0 );
analogWrite(pin_moon, 0 );
analogWrite(pin_starsA, 0 );
if (stato == 0) { //se il ritardo non e' ancora iniziato
timeStep = millis(); //setta variabile tempo
stato = 1;} //setta flag per dire al ciclo che ora il ritardo e' iniziato
else if (stato == 1) { //se il ritardo e' iniziato
if ((millis() - timeStep) >= 806{ //quando i 100 ms sono passati
digitalWrite (RELAY_PIN3,HIGH);
digitalWrite (RELAY_PIN6,LOW);
digitalWrite (RELAY_PIN5,LOW); // ... istruzioni da eseguire dopo i 100 millisecondi ... //esegui le istruzioni
stato = 0; //resetta flag
}
}
}
}
Arduino:1.6.4 (Windows 7), Scheda:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
Uso la libreria Tone nella cartella: C:\Users\Giuseppe\Documents\Arduino\libraries\Tone (legacy)
C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10604 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Program Files\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files\Arduino\hardware\arduino\avr\variants\mega -IC:\Users\Giuseppe\Documents\Arduino\libraries\Tone C:\Users\Giuseppe\AppData\Local\Temp\build7517881731082121046.tmp\sketch_oct22d.cpp -o C:\Users\Giuseppe\AppData\Local\Temp\build7517881731082121046.tmp\sketch_oct22d.cpp.o
sketch_oct22d.ino: In function 'void loop()':
sketch_oct22d.ino:127:36: error: expected ')' before '{' token
sketch_oct22d.ino:133:1: error: expected primary-expression before '}' token
sketch_oct22d.ino:133:1: error: expected ';' before '}' token
expected ')' before '{' token