Problem with Arduino Uno

Hi All,
I have just purchased 3 new Uno boards which it says are the upgrades to the ATMEGA 328. I have been using an ATMEGA 328 along with a PIR motion detector and it works perfectly, I have 4 of these PIRS and tested all with the ATMEGA 328 and they work fine. Problem comes when I try to run the PIR with UNO, the sensors become extremely unstable and of no use for my task. Of course I have checked serial ports and boards etc, the wiring and code are both exactly the same for the ATMEGA 328 and UNO. I should make it clear that I'm running one sensor with one Arduino at a time.
Anyone else experienced similar problems - I'd be so happy for a solution.

Many thanks,

Frank

Sounds strange...
Can you post your code and give a link to the datasheet of the PIR?

Does the UNO run other sketches right?

Hi Robtillaart,
I've tested all the UNOs with the blink sketch all work as they should.
Hesr is my Arduino code and I have included my Processing code as the sketch outputs a sound in Processing. All of this works perfectly with the ATMEGA 328
Link to motion sensor/////http://www.skpang.co.uk/catalog/pir-motion-sensor-p-796.html

Many thanks for your attention. Frank

Arduino

/*

  • PIR sensor tester
    */

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
float startms = millis();

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
digitalWrite(ledPin, 0);
}

void loopTEST(){
float currentms = millis() - startms;

if (currentms > 500){

val = !val;
digitalWrite(ledPin, val);
startms = millis();
}
}

void loop(){
delay(100);
val = digitalRead(inputPin); // read input value
digitalWrite(ledPin, val); // turn LED ON or OFF

// Serial.print(val) BUT SEE BELOW. DON'T UNCOMMENT!!!!
if (val == 1){

Serial.print(1, BYTE); // There is a potential reason for this verbose version:--

}else{
Serial.print(0, BYTE);
}
}

Processing

import ddf.minim.;
import ddf.minim.signals.
;
import ddf.minim.analysis.;
import ddf.minim.effects.
;
//name it
Minim minim;
//Name the track
AudioPlayer AllahuAkbarAdan;
boolean flag = false;
//Processing:

import processing.serial.*;

Serial port;
String inputString;
float inputConvertedToFloat;

float minimumDelayUntilStasis = 1000;
float startOfMillisecondCount = millis();
float startms = millis();
boolean val = false;
boolean hasTimedOut = false;

boolean soundSwitch = true;
int previousInByte = 1; // 1 means NOT moving

void setup(){
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600); // [0] may have to change
frameRate(200);
///////////////

minim = new Minim (this);
AllahuAkbarAdan = minim.loadFile ("AllahuAkbarAdan.mp3");
AllahuAkbarAdan. loop();
AllahuAkbarAdan.mute();

/////////////////////
}

void draw() {
//lully.setVolume(0.00001);

if (port.available() > 0) {
int inByte = port.readChar(); // N.B. Oddly, a zero reading means there IS motion

print(inByte);
if (inByte == 0){
if (previousInByte == 1){
print("change to zero");
if (soundSwitch == true){
AllahuAkbarAdan.unmute();
}else{
AllahuAkbarAdan.mute();
}
soundSwitch = !soundSwitch;
}
}
previousInByte = inByte;
}
}

void drawTestingMINIM(){
float currentms = millis() - startms;

if (currentms > 2000){

val = !val;
if (val == true){
AllahuAkbarAdan.unmute();
}else{
AllahuAkbarAdan.mute();
}
startms = millis();
}
}

Please modify your post, select the code part and press the # button to get the code properly tagged, looks som much better then ...

Hi Robillaart, thanks wasn't quite sure how to do that. :slight_smile: F

/*
 * PIR sensor tester
 */
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;   // variable for reading the pin status
float startms = millis();
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
  digitalWrite(ledPin, 0);
}

void loopTEST(){
float currentms = millis() - startms;

if (currentms > 500){
  
  val = !val;
  digitalWrite(ledPin, val);
  startms = millis();
}
}
 
void loop(){
  delay(100);
  val = digitalRead(inputPin);  // read input value
  digitalWrite(ledPin, val);  // turn LED ON or OFF
  
  // Serial.print(val) BUT SEE BELOW. DON'T UNCOMMENT!!!!
    if (val == 1){
    
  
   Serial.print(1, BYTE); // There is a potential reason for this verbose version:--
   
   
    }else{
     Serial.print(0, BYTE);
    }
}


Processing

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
//name it
Minim minim;
//Name the track
AudioPlayer AllahuAkbarAdan;
boolean flag = false;
//Processing:

import processing.serial.*;

Serial port;
String inputString;
float inputConvertedToFloat;

float minimumDelayUntilStasis = 1000;
float startOfMillisecondCount = millis();
float startms = millis();
boolean val = false;
boolean hasTimedOut = false;

boolean soundSwitch = true;
int previousInByte = 1; // 1 means NOT moving

void setup(){
 println(Serial.list());
 port = new Serial(this, Serial.list()[0], 9600); //
may have to change
frameRate(200);
 ///////////////
 
  minim = new Minim (this);
AllahuAkbarAdan = minim.loadFile ("AllahuAkbarAdan.mp3");
 AllahuAkbarAdan. loop();
AllahuAkbarAdan.mute();
  
  /////////////////////
}


void draw() {
 //lully.setVolume(0.00001);
  
 if (port.available() > 0) {
 int inByte = port.readChar(); // N.B. Oddly, a zero reading means there IS motion
 
 print(inByte);
 if (inByte == 0){
 if (previousInByte == 1){
   print("change to zero");
   if (soundSwitch == true){
   AllahuAkbarAdan.unmute();
   }else{
   AllahuAkbarAdan.mute();
   }
   soundSwitch = !soundSwitch;
 }
 }
 previousInByte = inByte;
 }
}

void drawTestingMINIM(){
float currentms = millis() - startms;

if (currentms > 2000){
  
  val = !val;
  if (val == true){
AllahuAkbarAdan.unmute();
  }else{
AllahuAkbarAdan.mute();
  }
  startms = millis();
}
}

You could also change the existing post, but ok.

back to the code, void loop() can be shorter, but there is nothing wrong with the code as far as I see...

void loop(){
  delay(100);
  val = digitalRead(inputPin);  // read input value
  digitalWrite(ledPin, val);      // turn LED ON or OFF
 
  // Serial.print(val) BUT SEE BELOW. DON'T UNCOMMENT!!!!
   Serial.print(val, BYTE); 
}

furthermore use unsigned long for math with millis()
float currentms = millis() - startms; ==> unsigned long currentms = millis() - startms; // idem startms of course
floats have far less precision, 6-7 digits where long has 9-10.

Think the same is true for the processing script...

Rob

Hi,
ok will have a look at that, but do you think that might be the issue if the code is ok?

Thanks,

F

hi All,
just to say I solved this issue. Spent hors changing the delay in both Arduino and Processing and also the Baud rate. I found the right combination that works. I can post it here if anyone would like to se the solution.

best,

Frank

WOuld be informative for people with the same problem !