Pages: [1]   Go Down
Author Topic: void value not ignored as it ought to be - errore durante programmazione gioco.  (Read 383 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ciao a tutti ragazzi, sono JonnyK.
ho avuto un errore durante la programmazione di un gioco.
di seguito vi posto il codice e la descrizione di quello che dovrebbe fare il gioco!

Code:
int trig = 7;
int echo = 8;

long randNumber;

void setup() {
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  randomSeed(analogRead(A0));
  
  Serial.begin( 9600 );
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop() {

  digitalWrite (trig, LOW);
 
  digitalWrite (trig, HIGH);
  delayMicroseconds( 10 );
  digitalWrite (trig, LOW);
 
  long durata = pulseIn(echo, HIGH);

 
  long r = 0.0343 * durata / 2;
 
  Serial.print ("Distanza: ");
  Serial.print (r);
  Serial.println (" cm");
  delay ( 100 );
  
  
  
  randNumber = random(1,4);
  
  if (randNumber==1)
       digitalWrite(2,HIGH);
  else digitalWrite(2,LOW);
  
  
  if (randNumber==2)
       digitalWrite(3,HIGH);
  else digitalWrite(3,LOW);
  
  
  if (randNumber==3)
       digitalWrite(4,HIGH);
  else digitalWrite(4,LOW);
  


// if ((digitalWrite(2,HIGH)) (durata < 50))
   digitalWrite(7,HIGH);
   digitalWrite(8,LOW);
else  digitalWrite(7,LOW);
      digitalWrite(8,HIGH);

if ((digitalWrite(2,HIGH)) (durata < 100) (durata > 50))
   digitalWrite(7,HIGH);
   digitalWrite(8,LOW);
else  digitalWrite(7,LOW);
      digitalWrite(8,HIGH);
      
if ((digitalWrite(2,HIGH)) (durata < 150) (durata > 100))
   digitalWrite(7,HIGH);
   digitalWrite(8,LOW);
else  digitalWrite(7,LOW);
      digitalWrite(8,HIGH);

}


allora, il gioco si sviluppa così:
ci sono 3 led che si accendono in modo pseudo-random.
a seconda di quale led si accende la persona deve saltare su una pedana di 150 cm
le distanze vengono misurate da un sensore ad ultrasuoni HC-SR04.
se si salta nel posto giusto, si accende il led verde pin 7, se succede il contrario, pin 8 led rosso.

mi da quest'errore:

void value not ignored as it ought to be

selezionando la stringa di codice che vi metto come commento.

ciao a tutti e grazie in anticipo!

JonnyK
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26638
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ((digitalWrite(2,HIGH))
"digitalWrite" does not return a value
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Italy
Online Online
Brattain Member
*****
Karma: 336
Posts: 23149
Logic is my way
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

JonnyK, nella sezione internazionale si usa l'inglese, se vuoi pubblicare un quesito in italiano, lo devi inserire qui.
Logged


Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 115
Posts: 7276
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mancano solo una decina di parentesi graffe.  smiley-mr-green

Non puoi scrivere così
Code:
if ((digitalWrite(2,HIGH)) (durata < 100) (durata > 50))
   digitalWrite(7,HIGH);
   digitalWrite(8,LOW);
else  digitalWrite(7,LOW);
      digitalWrite(8,HIGH);

devi scriverlo con le parentesi
Code:
if ((digitalWrite(2, HIGH)) (durata < 100) (durata > 50))
{
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
} else {
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
}

Poi ci sono altri errori nell'IF....
« Last Edit: July 19, 2014, 03:46:09 am by PaoloP » Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26638
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ((digitalWrite(2, HIGH)) (durata < 100) (durata > 50))
?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ivrea (to)
Offline Offline
Faraday Member
**
Karma: 87
Posts: 5067
miaaao ^-^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ((digitalWrite(2, HIGH)) (durata < 100) (durata > 50))
?

Forse volevi scrivere, se il pin 2 è HIGH (digitalRead) e (AND) durata<100 e (AND) durata>50 allora...
Code:
if( digitalRead(2)==HIGH && (durata<100 && durata>50) )
Logged

my name is IGOR, not AIGOR

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, ho rifatto il codice
Code:
int trig = 7;
int echo = 8;

long randNumber;

void setup() {
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  randomSeed(analogRead(A0));
 
  Serial.begin( 9600 );
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
 
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop() {

  digitalWrite (trig, LOW);
 
  digitalWrite (trig, HIGH);
  delayMicroseconds( 10 );
  digitalWrite (trig, LOW);
 
  long durata = pulseIn(echo, HIGH);

 
  long r = 0.0343 * durata / 2;
 
  Serial.print ("Distanza: ");
  Serial.print (r);
  Serial.println (" cm");
  delay ( 100 );
 
 
 
  randNumber = random(1,4);
 
  if (randNumber==1)
       digitalWrite(2,HIGH);
  else digitalWrite(2,LOW);
 
 
  if (randNumber==2)
       digitalWrite(3,HIGH);
  else digitalWrite(3,LOW);
 
 
  if (randNumber==3)
       digitalWrite(4,HIGH);
  else digitalWrite(4,LOW);
 
  delay(2000);


if( digitalRead(2)==HIGH && (durata < 50) )
   { digitalWrite(7,HIGH);
   digitalWrite(8,LOW); }
else  { digitalWrite(7,LOW);
      digitalWrite(8,HIGH); }

if( digitalRead(2)==HIGH && (durata < 100 && durata > 50) )
   { digitalWrite(7,HIGH);
   digitalWrite(8,LOW); }
else  { digitalWrite(7,LOW);
      digitalWrite(8,HIGH); }
     
if( digitalRead(2)==HIGH && (durata < 150 && durata > 100) )
   { digitalWrite(7,HIGH);
   digitalWrite(8,LOW); }
else  { digitalWrite(7,LOW);
      digitalWrite(8,HIGH); }

}

ma mi da degli errori, non di programmazione, errori al livello di funzionamento:
il LED rosso pin 8 rimane sempre acceso, ed il LED verde pin 7 fa solo una piccola (scintilla), ma non si accende.
inoltre il monitor seriale non rileva nulla!

ciao e grazie in anticipo

JonnyK
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 115
Posts: 7276
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Prova così:
Code:
const byte LED1 = 2;
const byte LED2 = 3;
const byte LED3 = 4;
const byte RED = 8;
const byte GREEN = 7;

const byte trig = 5;
const byte echo = 6;

long randNumber;

void setup() {
  delay(1000);
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  randomSeed(analogRead(A0));
}

void loop() {
  unsigned long r = sonar();
  Serial.print ("Distanza: ");
  Serial.print (r);
  Serial.println (" cm");

  delay ( 100 );

  randNumber = random(1, 4);

  if (randNumber == 1)
    digitalWrite(LED1, HIGH);
  else
    digitalWrite(LED1, LOW);

  if (randNumber == 2)
    digitalWrite(LED2, HIGH);
  else
    digitalWrite(LED2, LOW);

  if (randNumber == 3)
    digitalWrite(LED3, HIGH);
  else
    digitalWrite(LED3, LOW);

  delay(2000);

  if (digitalRead(LED1) == HIGH)
  {
    if (r < 50)
    { digitalWrite(GREEN, HIGH);
      digitalWrite(RED, LOW);
    }
    else  {
      digitalWrite(GREEN, LOW);
      digitalWrite(RED, HIGH);
    }

    if (r < 100 && r > 50)
    { digitalWrite(GREEN, HIGH);
      digitalWrite(RED, LOW);
    }
    else  {
      digitalWrite(GREEN, LOW);
      digitalWrite(RED, HIGH);
    }

    if (r < 150 && r > 100)
    { digitalWrite(GREEN, HIGH);
      digitalWrite(RED, LOW);
    }
    else  {
      digitalWrite(GREEN, LOW);
      digitalWrite(RED, HIGH);
    }
  }
} // End Loop

unsigned long sonar() {
  unsigned long durata, radius;
  digitalWrite (trig, LOW);
  digitalWrite (trig, HIGH);
  delayMicroseconds( 10 );
  digitalWrite (trig, LOW);
  durata = pulseIn(echo, HIGH);
  radius = 0.0343 * durata / 2;
  return radius;
}

Verifica i pin iniziali.
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

grazie a tutti e scusate per il ritardo!  smiley-lol smiley-lol smiley-grin

PaoloP, non hai capito cosa deve fare il gioco, dopo lo rispiego. smiley-razz smiley smiley-mr-green

ho cambiato alcune cose, ad esempio, non ci sono più i led rossi e verdi, ma ci sono 3 led a secondo del led della funzione random() che si accende.

ho per l'ennesima volta rifatto il codice, ma ancora non fa ciò che deve fare...    smiley-mad smiley-confuse smiley-cry smiley-cry smiley-sad smiley-sad


Code:
const byte LED1 = 2;
const byte LED2 = 3;
const byte LED3 = 4;
const byte Ver1 = 7;
const byte Ver2 = 8;
const byte Ver3 = 9;

const byte trig = 5;
const byte echo = 6;

long randNumber;

void setup() {
  delay(1000);
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
 
  pinMode(Ver1, OUTPUT);
  pinMode(Ver2, OUTPUT);
  pinMode(Ver3, OUTPUT);
 
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  randomSeed(analogRead(A0));
}

void loop() {
  unsigned long r = sonar();
  Serial.print ("Distanza: ");
  Serial.print (r);
  Serial.println (" cm");

  delay ( 100 );

  randNumber = random(1, 4);

  if (randNumber == 1)
    digitalWrite(LED1, HIGH);
  else
    digitalWrite(LED1, LOW);

  if (randNumber == 2)
    digitalWrite(LED2, HIGH);
  else
    digitalWrite(LED2, LOW);

  if (randNumber == 3)
    digitalWrite(LED3, HIGH);
  else
    digitalWrite(LED3, LOW);

  delay(2000);

 
    if ( digitalRead(2)==HIGH && (r < 50) )
    { digitalWrite(Ver1,HIGH);
      digitalWrite(Ver2,LOW);
      digitalWrite(Ver3,LOW);
    }
    else  {
      digitalWrite(Ver1,LOW);
      digitalWrite(Ver2,LOW);
      digitalWrite(Ver3,LOW);
    }

    if ( digitalRead(2)==HIGH && (r < 100 && r > 50) )
    { digitalWrite(Ver1,LOW);
      digitalWrite(Ver2,HIGH);
      digitalWrite(Ver3,LOW);
    }
    else  {
      digitalWrite(Ver1,LOW);
      digitalWrite(Ver2,LOW);
      digitalWrite(Ver3,LOW);
    }

    if ( digitalRead(2)==HIGH && (r < 150 && r > 100) )
    { digitalWrite(Ver1,LOW);
      digitalWrite(Ver2,LOW);
      digitalWrite(Ver3,HIGH);
    }
    else  {
      digitalWrite(Ver1,LOW);
      digitalWrite(Ver2,LOW);
      digitalWrite(Ver3,LOW);
    }
}

unsigned long sonar() {
  unsigned long durata, radius;
  digitalWrite (trig, LOW);
  digitalWrite (trig, HIGH);
  delayMicroseconds( 10 );
  digitalWrite (trig, LOW);
  durata = pulseIn(echo, HIGH);
  radius = 0.0343 * durata / 2;
  return radius;
}

eccolo..

allora prima di tutto, c'è sempre quel problema della "scintilla" dei led per il punteggio, che adesso sono i 3 led 7,8, e 9.

poi, vorrei fare in modo che il sensore ad ultrasuoni cominci a misurare circa mezzo secondo dopo il led della funzione random()...

come fare?

ciao e grazie in anticipo

JonnyK
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 115
Posts: 7276
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Così?
Code:
const byte LED1 = 2;
const byte LED2 = 3;
const byte LED3 = 4;
const byte Ver1 = 7;
const byte Ver2 = 8;
const byte Ver3 = 9;

const byte trig = 5;
const byte echo = 6;

long randNumber;

void setup() {
  delay(1000);
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  pinMode(Ver1, OUTPUT);
  pinMode(Ver2, OUTPUT);
  pinMode(Ver3, OUTPUT);

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  randomSeed(analogRead(A0));
}

void loop() {
  unsigned long r = sonar();                  // Legge il sonar
  Serial.print ("Distanza: ");
  Serial.print (r);
  Serial.println (" cm");

  delay(100);

  randNumber = random(1, 4);                  // sceglie un numero a caso

  if (randNumber == 1)
    digitalWrite(LED1, HIGH);
  else
    digitalWrite(LED1, LOW);

  if (randNumber == 2)
    digitalWrite(LED2, HIGH);
  else
    digitalWrite(LED2, LOW);

  if (randNumber == 3)
    digitalWrite(LED3, HIGH);
  else
    digitalWrite(LED3, LOW);

  delay(2000);                               // Aspetto 2 secondi e poi controllo

  if ( randNumber == 1 && (r < 50) )
  { digitalWrite(Ver1, HIGH);
    digitalWrite(Ver2, LOW);
    digitalWrite(Ver3, LOW);
  }
  else  {
    digitalWrite(Ver1, LOW);
    digitalWrite(Ver2, LOW);
    digitalWrite(Ver3, LOW);
  }

  if ( randNumber == 2 && (r < 100 && r > 50) )
  { digitalWrite(Ver1, LOW);
    digitalWrite(Ver2, HIGH);
    digitalWrite(Ver3, LOW);
  }
  else  {
    digitalWrite(Ver1, LOW);
    digitalWrite(Ver2, LOW);
    digitalWrite(Ver3, LOW);
  }

  if ( randNumber == 3 && (r < 150 && r > 100) )
  { digitalWrite(Ver1, LOW);
    digitalWrite(Ver2, LOW);
    digitalWrite(Ver3, HIGH);
  }
  else  {
    digitalWrite(Ver1, LOW);
    digitalWrite(Ver2, LOW);
    digitalWrite(Ver3, LOW);
  }

  delay(2000);
} // Fine loop

unsigned long sonar() {
  unsigned long durata, radius;
  digitalWrite (trig, LOW);
  digitalWrite (trig, HIGH);
  delayMicroseconds( 10 );
  digitalWrite (trig, LOW);
  durata = pulseIn(echo, HIGH);
  radius = 0.0343 * durata / 2;
  return radius;
}
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Pages: [1]   Go Up
Jump to: