0

我正在使用 Arduino 并使用 Arduino 库进行处理。

我收到错误“该函数bitWrite(byte, int, int)不存在。”;它接缝处理+ Arduino bitWrite 函数不能一起工作。由于这条线,它被提出:

arduino.bitWrite(data,desiredPin,desiredState);

我在这个项目中的目标是修改音乐反应草图以使用移位寄存器。

这是我的完整代码:

Arduino_Shift_display

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();
}

节拍监听器

class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioPlayer source;

  BeatListener(BeatDetect beat, AudioPlayer source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }

  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }

  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}
4

2 回答 2

2

您可以使用标准的按位运算符来实现相同的目的。要打开一点:

data |= 1 << bitNumber;

右侧 ( 1 << bitNumber) 是用于创建合适的位掩码的位移操作。它采用单个“1”位并将其向左移动,直到到达所需位置。按位或赋值 ( |=) 将新的位掩码与data. 这将打开所需的位,但其余部分保持不变。

关闭的代码略有不同:

data &= ~(1 << bitNumber);

您可以在此处看到相同的位移操作。但是,它前面是一元否定运算符 ( ~)。这会将所有的 1 交换为 0,并将所有的 0 交换为 1。结果与我们之前使用的位掩码完全相反。不过这次您不能进行按位或运算,否则您将打开所有其他位。按位与赋值 ( &=) 用于将此掩码与data变量组合。这确保了所需的位被关闭,而其余的则保持不变。

在您的代码中,desiredPin相当于bitNumber.

对按位运算如何工作的完整解释可能非常冗长。如果您需要更多帮助,我建议您在线寻找一个好的教程。

于 2013-11-02T01:32:24.097 回答
0

还有bitSetbitClearArduino 宏可以使代码比位移和使用AND和更具可读性OR。格式为bitSet(what_to_modify,bit_number)bitClear(what_to_modify,bit_number)。这些转换为非常有效的代码,可用于操作变量和硬件寄存器。例如,如果您想打开 Arduino UNO 上的引脚 13,您首先需要查看 Arduino 引脚 13 实际上是 Atmel atmega328 芯片的 PORTB 上的引脚 5。所以命令是:

bitSet(PORTB,5);
于 2015-06-11T22:39:35.277 回答