2013-03-26 86 views
0

我正在使用Arduino和處理Arduino庫。處理+ bitWrite + 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(); 
} 

BeatListener

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); 
    } 
} 
+0

所以我發現這是由於Arduino處理庫的限制。我現在需要另一種方法來操縱該字節的位。任何人都可以提出一種技術 – skyfly200 2013-03-28 17:25:21

回答

1

您可以使用標準的位操作實現同樣的事情。把一個位上:

data |= 1 << bitNumber; 

右手側(1 << bitNumber)是一個位移位操作以創建一個合適的位掩碼。它需要單個'1'位並向左移動,直到達到所需的位置。按位或分配(|=)將該新位掩碼與data中的現有位組合在一起。這將打開所需的位,但保持其餘不變。

把咬下的代碼略有不同:

data &= ~(1 << bitNumber); 

你可以在這裏看到同樣的比特移位操作。但是,它的前面是一元否定運算符(~)。這將所有的1交換爲0,所有的0交換爲1。結果與之前使用的位掩碼完全相反。儘管這次你不能進行按位或操作,否則你會打開所有其他位。使用按位分配(&=)代替將此掩碼與data變量組合。這確保了所需的位被關閉,其餘的都未被觸及。

在您的代碼中,desiredPin相當於bitNumber

關於按位運算如何工作的完整說明可能會很長。如果你需要更多的幫助,我建議你在網上尋找一個很好的教程。

0

還有bitSetbitClear Arduino宏使得代碼比位移更易讀,使用ANDOR。格式是bitSet(what_to_modify,bit_number)bitClear(what_to_modify,bit_number)。這些轉換成非常高效的代碼,可用於操縱變量和硬件寄存器。例如,如果你想打開Arduino UNO的引腳13,你首先需要查看Arduino引腳13實際上是Atmel atmega328芯片PORTB上的引腳5。所以命令將是:

bitSet(PORTB,5);