ultrasonic wave

Hi guys I wrote this code to generate wave from the ultrasonic sensor but when I changed or delete the OCR value nothing happens do you have any advice for me?

#include "LiquidCrystal.h"  //lcd libary 
#define myOutputPin1 8  
#define myOutputPin2 9                                     
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);   //LCD object Parameters: (rs, enable, d4, d5, d6, d7)
int ledPins[] = {0,1,2,3,4,5,6,7};
int i;
const int trigPin = 12; //trig pin connection
const int echoPin = 11;  //echopin connection
long duration;
int distanceCm;
float liquid;
int x;
int toggle0 = 0;
int toggle1 = 0;                                                                                                         
void setup() {      // setup perameter
lcd.begin(16,2);                                                   
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(0,0);
lcd.print("  Distance    ");
lcd.setCursor(0,1);
lcd.print("  Measurement  ");
delay(2000);
lcd.clear();
for(int i = 0; i <= 7; i++){       
pinMode(ledPins[i],OUTPUT);
}
pinMode(myOutputPin1,OUTPUT);
pinMode(myOutputPin2,OUTPUT);
cli();//stop interrupts
 TCCR2A = 0;// set entire TCCR2A register to 0
 TCCR2B = 0;// same for TCCR2B
 TCNT2  = 0;//initialize counter value to 0
  // set compare match register for 2khz increments
 OCR2A = x;// = (16*10^6) / (2000*64) - 1 (must be <256)
  // turn on CTC mode
 TCCR2A |= (1 << WGM21);
  // Set CS22 and CS21 bits for 256 prescaler
 TCCR2B |= (1 << CS22)|(1<<CS21) ;   
  // enable timer compare interrupt
 TIMSK0 |= (1 << OCIE0A);
 TCCR1A = 0;// set entire TCCR2A register to 0
TCCR1B = 0;// same for TCCR2B
TCNT1  = 0;//initialize counter value to 0
 // set compare match register for 2khz increments
OCR1A = x;// = (16*10^6) / (2000*64) - 1 (must be <256)
 // turn on FPWM mode
TCCR1B |= (1 << WGM13)&(1<<WGM12);
TCCR1A |= (1 << WGM11)&(1<<WGM10);
 // Set CS12 bits for 256 prescaler
TCCR1B |= (1<<CS12) ;
TIMSK1 |= (1 << OCIE1A);
 // enable timer compare interrupt
 sei();//allow interrupts
}
ISR(TIMER0_COMPA_vect){//timer0 interrupt 2kHz toggles pin 8
//generates pulse wave of frequency 2kHz/2 = 1kHz (takes two cycles for full wave- toggle high then toggle low)
  if (toggle0){
    digitalWrite(myOutputPin1,HIGH);
    toggle0 = 0;
  }
  else{
    digitalWrite(myOutputPin1,LOW);
    toggle0 = 1;
  }
}
 ISR(TIMER1_COMPA_vect){//timer1 interrupt 2kHz toggles pin 9
//generates pulse wave of frequency 2kHz/2 = 1kHz (takes two cycles for full wave- toggle high then toggle low)
 if (toggle1){
   digitalWrite(myOutputPin2,HIGH);
   toggle1 = 0;
 }
 else{
   digitalWrite(myOutputPin2,LOW);
   toggle1 = 1;
 }
}
void loop() {   // loop of flow program
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;                                                                                 
delay(10);
lcd.setCursor(0,0);
lcd.print("Distance:");
lcd.print(distanceCm);
lcd.print(" Cm ");
delay(10);
int x=map(distanceCm,3,422,0,255);

    showBinNumber(x);
   delay(10);
  }

void showBinNumber(int num) {
  for (int i=0; i<8; i++) {
    if (num%2)
      digitalWrite(ledPins[i], LOW);
    else
      digitalWrite(ledPins[i], HIGH);
    num/=2;
  }
}

2kHz isn’t ultrasonic.
What are you trying to do?

Please use the IDE’s auto-format tool before posting code.

You are using Timer1 and Timer2. Why are you defining the ISR for Timer0?!?

ISR(TIMER0_COMPA_vect) //timer0 interrupt 2kHz toggles pin 8
{

Warning: If the ultrasonic sensor doesn't receive an echo (too close or too far) you will get a distance of 0. This will map to a negative number since 0 is less than 3.

 int x = map(distanceCm, 3, 422, 0, 255);

johnwasser:
You are using Timer1 and Timer2. Why are you defining the ISR for Timer0?!?

ISR(TIMER0_COMPA_vect) //timer0 interrupt 2kHz toggles pin 8

{



Warning: If the ultrasonic sensor doesn't receive an echo (too close or too far) you will get a distance of 0. This will map to a negative number since 0 is less than 3.


int x = map(distanceCm, 3, 422, 0, 255);

I just saw that I tried now to use timer2 but the LCD went off but the ultrasonic is working

TheMemberFormerlyKnownAsAWOL:
2kHz isn’t ultrasonic.
What are you trying to do?

Please use the IDE’s auto-format tool before posting code.

sorry for that I am new to the Arduino can you be more specific.
I mean I am taking the OCR value from the ultrasonic value (x).
this is the code but when I used timer2 LCD went off.

#include "LiquidCrystal.h"  //lcd libary 
#define myOutputPin1 8
#define myOutputPin2 9
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);   //LCD object Parameters: (rs, enable, d4, d5, d6, d7)
int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int i;
const int trigPin = 12; //trig pin connection
const int echoPin = 11;  //echopin connection
long duration;
int distanceCm;
float liquid;
int x;
int toggle0 = 0;
int toggle1 = 0;
void setup() {      // setup perameter
  lcd.begin(16, 2);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.setCursor(0, 0);
  lcd.print("  Distance    ");
  lcd.setCursor(0, 1);
  lcd.print("  Measurement  ");
  delay(2000);
  lcd.clear();
  for (int i = 0; i <= 7; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(myOutputPin1, OUTPUT);
  pinMode(myOutputPin2, OUTPUT);
  cli();//stop interrupts
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0
  // set compare match register.
  OCR2A = x;
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS22 and CS21 bits for 256 prescaler
  TCCR2B |= (1 << CS22) | (1 << CS21) ;
  // enable timer compare interrupt
  TIMSK0 |= (1 << OCIE0A);
  TCCR1A = 0;// set entire TCCR2A register to 0
  TCCR1B = 0;// same for TCCR2B
  TCNT1  = 0;//initialize counter value to 0
  OCR1A = x;
  // turn on FPWM mode
  TCCR1B |= (1 << WGM13) & (1 << WGM12);
  TCCR1A |= (1 << WGM11) & (1 << WGM10);
  // Set CS12 bits for 256 prescaler
  TCCR1B |= (1 << CS12) ;
  TIMSK1 |= (1 << OCIE1A);
  // enable timer compare interrupt
  sei();//allow interrupts
}
ISR(TIMER2_COMPA_vect) {
  if (toggle0) {
    digitalWrite(myOutputPin1, HIGH);
    toggle0 = 0;
  }
  else {
    digitalWrite(myOutputPin1, LOW);
    toggle0 = 1;
  }
}
ISR(TIMER1_COMPA_vect) {
  if (toggle1) {
    digitalWrite(myOutputPin2, HIGH);
    toggle1 = 0;
  }
  else {
    digitalWrite(myOutputPin2, LOW);
    toggle1 = 1;
  }
}
void loop() {   // loop of flow program
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;
  delay(10);
  lcd.setCursor(0, 0);
  lcd.print("Distance:");
  lcd.print(distanceCm);
  lcd.print(" Cm ");
  delay(10);
  int x = map(distanceCm, 3, 422, 0, 255);

  showBinNumber(x);
  delay(10);
}

void showBinNumber(int num) {
  for (int i = 0; i < 8; i++) {
    if (num % 2)
      digitalWrite(ledPins[i], LOW);
    else
      digitalWrite(ledPins[i], HIGH);
    num /= 2;
  }
}

I mean I am taking the OCR value from the ultrasonic value (x).

.How?

The only x that I can see that goes anywhere near the timer registers is zero.

TheMemberFormerlyKnownAsAWOL:
.How?

I can't do this? xD sorry I am really new.
that what I am trying to do:-

(Use this digital value as a comparator of a Timer 2, CTC mode to change the frequency of the output a wave on pin OCR2A)

after converting the ultrasonic value to digital. correct me if I am wrong

Can you show me where you think this is happening?
I can’t see it.

TheMemberFormerlyKnownAsAWOL:
Can you show me where you think this is happening?
I can't see it.

hmm, I am trying to solve it for like 5 days. I thought that I can take the x value from the ultrasonic and put it in the OCR to generate the wave I am really confused sir.
plus when I delete the OCR line from the code nth change in proteus.

i think i will try again thx for helping man

int x=map(distanceCm,3,422,0,255);

Note that this line creates a new LOCAL variable named 'x'. This is different from the GLOBAL variable named 'x' that you store in the OCR registers when the interrupts happen. That would explain why the ultrasonic distance sensor has no effect on the timers.

You probably wanted:

x = constrain(map(distanceCm,3,422,0,255), 0, 255);

The constrain() fixes with the problem of 0 mapping to a negative value and values over 422cm mapping to a value over 255.

johnwasser:

int x=map(distanceCm,3,422,0,255);

Note that this line creates a new LOCAL variable named 'x'. This is different from the GLOBAL variable named 'x' that you store in the OCR registers when the interrupts happen. That would explain why the ultrasonic distance sensor has no effect on the timers.

You probably wanted:

x = constrain(map(distanceCm,3,422,0,255), 0, 255);

The constrain() fixes with the problem of 0 mapping to a negative value and values over 422cm mapping to a value over 255.

will even if I removed the OCR line from the code it doesn't even matter there is a wave generated from somewhere idk