Arduino 处理+;位写入+;阿杜伊诺

Arduino 处理+;位写入+;阿杜伊诺,arduino,bit-manipulation,processing,frequency-analysis,Arduino,Bit Manipulation,Processing,Frequency Analysis,我正在与Arduino合作,并与Arduino库进行处理 我得到错误“函数位写入(byte,int,int)不存在。”; 可以看出,processing+Arduino位写入功能不能协同工作。 由于这一行的原因,它被提高了: arduino.bitWrite(data,desiredPin,desiredState); 我在这个项目中的目标是修改一个音乐反应性草图以使用移位寄存器 这是我的全部代码: Arduino_移位_显示器 import ddf.minim.*; import ddf.m

我正在与Arduino合作,并与Arduino库进行处理

我得到错误“函数
位写入(byte,int,int)
不存在。”; 可以看出,processing+Arduino位写入功能不能协同工作。 由于这一行的原因,它被提高了:

arduino.bitWrite(data,desiredPin,desiredState);
我在这个项目中的目标是修改一个音乐反应性草图以使用移位寄存器

这是我的全部代码:

Arduino_移位_显示器

import ddf.minim.*;
import ddf.minim.analysis.*;
import processing.serial.*;
import cc.arduino.*;

int displayNum = 8;

Arduino arduino;
//Set these in the order of frequency - 0th pin is the lowest frequency,
//while the final pin is the highest frequency
int[] lastFired = new int[displayNum];

int datapin = 2; 
int clockpin = 3;
int latchpin = 4;
int switchpin = 7;

byte data = 0;

//Change these to mess with the flashing rates
//Sensitivity is the shortest possible interval between beats
//minTimeOn is the minimum time an LED can be on
int sensitivity = 75;
int minTimeOn = 50;

String mode;
String source;

Minim minim;
AudioInput in;
AudioPlayer song;
BeatDetect beat;

//Used to stop flashing if the only signal on the line is random noise
boolean hasInput = false;
float tol = 0.005;

void setup(){
  // shift register setup
  arduino.pinMode(datapin, arduino.OUTPUT);
  arduino.pinMode(clockpin, arduino.OUTPUT);  
  arduino.pinMode(latchpin, arduino.OUTPUT);
  arduino.digitalWrite(switchpin, arduino.HIGH);

  //Uncomment the mode/source pair for the desired input

  //Shoutcast radio stream
  //mode = "radio";
  //source = "http://scfire-ntc-aa05.stream.aol.com:80/stream/1018";

  //mode = "file";
  //source = "/path/to/mp3";

  mode = "mic";
  source = "";

  size(512, 200, P2D);

  minim = new Minim(this);
  arduino = new Arduino(this, Arduino.list()[1]);


  minim = new Minim(this);

  if (mode == "file" || mode == "radio"){
    song = minim.loadFile(source, 2048);
    song.play();
    beat = new BeatDetect(song.bufferSize(), song.sampleRate());
    beat.setSensitivity(sensitivity);
  } else if (mode == "mic"){
    in = minim.getLineIn(Minim.STEREO, 2048);
    beat = new BeatDetect(in.bufferSize(), in.sampleRate());
    beat.setSensitivity(sensitivity);
  }
}

void shiftWrite(int desiredPin, int desiredState)

// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().

// Like digitalWrite(), this function takes two parameters:

//    "desiredPin" is the shift register output pin
//    you want to affect (0-7)

//    "desiredState" is whether you want that output
//    to be HIGH or LOW

// Inside the Arduino, numbers are stored as arrays of "bits",
// each of which is a single 1 or 0 value. Because a "byte" type
// is also eight bits, we'll use a byte (which we named "data"
// at the top of this sketch) to send data to the shift register.
// If a bit in the byte is "1", the output will be HIGH. If the bit
// is "0", the output will be LOW.

// To turn the individual bits in "data" on and off, we'll use
// a new Arduino commands called bitWrite(), which can make
// individual bits in a number 1 or 0.
{
  // First we'll alter the global variable "data", changing the
  // desired bit to 1 or 0:

  arduino.bitWrite(data,desiredPin,desiredState);

  // Now we'll actually send that data to the shift register.
  // The shiftOut() function does all the hard work of
  // manipulating the data and clock pins to move the data
  // into the shift register:

  arduino.shiftOut(datapin, clockpin, MSBFIRST, data);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                


  // Once the data is in the shift register, we still need to
  // make it appear at the outputs. We'll toggle the state of
  // the latchPin, which will signal the shift register to "latch"
  // the data to the outputs. (Latch activates on the high-to
  // -low transition).

  arduino.digitalWrite(latchpin, arduino.HIGH);
  arduino.digitalWrite(latchpin, arduino.LOW);
}

void draw(){
  if (mode == "file" || mode == "radio"){
    beat.detect(song.mix);
    drawWaveForm((AudioSource)song);
  } else if (mode == "mic"){
    beat.detect(in.mix); 
    drawWaveForm((AudioSource)in);
  }

  if (hasInput){ //hasInput is set within drawWaveForm
    for (int i=0; i<displayNum-1; i++){
      if ( beat.isRange( i+1, i+1, 1) ){
        shiftWrite(i, 1);
        lastFired[i] = millis();
      } else {
        if ((millis() - lastFired[i]) > minTimeOn){
          shiftWrite(i, 0);
        }
      }
    } 
  }
}  //End draw method

//Display the input waveform
//This method sets 'hasInput' - if any sample in the signal has a value
//larger than 'tol,' there is a signal and the lights should flash.
//Otherwise, only noise is present and the lights should stay off.
void drawWaveForm(AudioSource src){
  background(0);
  stroke(255);

  hasInput = false;

  for(int i = 0; i < src.bufferSize() - 1; i++)
  {
    line(i, 50 + src.left.get(i)*50, i+1, 50 + src.left.get(i+1)*50);
    line(i, 150 + src.right.get(i)*50, i+1, 150 + src.right.get(i+1)*50);

    if (!hasInput && (abs(src.left.get(i)) > tol || abs(src.right.get(i)) > tol)){
      hasInput = true;
    }
  } 
}

void resetPins(){
  for (int i=0; i<ledPins.length; i++){
    arduino.digitalWrite(ledPins[i], Arduino.LOW);   
  } 
}

void stop(){
  resetPins();  
  if (mode == "mic"){
    in.close();
  }  
  minim.stop();
  super.stop();
}

使用标准的位运算符也可以实现同样的效果。要打开一点,请执行以下操作:

data |= 1 << bitNumber;
您可以在这里看到相同的位移位操作。但是,它前面有一元否定运算符(
~
)。这会将所有1替换为0,将所有0替换为1。结果与我们以前使用的位掩码完全相反。不过,这次不能执行按位or操作,否则将打开所有其他位。而是使用按位and赋值(
&=
)将此掩码与
数据
变量组合。这可确保所需位已关闭,其余位未被触及

在您的代码中,
desiredPin
相当于
位号


关于按位操作如何工作的完整解释可能相当冗长。如果您需要更多帮助,我建议您在线查找一个好的教程。

还有
位集
位清除
Arduino宏,它们使代码比位移位和使用
更具可读性。格式可以是
位集(要修改的内容,位号)
位清除(要修改的内容,位号)
。这些转换成非常有效的代码,可以用来操作变量和硬件寄存器。例如,如果您想打开Arduino UNO上的引脚13,首先需要查找Arduino引脚13实际上是Atmel atmega328芯片端口B上的引脚5。因此,命令是:

bitSet(PORTB,5);

所以我发现这是由于Arduino处理库的限制。我现在需要另一种方法来处理那个字节的位。有人能推荐一种技巧吗?
data &= ~(1 << bitNumber);
bitSet(PORTB,5);