Plz help my program

<arduino code>
const int sampleWindow=38;
unsigned int sample;

  unsigned long startMillis=millis();
  unsigned int peakToPeak=0;

  unsigned int signalMax=0;
  unsigned int signalMin=1024;
//clipping indicator variables
boolean clipping = 0;

//data storage variables
byte newData = 0;
byte prevData = 0;
unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally
int timer[10];//sstorage for timing of events
int slope[10];//storage for slope of events
unsigned int totalTimer;//used to calculate period
unsigned int period;//storage for period of wave
byte index = 0;//current storage index
float frequency;//storage for frequency calculations
int maxSlope = 0;//used to calculate max slope as trigger point
int newSlope;//storage for incoming slope data

//variables for decided whether you have a match
byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long
byte slopeTol = 3;//slope tolerance- adjust this if you need
int timerTol = 10;//timer tolerance- adjust this if you need

//variables for amp detection
unsigned int ampTimer = 0;
byte maxAmp = 0;
byte checkMaxAmp;
byte ampThreshold = 50;//raise if you have a very noisy signal

void setup(){
  
  Serial.begin(9600);
  
  pinMode(13,OUTPUT);//led indicator pin
  pinMode(12,OUTPUT);//output pin
  
  cli();//diable interrupts
  
  //set up continuous sampling of analog pin 0 at 38.5kHz
 
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
  
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
  
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
  
  sei();//enable interrupts
}

ISR(ADC_vect) {//when new ADC value ready
  
  PORTB &= B11101111;//set pin 12 low
  prevData = newData;//store previous value
  newData = ADCH;//get value from A0
  if (prevData < 127 && newData >=127){//if increasing and crossing midpoint
    newSlope = newData - prevData;//calculate slope
    if (abs(newSlope-maxSlope)<slopeTol){//if slopes are ==
      //record new data and reset time
      slope[index] = newSlope;
      timer[index] = time;
      time = 0;
      if (index == 0){//new max slope just reset
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
        index++;//increment index
      }
      else if (abs(timer[0]-timer[index])<timerTol && abs(slope[0]-newSlope)<slopeTol){//if timer duration and slopes match
        //sum timer values
        totalTimer = 0;
        for (byte i=0;i<index;i++){
          totalTimer+=timer[i];
        }
        period = totalTimer;//set period
        //reset new zero index values to compare with
        timer[0] = timer[index];
        slope[0] = slope[index];
        index = 1;//set index to 1
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
      }
      else{//crossing midpoint but not match
        index++;//increment index
        if (index > 9){
          reset();
        }
      }
    }
    else if (newSlope>maxSlope){//if new slope is much larger than max slope
      maxSlope = newSlope;
      time = 0;//reset clock
      noMatch = 0;
      index = 0;//reset index
    }
    else{//slope not steep enough
      noMatch++;//increment no match counter
      if (noMatch>9){
        reset();
      }
    }
  }
    
  if (newData == 0 || newData == 1023){//if clipping
    PORTB |= B00100000;//set pin 13 high- turn on clipping indicator led
    clipping = 1;//currently clipping
  }
  
  time++;//increment timer at rate of 38.5kHz
  
  ampTimer++;//increment amplitude timer
  if (abs(127-ADCH)>maxAmp){
    maxAmp = abs(127-ADCH);
  }
  if (ampTimer==1000){
    ampTimer = 0;
    checkMaxAmp = maxAmp;
    maxAmp = 0;
  }
  
}

void reset(){//clea out some variables
  index = 0;//reset index
  noMatch = 0;//reset match couner
  maxSlope = 0;//reset slope
}


void checkClipping(){//manage clipping indicator LED
  if (clipping){//if currently clipping
    PORTB &= B11011111;//turn off clipping indicator led
    clipping = 0;
  }
}


void loop(){
  
  checkClipping();
  
  if (checkMaxAmp>ampThreshold){
    frequency = 38462/float(period);//calculate frequency timer rate/period
  
    //print results
    Serial.print(frequency);
    Serial.println(",");
  }
 
  delay(900);//delete this if you want


  while(millis()-startMillis<sampleWindow)
  {
    sample=analogRead(0);
    if(sample<1024);
    {
    if(sample>signalMax)
    {
      signalMax=sample;
    }
    else if (sample<signalMin)
    {
      signalMin=sample;
    }
  }
}
 peakToPeak = signalMax-signalMin;
 double volts = (peakToPeak * 5.0)/1024;

Serial.println(volts);
Serial.print("\n");
 delay(3000);
    }

x is frequency
y is Sound Size

> import processing.serial.*; //import the Serial library
> Serial myPort;
> int i;
> String message=null;
> 
> 
> 
> float x; // variable holding the value from A0
> float y; // variable holding the value from A1
> 
> 
> String portName;
> String val;
> 
> void settings()
> {
>   size ( 800, 800 ) ; // window size
> delay(3000);
>   
>   // we are opening the port
>    myPort = new Serial(this, Serial.list()[0], 9600);
>   myPort.bufferUntil('\n'); 
>   
> 
> }
> 
> // drawing loop
> void draw()
> {
> 
>   clear() ; // clean the screen
>   
>   background(255,0);
> 
>  
> 
> 
> 
> 
>    for(float x= 0; x < 535; x += 0){
>       fill(255,51, 51);
>       ellipse(random(width),random(height),y/2,y/2);
>    }
>    break;
>      for(float x= 536; x < 595; x += 536){
>       fill(255,102, 51);
>       ellipse(random(width),random(height),y/2,y/2);
>    }
>      for(float x= 596; x < 670; x += 596){
>       fill(255,255, 51);
>       ellipse(random(width),random(height),y/2,y/2);
>    }
>  
>     for(float x= 671; x < 750; x += 671){
>       fill(51,0, 255);
>       ellipse(random(width),random(height),y/2,y/2);
>    }
>     for(float x= 751; x < 9000; x += 751){
>       fill(51,0, 255);
>       ellipse(random(width),random(height),y/2,y/2);
>    }
>   
> }
> 
> 
> // data support from the serial port
>   void serialEvent(Serial i){ 
>   String message = myPort.readStringUntil(10); 
>   if (message != null) 
>   { 
>     print("Received: "); 
>     println(message); 
>     x = float(message); 
>     y =float(message);
> }
> 
> 
>   }

I want visuializing processing code
frequency is color
Sound Size is size
plz why my code isn’t doing
help me

Hi,

Welcome to the community! :wink:

I see that you put a lot of effort into this but putting a long code is quite hard to understand for us. Can you please try to break down your issue into pieces and explain the steps more clearly? (what is it doing, in which way? do you have a graphical example of the result you want to get? even with a sketch on paper)

1 Like

Hi @kokobab In your Processing code, ‘clear’ I think is not needed. The ‘break;’ a few lines after start of draw is not good, and just above it the ‘x += 0’ will remain in that loop forever. On next post please try to avoid the ‘>’ on every line. I suggest you use a boolean so it only draws when something has been received. Something like:


boolean bDoRedraw = false;

void draw() {
  if (bDoRedraw) {
     // all the drawing stuff
    background();
     ...
    bDoRedraw = false;
  }
}

void SerialEvent...{
  bDoRedraw = true;
}
1 Like