difficulty connecting arduino to processing & difficulty in servo control T_T

edited November 2016 in Arduino

Hi there,

Recently I want to make a project using processing to capture mind wave and send 0, 1, 2 to Arduino to control a servo. I encountered 3 problems:

1, Processing's serial cannot transmit to Arduino, but it can send 0 at 1st time, and then nothing works.

2, My servo does not stick to the angle I wrote to him. It keeps spinning around and around..

3, I intend to make servo turn clock wise when receiving '1', and anticlockwise receiving '2', but it dose not do anticlockwise all the time, it turns anticlockwise for 3seconds, and then turn clockwise, and repeat.....

Following are my codes. Each of the problems being solved can save me a step from the fire of July.......

Processing:

//Thinkgear
import neurosky.*;
import org.json.*;

//Arduino
import cc.arduino.*;
import org.firmata.*;
import processing.serial.*;

ThinkGearSocket neuroSocket;
int attention=10;
int meditation=10;
int attentionBrightness = 0;

int attentionValue = 40;
int meditationValue = 40;

Arduino arduino;
Serial myPort;  // Create object from Serial class
int ledPin = 3;
int servoPin = 9;



void setup() 
{
  size(600,600); 

  //ThinkGear Connection
  ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
  try {neuroSocket.start();}
  catch (Exception e) {println("Is Thinkgear running?");}
  smooth();
  frameRate(10);

  //Arduino Connection 
  String portName = Serial.list()[5]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  printArray(Serial.list());
}


void draw() {

  visualisation();

  //case 0: halt & LED BLINK 
  if (attention<attentionValue)  
    {
      if(meditation<=meditationValue)
      { myPort.write('0');  println("0"); } 

   //case 1:  close & LED OFF
      else if (meditation>meditationValue)
     { myPort.write('1');   println("1"); }   
    }

  //case 2:  open & LED ON
  else if (attention>attentionValue)
  { myPort.write('2');  println("2"); }
}


////alternative test
//   if (mousePressed == true) 
//   { myPort.write('0');  println("0"); } 

//   //case 1:  close & LED OFF
//   if (mousePressed==false && keyPressed==false)
//   { myPort.write('1');   println("1"); }   

//  //case 2:  open & LED ON
//  if (keyPressed==true)
//  { myPort.write('2');  println("2");}
//}


void poorSignalEvent(int sig) {
  println("SignalEvent "+sig);
}

public void attentionEvent(int attentionLevel) 
{
  println("Attention Level: " + attentionLevel);
  attention = attentionLevel;
}


void meditationEvent(int meditationLevel) 
{
  println("Meditation Level: " + meditationLevel);
  meditation = meditationLevel;
}

void stop() {
  neuroSocket.stop();
  super.stop();
}

and Arduino,

#include <VarSpeedServo.h>

//initiate
VarSpeedServo myservo;                            
int pos = 0;    
int servoPin = 9;
int ledPin = 3;


//conncect to processing
char val;


void setup() 
{ 
  //led
  pinMode(ledPin,OUTPUT);

  //servo
  myservo.attach(servoPin);  
  myservo.write(0,255,true);  //reset position
  delay(1000);
  Serial.begin(9600);
} 

void loop() 
{ 
  if (Serial.available())
  val = Serial.read();
  if(val =='1')
  {
  digitalWrite(ledPin,LOW);
  myservo.attach(servoPin);  
  int pos=0;
  while(pos < 90)        // from 0 to 180   
  {                                                  
    myservo.write(pos,10,true);        
    Serial.println(myservo.read());
    delay(5);    
    pos += 1;                      
  } 

//  digitalWrite(servoPin,LOW);
  delay(20);
  }

  if (val =='2')
  {
  digitalWrite(ledPin,HIGH);
  myservo.attach(servoPin);  
  int pos = 90;
  while(pos>=1)   //from 180 to 0  
  {                                
    myservo.write(pos,10,true);         
    Serial.println(myservo.read());
    pos-=1;
   digitalWrite(servoPin, HIGH);
   delay(20); 
  } 
  }

  else if (val == '0')
  {
  digitalWrite(ledPin, HIGH);  
  delay(1000);              
  digitalWrite(ledPin, LOW);    
  delay(1000);              
  myservo.detach();
  }
  }

Answers

Sign In or Register to comment.