Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何通过popen发送整数?_Cocoa_Arduino_Popen - Fatal编程技术网

Cocoa 如何通过popen发送整数?

Cocoa 如何通过popen发送整数?,cocoa,arduino,popen,Cocoa,Arduino,Popen,我需要能够将一个整数从cocoa发送到arduino 发送字符很容易,例如一位数的整数,但我似乎找不到发送两位数和三位数整数的方法 其目的是将LED的亮度连续控制在0到255之间 到目前为止,我可以使用以下代码打开和关闭它: int ledPin = 9; // LED connected to digital pin 9 int incomingByte = 0; // for incoming serial data void setup() {

我需要能够将一个整数从cocoa发送到arduino

发送字符很容易,例如一位数的整数,但我似乎找不到发送两位数和三位数整数的方法

其目的是将LED的亮度连续控制在0到255之间

到目前为止,我可以使用以下代码打开和关闭它:

int ledPin =  9;    // LED connected to digital pin 9
int incomingByte = 0;   // for incoming serial data


void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()                     
{

  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if(incomingByte == 105){         //105 corresponds to i and is programmed in cocoa to turn the LED on
      digitalWrite(ledPin, HIGH);
    }
    else if(incomingByte == 111){    //111 corresponds to o and is programmed in cocoa to turn the LED on
      digitalWrite(ledPin, LOW);
    }
  }
}
但是,我无法确定如何将值设置在0和255之间。我将使用“AnalogWrite”代替“digitalWrite”,但是,我不知道如何将incomingByte发送为介于0和255之间的值

这是cocoa代码:

#import "MainController.h"

@implementation MainController


-(IBAction)ledOn:(id)sender{
        popen("echo i > /dev/cu.usbmodem1411", "r");

}

-(IBAction)ledOff:(id)sender{
        popen("echo o > /dev/cu.usbmodem1411", "r");
}


@end

谢谢。

您可以在arduino代码中将包含多个数字的整数视为cstring,然后通过
atoi()
将其转换为整数

以下代码将从串行缓冲区捕获字节作为cstring:

char buffer[MAX_BUFFER_SIZE]; // global c character array variable

boolean inputReady()
{
  byte index = 0;
  byte avail = Serial.available();
  if(avail > 0)
  {
    while(index < avail)
    {
      byte val;
      do
      {
        val = Serial.read();
      }
      while (val == -1); // if value is no longer -1, bytes are captured
      buffer[index] = val;
      index++;
    }
    buffer[index] = 0; //terminate the character array
    return true;
  }
  return false;
}
然后,您可以通过
popen()
从cocoa代码中以字符串形式发送pwm值

我已经在我的Arduino Uno上进行了测试,可能会在其他变体上使用。我希望这对你的项目有帮助,祝你好运

void loop()
{
  if(inputReady())
  {
    int pwmValue = atoi(buffer); // convert ascii buffer to integer.
    if(pwmValue >= 0 && pwmValue <= 255) // filter values from 0-255
       analogWrite(ledPin, pwmValue);
    else
       digitalWrite(ledPin, LOW); // turn off if pwmValue is more than 255
  }
  delay(100);
}
#import "MainController.h"

@implementation MainController


-(IBAction)ledPWMValue:(id)sender{
        popen("echo 254 > /dev/cu.usbmodem1411", "r");

}

@end