Arduino en processing laten communiceren

Software die niet past in bovenstaande onderwerpen
Berichten: 2
Geregistreerd: 08 Jan 2014, 12:08

Arduino en processing laten communiceren

Berichtdoor Diane » 08 Jan 2014, 12:53

Hoihoi,

ik ben best wel nieuw met programmeren. Voor mijn studie moet ik een processing programma aansturen met een signaal vanuit Arduino, maar ik kom er echt niet uit en ik hoop hier hulp te vinden.

De bedoeling is dat een pacman een random andere kant uitgaat als er geklapt wordt. Eerst heb ik een programma geschreven voor Arduino waarbij een lampje gaat branden als er geklapt wordt en de elektronische schakeling met de microfoon werkt ook. Ik heb ervoor gezorgd dat er iedere keer wanneer er geklapt wordt, een random getal van 0 tot 3 (dus 4 getallen) in de serial gaan. Dit voor de richting van de pacman straks (naar boven, beneden, links, rechts)

Arduino programma

int sensorPin = A0; // Microphone Sensor Pin
int LED = 13; //LED on the arduino at pin 13 (?)
int sensorValue = 0;
int randomDirection = 0;


void setup () {
pinMode (LED, OUTPUT);
Serial.begin(9600);
}

void loop() {
//read the value from the sensor
sensorValue = analogRead(sensorPin);
//Serial.println(sensorValue);
if (sensorValue>900){ //The 'clap' sensor value is higher than 900
randomDirection = random(0,4);
Serial.write(randomDirection);
Serial.println(randomDirection);
digitalWrite(LED, HIGH); // The LED stays on for 2 seconds
delay(2000); // The red LED stays on for 2 secondsz
} else {
digitalWrite(LED, LOW);
}

}


Vervolgens heb ik een programma op internet gevonden (dus niet zelf geschreven), van een bewegende pacman. Deze werd origineel bestuurd met de pijltjestoetsen op je toetsenbord, maar dit heb ik veranderd in "randomDirection" 0-3. Met hulp van tutorials op internet heb ik geprobeerd serial connectie plaats te laten vinden.

Programma processing
import processing.serial.*;
import cc.arduino.*;


int radius = 20;
int direction = 1;
int time;
int intervel = 300;
int randomDirection = 2;
Arduino arduino;


//time and intervel to open and close mouth
float x = 10, y = 10, speed = 0.5;

void setup()
{
arduino = new Arduino(this, Arduino.list()[0], 57600);
size(500,500);
smooth();
ellipseMode(RADIUS);
fill(62,1,124);
noStroke();
time = millis();//number of milliseconds since program is run...
}

void draw()
{
println(Arduino.list());
background(175);
if((x > width - radius) || (x < radius))
direction = -direction;
arc(x,y,radius,radius,0.52,5.76);
background(175);
frameRate(13);
//slows it down a bit (program runs 13 times a second as opposed to 60)
//so you can see the mouth close
if(randomDirection == 0)// if the up arrow is pressed...
{
arc(x,y,radius,radius,5.2,10.6);
if(millis() > time + intervel)
//milliseconds plus 700 - 700 milliseconds -
//if milliseconds is greater than 700 milliseconds
//something will happen (every 0.7 seconds)
{
time = millis();
arc(x,y,radius,radius,5.18,10.6);
arc(x,y,radius,radius,5.08,10.7);
arc(x,y,radius,radius,4.8,10.8);
arc(x,y,radius,radius,4.5,11);
ellipse(x,y,radius,radius);
arc(x,y,radius,radius,4.5,11);
arc(x,y,radius,radius,4.8,10.8);
arc(x,y,radius,radius,5.08,10.7);
//lots of different arcs with the angles geting smaller/bigger
//to give the illusion of a mouth opening/closing
}

y = y - 1;
//changes the y value of the arcs every time the draw function is run
//so the pacman moves up on the y axis
}
else if(randomDirection == 1)
{
arc(x,y,radius,radius,8.4,13.7);
if(millis() > time + intervel)
{
time = millis();
arc(x,y,radius,radius,8.3,13.6);
arc(x,y,radius,radius,8.2,13.5);
arc(x,y,radius,radius,8.1,13.4);
arc(x,y,radius,radius,8,13.3);
arc(x,y,radius,radius,7.8,14);
ellipse(x,y,radius,radius);
arc(x,y,radius,radius,7.8,14);
arc(x,y,radius,radius,8,13.3);
arc(x,y,radius,radius,8.1,13.4);
arc(x,y,radius,radius,8.2,13.5);
}
y = y + 1;
}
else if(randomDirection == 2)
{
arc(x,y,radius,radius,0.52,5.76);
if(millis() > time + intervel)
{
time = millis();
arc(x,y,radius,radius,0.52,5.76);
arc(x,y,radius,radius,0.32,5);
arc(x,y,radius,radius,0.12,3.5);
arc(x,y,radius,radius,-3,0);
ellipse(x,y,radius,radius);
arc(x,y,radius,radius,-3,0);
arc(x,y,radius,radius,0.12,3.5);
arc(x,y,radius,radius,0.32,5);
}
x = x + 1;
}
else if (randomDirection == 3)
{
arc(x,y,radius,radius,3.67,8.90);
if(millis() > time + intervel)
{
time = millis();
arc(x,y,radius,radius,3.67,8.90);
arc(x,y,radius,radius,3.47,9.1);
arc(x,y,radius,radius,3.37,9.2);
arc(x,y,radius,radius,3.17,9.4);
arc(x,y,radius,radius,3.07,9.5);
arc(x,y,radius,radius,3.17,9.4);
arc(x,y,radius,radius,3.37,9.2);
arc(x,y,radius,radius,3.47,9.1);
}
x = x - 1;
}
if(x <= 10)
x = 10;
if(x >= width - 10)
x = width - 10;
if(y >= height - 10)
y = height - 10;
if(y <= 10)
y = 10;
//that's all to make it stop when it reaches the edge of the box.
//if y = 10 then its stationary.
//I made it stop 10 away from the edge because
//otherwise half of it goes through the the edge/wall.

}


Nu is het punt: als ik alleen het processing programma laat draaien, gaat bij een klap wel het lampje branden, maar veranderd de pacman niet van richting. Wat doe ik fout en hoe zorg ik dat het wel werkt?

Alvast heel hartelijk bedankt!
Groetjes Diane

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Arduino en processing laten communiceren

Berichtdoor nicoverduin » 08 Jan 2014, 23:41

Heb je al getest of er ook daadwerkelijk iets over de Serial naar de PC gaat? Ik heb in het verleden nog wel eens met processing gewerk en toen had ik ook zat problemen met de seriele verbinding onder Processing.
Ik zou de Seriele monitor aanzetten en kijken wat je van de Arduino naar de PC stuurt.
Daarnaast begrijp ik niet helemaal waarom je Serial.Write uitvoert en vervolgens Serial.println?
@edit: Ik zit nog ff verder te kijken naar het processing programma, maar waar lees jij de Serial eigenlijk?

Dit is de code van een ander programma waar Serial werd gebruikt:
Code: Alles selecteren
/**
 * name retryArduinoConnect()
 * Connects to the Arduino
 *
 */
public void retryArduinoConnect() {
    //
    // set connecting flag
    //
    connecting = true;
   
    if (!runWithoutArduino) {
        //    try{
        //      arduinoPort.stop();
        //    }catch(Exception e) {
        //      delay(10);
        //    }
        // Find Serial Port that the arduino is on
        // The arduino is sending out a 'T' every 100 millisecs. Contributed by Don K.
        long millisStart;
        int i = 0;
        //
        // get a list of COM ports available
        //
        int len = Serial.list().length;        //get number of ports available
        println(Serial.list());                //print list of ports to screen
        println("Serial Port Count = " + len); //print count of ports to screen
        //
        // check is there is any port available
        //
        if (len == 0) {
            //
            // No COM ports so NO Arduino
            //
            runWithoutArduino = true;
            println("no Arduino detected. Will run without Arduino. Cheers");
        }
        //
        // find a port whcih is available and see if an arduino is connected to it
        //
        for (i = 0; i < len; i++) {
            //
            // each ports is tested by checking that there is a 'T' in the buffer
            //
            println("Testing port " + Serial.list()[i]);
            //
            // creat a new COM port object
            //
            arduinoPort = new Serial(this, Serial.list()[i], 1200);      // Open 1st port in list
            //
            // wait for USB port reset (2 seconds)
            //
            millisStart = millis();
            while ( (millis () - millisStart) < 2000) ;
            //
            // now wait for the Arduino to request connection
            //
            arduinoPort.bufferUntil('T');                   //buffer until there is a 'T'
            //
            // now send a 'C' to the Arduino
            //
//            arduinoPort.write('C');
            //
            // Now wait for next 'T'
            //
            millisStart = millis();
            while ( (millis () - millisStart) < 100) ;      //collect some chars
            //
            // check if there is a character
            //
            if (arduinoPort.available() > 0)                //if we have a character
            {
                char c = arduinoPort.readChar();            //get the character
                if (c == 'T')        //if we got a 'T'
                {
                    break;        //leave for loop
                }
            } else {
                //
                // apparently not an Arduino port
                //
                arduinoPort.stop();      //if no 'T', stop port
            }
            //
            // no more ports avaialble?
            //
            if (i == len - 1) {
                //
                // no so there is no Arduino connected
                //
                runWithoutArduino = true;
                println("no Arduino detected. Will run without Arduino. Cheers");
            }
        }
        //
        // let's tell the user there is a connection
        //
        if (!runWithoutArduino) {
            //
            // Houston we have a connection
            //
            println("Serial Port used = " + Serial.list()[i]);
            serPortUsed = Serial.list()[i];
            millisStart = millis();
            //
            // wait 5 seconds for everybody to get used to it
            //
            while ( (millis () - millisStart) < 5000) ;
        }
    }
    //
    // finished connecting
    //
    connecting = false;
}

Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 2
Geregistreerd: 08 Jan 2014, 12:08

Re: Arduino en processing laten communiceren

Berichtdoor Diane » 09 Jan 2014, 00:34

Dankjewel! Het is me uiteindelijk gelukt. Ik heb een extra lijn toegevoegd in het processing programma waarmee ik de serial kon uitlezen, uiteindelijk met wat prutsen de boel aan de gang gekregen.

Bedankt voor je hulp ;)

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Arduino en processing laten communiceren

Berichtdoor nicoverduin » 09 Jan 2014, 01:35

Graag gedaan
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Terug naar Overige Software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 3 gasten