Question

I am working with an Arduino and Processing with the Arduino library.

I get the error "The function bitWrite(byte, int, int) does not exist."; it seams that processing + Arduino bitWrite function are not working together. its raised due to this line:

arduino.bitWrite(data,desiredPin,desiredState);

my goal in this project is modifying a music reactive sketch to work with shift registers.

Here is my full code:

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);
  }
}
Was it helpful?

Solution

You can achieve the same thing using standard bitwise operators. To turn a bit on:

data |= 1 << bitNumber;

The right-hand side (1 << bitNumber) is a bit-shift operation to create a suitable bit-mask. It takes the single '1' bit and moves it left until it reaches the desired position. The bitwise-or assignment (|=) combines that new bit-mask with the existing bits in data. This turns the desired bit on, but leaves the rest untouched.

The code to turn a bit off is slightly different:

data &= ~(1 << bitNumber);

You can see the same bit-shift operation here. However, it's preceded by the unary negation operator (~). This swaps all the 1's for 0's, and all the 0's for 1's. The result is the exact opposite of the bit-mask we used before. You can't do a bitwise-or operation this time though, or else you'll turn all the other bits on. The bitwise-and assignment (&=) is used instead to combine this mask with the data variable. This ensures the desired bit is turned off, and the rest are untouched.

In your code, desiredPin is the equivalent of bitNumber.

A full explanation of how bitwise operations work can be quite lengthy. I'd recommend looking for a good tutorial online if you need more help with that.

OTHER TIPS

There are also the bitSet and bitClear Arduino macros that make the code a little more readable than bit shifting and using AND and OR. The format is either bitSet(what_to_modify,bit_number) and bitClear(what_to_modify,bit_number). These translate into very efficient code and can be used to manipulate both, variables and hardware registers. So for example, if you wanted to turn on pin 13 on the Arduino UNO, you would first need to look up that Arduino pin 13 is actually pin 5 on PORTB of the Atmel atmega328 chip. So the command would be:

bitSet(PORTB,5);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top