如何让Arduino运行一个脚本

时间:2015-03-29 15:39:48

标签: arduino

我是一名软件开发人员,但我是Arduino和电子世界的新手。
我想建立一个结合两者的简单项目,并且非常感谢任何帮助从哪里开始。

最终项目应该是带有按钮和LED的Arduino,当点击按钮时我想在我的mac上运行脚本,然后如果脚本成功完成我想打开LED

我已经看过一些关于如何使用按钮和LED的教程 我感兴趣的主要是从Arduino到mac的通信,反之亦然。特别是如何让它在我的Mac上运行脚本。

3 个答案:

答案 0 :(得分:2)

您应该查看Serial class和示例(通过File > Examples > Commmunication

您需要在Arduino端编写一些代码,以便在按下按钮时通过Serial发送数据(这样您就可以触发脚本)并接收数据(脚本完成时)来控制LED。 / p>

以下是Arduino方面的一个粗略示例:

const int btnPin = 12;//button pin
const int ledPin = 13;

int lastButtonState;

void setup(){ 
  //setup pins
  pinMode(btnPin,INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
  //setup communication
  Serial.begin(9600);
}

void loop() {
  int currentButtonState = digitalRead(btnPin);//read button state
  if(lastButtonState != currentButtonState && currentButtonState == LOW){//if the state of the pin changed
    Serial.write(currentButtonState);//send the data
    lastButtonState = currentButtonState;//update the last button state
    //turn on LED
    digitalWrite(ledPin,HIGH);
  }
}
void serialEvent(){//if any data was sent
  if(Serial.available() > 0){//and there's at least 1 byte to look at
    int data = Serial.read();//read the data
    //do something with it if you want
    //turn off the LED
    digitalWrite(ledPin,LOW);
  }
}

请注意,您的设置中可能有不同的引脚编号,并且根据按钮的连接方式,您将在检查{{1}的条件中查找LOWHIGH值}}。

就沟通而言,有几个关键要素:

currentButtonState

以波特率9600启动串行通信。您需要在另一端匹配此波特率以确保正确通信。

Serial.begin(9600); ;

将数据发送到端口。

Serial.write()

在Arduino中预定义,并在新的串行数据到达时被调用。

在脚本方面,您还没有提到语言,但原理是相同的:您需要知道端口名称和波特率才能与Mac建立通信。

端口是您用来上传Arduino代码的端口(类似serialEvent() ),在这种特殊情况下,波特率为9600.您应该能够在Arduino IDE中使用串行监视器进行测试。

你的脚本将是那里的行(伪代码)

/dev/tty.usbmodem####

请务必查看Interfacing with Software以查找您选择的脚本语言指南

<强>更新

以下是使用Processing使用它Serial library与arduino进行交互的快速示例。所以在Arduino方面,这里是一个最小的草图:

open serial connection ( port name, baudrate = 9600 )
poll serial connection
   if there is data
      run the script you need
      script finished executing, therefore send data back via serial connection

并在处理方面:

const int btnPin = 12;//button pin
const int ledPin = 13;

boolean wasPressed;

char cmd[] = "/Applications/TextEdit.app\n";

void setup(){ 
  //setup pins
  pinMode(btnPin,INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
  //setup communication
  Serial.begin(9600);
}

void loop() {
  int currentButtonState = digitalRead(btnPin);//read button state
  if(!wasPressed && currentButtonState == LOW){//if the state of the pin changed
    Serial.write(cmd);//send the data
    wasPressed = true;//update the last button state
    //turn on LED
    digitalWrite(ledPin,HIGH);
  }
  if(currentButtonState == HIGH) wasPressed = false;
}
void serialEvent(){//if any data was sent
  if(Serial.available() > 0){//and there's at least 1 byte to look at
    int data = Serial.read();//read the data
    //do something with it if you want
    //turn off the LED
    digitalWrite(ledPin,LOW);
  }
}

希望这是一个有用的概念证明。随意使用任何其他语言和串行库,而不是处理。语法可能略有不同(可能更多关于进程/命令的运行),但概念是相同的。

答案 1 :(得分:1)

Dunno如果能像你一样帮助你,但这个问题显示了一个简单的例子,说明如何在Android应用程序中使用简单按钮编写脚本

Run script with android app

希望有所帮助

答案 2 :(得分:0)

我在Linux中找到了一种解决方法。它有点乱,但它有效。我使用Coolterm将arduino的串行输出捕获到一个文本文件中,我编写了一个小的python脚本来读取文件(或者简单地说,就我的情况而言,如果文件不为空,则执行我想要的命令)。