Switching from Uno to Mega 2560 with no success!!

Hello!

I have an IDE code, that includes a couple of ultrasound sensors and the PCM library. When i put my hand closer to the ultrasound sensors, it plays a word i recorded at a higher frequency, as the hand gets closer.

It works perfectly on the Uno.

Now i want to put in several more ultrasound sensors, but don't have enough pin input places on the Uno.

So, i checked out again that the code works perfectly on the Uno and first of all i tried to switch to the exact pin numbers, ground connections and 5V connections, on the Mega, without adding any new ultrasound sensors. But, nothing happens - the speaker is totally silent! i checked and rechecked if i switched all the wires to the exact same places and yet all is the same, but the speaker is not working, then i switched back to the Uno again - and it is working! Back again to the Mega - Not working!!

What should i do?

What might be the reason?

Thanks a lot!!

Post your code. Likely related to which pins support PWM.

Hello CrossRoads!
Thanks for replying!

Please take into account, that i am only a beginner hobbyist, taking patches of code from here and there and putting them together, with no real in-depth understanding…

Here is the code:

(code tags added by moderator for beginner hobbyist)

#include <PCM.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

int isObstacle=0;
int ObstacleLoop=0;

int leftObstacleLoop1=0;
int leftObstacleLoop2=0;
int leftObstacleLoop3=0;

int rightObstacleLoop1=0;
int rightObstacleLoop2=0;
int rightObstacleLoop3=0;

int winFlag=0;

//const unsigned char sample[] PROGMEM = {
//  126, 127, 128, 128, 128,  127,  //Took out all the mass of numbers
//};

const unsigned char sample_left[] PROGMEM = {
129, 128, 128, 127, 128, 127, 127, 128,  //Took out all the mass of numbers
};

const unsigned char sample_right[] PROGMEM = {
126, 127, 127, 126, 127,  128, 128,  //Took out all the mass of numbers
};

const unsigned char silence[] PROGMEM = {
  126, 127, 127, 126, 127, //Took out all the mass of numbers
};

#define trigPin 6
#define echoPin 7
#define trigPin1 12
#define echoPin1 13

void setup() {
  
   mySerial.begin(9600);
   
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
}

void loop() {
  
   //mySerial.println("XXX?"); 
   
     long duration, distance, duration1, distance1;
  
   digitalWrite(trigPin, LOW);  // Added this line
   delayMicroseconds(4); // Added this line
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); // Added this line
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration/2) / 29.1;
  
 
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(4); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
 
 
  if(winFlag==0 && isObstacle==0){
    mySerial.write(byte(0));
      // winFlag=1; 
 
 }
     
   
 if (distance <= 200 && distance < distance1){
       
     startPlayback(sample_left, sizeof(sample_left));
     
     winFlag=1;
    
     if (distance <=200 && distance >=50) {
     isObstacle=0;
     leftObstacleLoop1=0;
     leftObstacleLoop2=0;
     leftObstacleLoop3=0;  
     
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=1;}
      
    ObstacleLoop=1;  
    startPlayback(silence, sizeof(silence));
    delay(250);
  
     
      }
         
    else  if (distance <=50 && distance >=28) {
     isObstacle=0;
     leftObstacleLoop1=0;
     leftObstacleLoop2=0;
     leftObstacleLoop3=0;  
     
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=1;}
      
    ObstacleLoop=1;  
    startPlayback(silence, sizeof(silence));
    delay(250);
     
      }
      
 else if (distance <=28 && distance >=24) {
     isObstacle=0;
     leftObstacleLoop1=0;
     leftObstacleLoop2=0;
     leftObstacleLoop3=0;  
     
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=1;}
      
    ObstacleLoop=1;  
      delay(2440);
      }
       else if (distance <=24 && distance >=20){
     isObstacle=0;
     leftObstacleLoop1=0;
     leftObstacleLoop2=0; 
     leftObstacleLoop3=0; 
          
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=0;}
      
      ObstacleLoop=1;  
       delay(1500);
       }
        else if (distance <=20 && distance >=16){
       isObstacle=0;
       leftObstacleLoop1=0;
       leftObstacleLoop2=0; 
       leftObstacleLoop3=0; 
            
        if(ObstacleLoop==0){
        mySerial.write(byte(0));
        winFlag=0;}
      
        ObstacleLoop=1;  
        delay(1080);
        }      
         else if (distance <=16 && distance >=12){
         isObstacle=0;
         leftObstacleLoop1=0;
         leftObstacleLoop2=0; 
         leftObstacleLoop3=0; 
              
        if(ObstacleLoop==0){
        mySerial.write(byte(0));
        winFlag=0;}
        
          ObstacleLoop=1;  
        delay(740);
         }
          else if (distance <=12 && distance >=8){
             if(leftObstacleLoop3==0)
             mySerial.write(byte(1));
             isObstacle=1;
             ObstacleLoop=0;
              leftObstacleLoop3++;
              winFlag=0;
              
          delay(530);
          }
           else if (distance <=8 && distance >=4){
            if(leftObstacleLoop2==0)
             mySerial.write(byte(1));
             isObstacle=1;
              ObstacleLoop=0;
              leftObstacleLoop2++;
               winFlag=0;
              
           delay(340);
           }
           else if (distance <=4 && distance >=0){
             if(leftObstacleLoop1==0)
             mySerial.write(byte(1));
             isObstacle=1;
              ObstacleLoop=0;
              leftObstacleLoop1++;
               winFlag=0;
               
              delay(180);
           }
            else {
             isObstacle=0;
             leftObstacleLoop1=0;
             leftObstacleLoop2=0;
             leftObstacleLoop3=0; 
                  
             if(ObstacleLoop==0)
              mySerial.write(byte(0));
              ObstacleLoop=1; 
               startPlayback(silence, sizeof(silence));
            }
    
  }
  else  startPlayback(silence, sizeof(silence));
  
 
  if (distance1 <= 200 && distance1 < distance){
       
     startPlayback(sample_right, sizeof(sample_right));
     
     winFlag=1;
     
           if (distance1 <=200 && distance1 >=50) {
     isObstacle=0;
        rightObstacleLoop1=0;
        rightObstacleLoop2=0; 
        rightObstacleLoop3=0; 
          
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=0;}
      
      ObstacleLoop=1;  
      startPlayback(silence, sizeof(silence));
      delay(250);
         
    }
    
     else if (distance1 <=50 && distance1 >=28) {
     isObstacle=0;
        rightObstacleLoop1=0;
        rightObstacleLoop2=0; 
        rightObstacleLoop3=0; 
          
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=0;}
      
      ObstacleLoop=1;  
      
      startPlayback(silence, sizeof(silence));
      delay(250);
         
    }
            
    else if (distance1 <=28 && distance1 >=24) {
     isObstacle=0;
        rightObstacleLoop1=0;
        rightObstacleLoop2=0; 
        rightObstacleLoop3=0; 
          
      if(ObstacleLoop==0){
      mySerial.write(byte(0));
      winFlag=0;}
      
      ObstacleLoop=1;  
      delay(2440);
      }
       else if (distance1 <=24 && distance1 >=20){
       isObstacle=0;
         rightObstacleLoop1=0;
         rightObstacleLoop2=0; 
         rightObstacleLoop3=0; 
            
        if(ObstacleLoop==0){
        mySerial.write(byte(0));
        winFlag=0;}
      
        ObstacleLoop=1;  
       delay(1500);
       }
        else if (distance1 <=20 && distance1 >=16){
       isObstacle=0;
        rightObstacleLoop1=0;
        rightObstacleLoop2=0; 
        rightObstacleLoop3=0;  
            
        if(ObstacleLoop==0){
        mySerial.write(byte(0));
        winFlag=0;}
      
        ObstacleLoop=1;  
        delay(1080);
        }      
         else if (distance1 <=16 && distance1 >=12){
       isObstacle=0;
       rightObstacleLoop1=0;
       rightObstacleLoop2=0; 
       rightObstacleLoop3=0; 
            
        if(ObstacleLoop==0){
        mySerial.write(byte(0));
        winFlag=0;}
      
        ObstacleLoop=1;  
        delay(740);
         }
          else if (distance1 <=12 && distance1 >=8){
             if(rightObstacleLoop3==0)
             mySerial.write(byte(2));
             isObstacle=1;
              ObstacleLoop=0;
              rightObstacleLoop3++;
               winFlag=0;
               
          delay(530);
          }
                else if (distance1 <=8 && distance1 >=4){
                 if(rightObstacleLoop2==0)
                 mySerial.write(byte(2));
                  isObstacle=1;
                  ObstacleLoop=0;
                  rightObstacleLoop2++;
                   winFlag=0;
                   
                  delay(340);
           }
           else if (distance1 <=4 && distance1 >=0){
             if(rightObstacleLoop1==0)
             mySerial.write(byte(2));
             isObstacle=1;
              ObstacleLoop=1;  
              rightObstacleLoop1++;
               winFlag=0;
               
              delay(180);
           }
            else {
               isObstacle=0;  
           rightObstacleLoop1=0;
           rightObstacleLoop2=0; 
           rightObstacleLoop3=0;
                
            if(ObstacleLoop==0)
            mySerial.write(byte(0));
            ObstacleLoop=1;  
               startPlayback(silence, sizeof(silence));
            }
            
      }
       else  startPlayback(silence, sizeof(silence));
       
    
}