首页 文章

使用Arduino和处理进行游戏

提问于
浏览
-2

我正在尝试形成一个双人游戏,需要对视觉进行音频反射 . 通过使用littebits声音触发声音输入和littbits arduino将其连接到计算机 . 但我是新手,并且不知道如何将arduino连接到处理并使用声音触发器的输入来在出现黑色方块时实现分数 .

这是我处理的代码和我从littlebits网站上采取的示例arduino代码,并尝试修改一点 .

提前致谢!

float dice;

int playerOne = 0; //player 1 score (left paddle)                              
int playerTwo = 0; //player 2 score (right paddle)                             
boolean oneWins = false;                                                       
boolean twoWins = false;                                                       

void setup(){                                                                  
    size(500, 500);                                                            
    smooth();                                                                  
    noStroke();                                                                
    frameRate(2.5);                                                            
}                                                                              

void draw() {                                                                  

    background(255);                                                           
    showGUI();                                                                 
    dice = random(0, 3);                                                       

    if (dice < 1.000001 && dice > 0.1){                                        
        fill ((0), (255), (0));                                                
        ellipse (250,250,100,100);                                             
    } else if (dice < 2.000001 && dice > 1.000001){                            
        rectMode(RADIUS);                                                      
        fill ((255), (0), (0));                                                
        rect (250,250,50,50);                                                  
    } else if (dice < 3.000000 && dice > 1.000000){                            
        rectMode(RADIUS);                                                      
        fill ((0), (0), (255));                                                
        rect (250,250,50,50);                                                  
    } else if (dice < 0.1){                                                    
        rectMode(RADIUS);                                                      
        fill(0);                                                               
        rect(250,250,50,50);                                                   
    }                                                                          
}                                                                              
----------arduino------                                                        

void setup() {                                                                 
    Serial.begin(9600); //Establish rate of Serial communication               
    establishContact(); //See function below                                   
}                                                                              

void loop() {                                                                  
    if (Serial.available() > 0) {                                              
        int inByte = Serial.read();                                            
        int leftTrigger = analogRead(A0);                                      

        Serial.print(leftTrigger, DEC);                                        
        Serial.print(",");                                                     

        int rightTrigger = analogRead(A1);                                     
        Serial.println(rightTrigger, DEC);                                     
    }                                                                          
}                                                                              

void establishContact() {                                                      

    while (Serial.available() <= 0) {                                          
        Serial.println("hello");                                               
        delay(300);                                                            
    }                                                                          
}

1 回答

  • 0

    您需要两段代码才能工作:一个在Arduino上发送命令,另一个在Processing上接收和解析这些命令 .

    我没有't used the littlebits modules, but here'是this very detailed tutorial的按钮示例 .

    Arduino代码:

    int switchPin = 4;               // switch connected to pin 4 
    
    void setup() { 
      pinMode(switchPin, INPUT);     // set pin 0 as an input 
      Serial.begin(9600);            // start serial communication at 9600bps 
    } 
    
    void loop() { 
      if (digitalRead(switchPin) == HIGH) {  // if switch is ON, 
        Serial.print(1, BYTE);               // send 1 to Processing
      } else {                               // if the switch is not ON,
        Serial.print(0, BYTE);               // send 0 to Processing 
      } 
      delay(100);                            // wait 100 milliseconds 
    }
    

    和匹配的处理代码:

    import processing.serial.*; 
    
    Serial port;            // create object from Serial class 
    int val;                // data received from the serial port 
    
    void setup() { 
      size(200, 200); 
      frameRate(10); 
    
      // open the port that the board is connected to
      // and use the same speed (9600bps) 
      port = new Serial(this, 9600); 
    } 
    
    void draw() { 
      if (0 < port.available()) {        // if data is available,
        val = port.read();               // read it and store it in val
      } 
      background(255);                   // set background to white
      if (val == 0)  {                   // if the serial value is 0,
        fill(0);                         // set fill to black
      } else {                           // if the serial value is not 0,
        fill(204);                       // set fill to light gray
      } 
      rect(50, 50, 100, 100);
    }
    

    请注意,Arduino发送一个Processing查找和解释的值 . 您还可以查看Arduino IDE中的 PhysicalPixel 示例,以获取有关将数据从Processing发送到Arduino的示例 .

相关问题