keskiviikko 18. maaliskuuta 2015

Simon Says Peli ja muut projektit

Ihan alunperin minun piti tehdä kännyn välityksellä kytkettävä ilmankostutin. eli siis olisin saanut etänä laitteen päälle tai pois. suomesta ei mistään kaikki osia olisi edes saanut. (kävin partcolla kysymässä). luin lisää aiheesta ja tulin tulokseen etten ala vaarantamaan omaa henkeäni koska olisi loppupeleissä pitänyt tehdä semmoisia sähkökytkentöjä mitä en "osaa". en halua saada pistorasiasta sähköiskua, joten hylkäsin tämän ajatuksen loppupeleissä.

Päätin tehdä pelin missä arduino antaa valoja järjestyksessä ja pelaajan pitää itse painaa samassa järjestyksessä oikeat valot perässä.

Alkuperäinen tekijä: Robert Spann
http://forum.arduino.cc/index.php?topic=8458.0

Olen käyttänyt älyttömän määrän aikaa tähän, enkä vain saa tätä toimimaan.



Kokeilin myös äänellistä versiota mutta en saa tones libraryä mitenkään toimimaan. aina jotain error messagea heittää.



Koodi:

/*Simon Says game by Robert Spann*/

int switch1 = 11; //The four button input pins
int switch2 = 10;
int switch3 = 9;
int switch4 = 8;
int led1 = 5; //LED pins
int led2 = 4;
int led3 = 3;
int led4 = 2;
int turn = 0;
int input1 = LOW;
int input2 = LOW;
int input3 = LOW;
int input4 = LOW;

int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];


void setup() {

 Serial.begin(9600);

 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(switch1, INPUT);
 pinMode(switch2, INPUT);
 pinMode(switch3, INPUT);
 pinMode(switch4, INPUT);
 randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function

for (int y=0; y<=99; y++){ //For statement to loop through the output and input functions
  output();
  input();
}

  }
 
void output() { //function for generating the array to be matched by the player
 
  for (int y=turn; y <= turn; y++){ //Limited by the turn variable
   Serial.println(""); //Some serial output to follow along
   Serial.print("Turn: ");
   Serial.print(y);
   Serial.println("");
   randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
   for (int x=0; x <= turn; x++){

     Serial.print(randomArray
  • );

         if (randomArray
  • == 1) {  //if statements to display the stored values in the array
           digitalWrite(led1, HIGH);
           delay(500);
           digitalWrite(led1, LOW);
           delay(100);
         }

         if (randomArray
  • == 2) {
           digitalWrite(led2, HIGH);
           delay(500);
           digitalWrite(led2, LOW);
           delay(100);
         }

         if (randomArray
  • == 3) {
           digitalWrite(led3, HIGH);
           delay(500);
           digitalWrite(led3, LOW);
           delay(100);
         }

         if (randomArray
  • == 4) {
           digitalWrite(led4, HIGH);
           delay(500);
           digitalWrite(led4, LOW);
           delay(100);
         }
        }
       }
      }



    void input() { //Function for allowing user input and checking input against the generated array

     for (int x=0; x <= turn;){ //Statement controlled by turn count
       input1 = digitalRead(switch1);
       input2 = digitalRead(switch2);
       input3 = digitalRead(switch3);
       input4 = digitalRead(switch4);

       if (input1 == HIGH){ //Checking for button push
         digitalWrite(led1, HIGH);
         delay(200);
         digitalWrite(led1, LOW);
         inputArray
  • = 1;
         delay(50);
         Serial.print(" ");
         Serial.print(1);
         if (inputArray
  • != randomArray
  • ) { //Checks value input by user and checks it against
           fail();                              //the value in the same spot on the generated array
         }                                      //The fail function is called if it does not match
         x++;
       }

       if (input2 == HIGH){
         digitalWrite(led2, HIGH);
         delay(200);
         digitalWrite(led2, LOW);
         inputArray
  • = 2;
         delay(50);
         Serial.print(" ");
         Serial.print(2);
         if (inputArray
  • != randomArray
  • ) {
           fail();
         }
         x++;

       }

       if (input3 == HIGH){
         digitalWrite(led3, HIGH);
         delay(200);
         digitalWrite(led3, LOW);
         inputArray
  • = 3;
         delay(50);
         Serial.print(" ");
         Serial.print(3);
         if (inputArray
  • != randomArray
  • ) {
           fail();
         }
         x++;

       }

       if (input4 == HIGH){

         digitalWrite(led4, HIGH);
         delay(200);
         digitalWrite(led4, LOW);
         inputArray
  • = 4;
         delay(50);
         Serial.print(" ");
         Serial.print(4);
         if (inputArray
  • != randomArray
  • ) {
           fail();
         }
         x++;

       }
      }
     delay(500);
     turn++; //Increments the turn count, also the last action before starting the output function over again
    }

    void fail() { //Function used if the player fails to match the sequence

     for (int y=0; y<=5; y++){ //Flashes lights for failure
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      digitalWrite(led4, HIGH);
      delay(200);
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
      delay(200);
     }
     delay(500);
     turn = -1; //Resets turn value so the game starts over without need for a reset button
    }

    void loop() { //Unused void loop(), though for some reason it doesn't compile without this /shrug
    }



Yritin myös hätäratkaisuna tehdä digitaalista noppaa, mutta en saanut painiketta toimimaan minkä pitäisi arpoa numero. kuva projektin alkuvaiheesta missä testaan näyttöä. painiketta ei ole vielä tässä kuvassa kytketty. en ottanut kuvaa enää siitä vaiheesta koska unohdin kun tämäkään ei toiminut:






pizzerian mainoskyltti testi

Aloitin kytkemällä TÄMÄN ohjeen mukaisesti.



Käytin hieman muokkaamaani esimerkki liquidcrystal "hello world"
koodia:

/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("halpa pizza!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
}


vaihdoin tämän:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Tähän:

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 ja tietenkin tekstin vaihdoin pizzeria tekstiksi.


light theremin testi



koodi täältä.

kytkennät tehty arduino project book project 06 kytkentöjen mukaisesti.



    int sensorValue;
    int sensorLow = 1023;
    int sensorHigh = 0;
    const int ledPin = 13;
    void setup() {
     
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, HIGH);
     
      while (millis() < 5000) {
        sensorValue = analogRead(A0);
        if (sensorValue > sensorHigh) {
          sensorHigh = sensorValue;
        }
        if (sensorValue < sensorLow) {
          sensorLow = sensorValue;
        }
      }
     
      digitalWrite(ledPin, LOW);
    }
    void loop() {
      sensorValue = analogRead(A0);
     
      int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
      tone(8, pitch, 20);
     
      delay(10);
    }

keskiviikko 4. helmikuuta 2015

Ultrasonic sensor test

Kaiku arduinolla:

/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* VCC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 2
* ECHO connection of the sensor attached to digital pin 4
Original code for Ping))) example was created by David A. Mellis
Adapted for HC-SR04 by Tautvidas Sipavicius
This example code is in the public domain.
*/


const int trigPin = 2;
const int echoPin = 4;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Morse code

Morse code arduinolla:

int led = 13;
//yj Integer representing dit (morse ' . ')
int dit = 500;
//yj Integer representing dah (morse ' - '>)
//yj Testing a multiple of another constant integer to make the timing scalable.
int dah = dit*3;
int blankTime = 100;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait 
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait 
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dah);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dah);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dah);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(dit);               // wait
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(blankTime);               // wait
}

perjantai 24. tammikuuta 2014

random valot

Tarkoituksena oli luoda jotain mielenkiintoista mutta tälläkertaa ei riittäny mielikuvitus kuin kahteen vilkkuvaan valoon. sanotaanko näin että se voi olla vaikka vaihtuvat valot. liitteenä kuva ja koodi.

Kytkennät oli helppo tehdä ja koodikin syntyi suhteellisen helposti.



int led = 13;
int led2 = 12;

// the setup routine runs once when you press reset:
void setup() {            
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
 }

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  {digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100); }