Arduino uno code problems

problem is simple when the cable is pluged in the code uplaods and works perfectly when unpluged the arduino uno just stops in the code that it was lastly done. Aka meaning i got a sum program so for example if i but a white paper under the QRE sensor it does start to reverse and when i unplug after its uploaded and i do the paper trick then the code stops in there and goes in reverse infite, even when pluged i got more code that does spins and ultra sonic search etc. All works when pluged in the PC and uploaded and when i unplug the code just stops where it was and dosent do anything else.
WHat may be the case here cause i have tried to google search and i gotten basicly asnwers that dosent help me.

SO i turn here with this problem. Any ideas and sorry if the text seems kinda hard to understand, explaining these things isint easy for me. :slight_smile:

THe code itself is attached aswell if the problems might be in the code which i doubt it.

Ultrasonic.ino (3.76 KB)

And sorry if there is a fix for this in the forums but i got no idea what to even search for. Soif anyone can give me a link that be great and then this thread can be deleted.

Hi, firstly you can post the sketch so everyone can read it using the [code ][ / code] brackets, the # icon.
I f your program works fine while powered by the PC, then you have a problem with your supply that you use without the PC.
What sort of supply do you have powering the arduino and the motors.
Can you please post a circuit diagram to show how everything is connected. A CAD diagram in jpeg, png, or pdf would be good, however a picture or scan of a hand drawn circuit would be fine.

Also before you post your sketch, goto TOOLS and select AUTO-FORMAT to improve the layout of your program lines.

Hope to help, Tom....... :slight_smile:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int pwm_a = 3; 
int valgus1 = 4;
int valgus2 = 5;
int V52 = 6;
int GND2 = 7;
int pin = 8; 
int GND = 9;
int V5 = 10;
int pwm_b = 11;  
int dir_a = 12;  
int dir_b = 13;  
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned long time;
unsigned long sizeofpulse;
float distance;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600);
  pinMode(V5, OUTPUT);
  pinMode(GND, OUTPUT);
  pinMode(V52, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(pin, OUTPUT);
  pinMode(pwm_a, OUTPUT);  
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  digitalWrite(GND, LOW);
  digitalWrite(GND2, LOW);
  digitalWrite(V5, HIGH);    
  digitalWrite(V52, HIGH);
  digitalWrite(pin, HIGH);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
  pinMode(pin, OUTPUT); //return digital pin to OUTPUT mode after reading
  digitalWrite(pin, LOW);
  delayMicroseconds(25);
  digitalWrite(pin, HIGH); //Trig pin pulsed LOW for 25usec
  time = micros(); //record timer
  pinMode(pin, INPUT); //change pin to INPUT to read the echo pulse 
  sizeofpulse = pulseIn(pin, LOW, 18000); //should be approx 150usec, timeout at 18msec
  time = micros() - time - sizeofpulse; // amount of time elapsed since we sent the trigger pulse and detect the echo pulse, then subtract the size of the echo pulse
  distance = (time*340.29/2/10000)-3; // convert to distance in centimeters
  int Valgus_Value = readQD();
  Serial.println(Valgus_Value);
  int Valgus_Value2 = readQD2();
  Serial.println(Valgus_Value2);  
  Serial.print(" Kaugus: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(10);
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (distance >= 25) //Anduri threshold
  {
    digitalWrite(dir_a, LOW);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 100);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

  else if (distance <= 25) //Anduri threshold
  {
    digitalWrite(dir_a, LOW);  
    digitalWrite(dir_b, HIGH);  
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(10);
  }

  if (Valgus_Value <= 255) //Anduri threshold
  {
    digitalWrite(dir_a, HIGH);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

  if (Valgus_Value2 <= 255) //Anduri threshold
  {
    digitalWrite(dir_a, HIGH);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

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

int readQD(){
  pinMode( Valgus1, OUTPUT );
  digitalWrite( Valgus1, HIGH );  
  delayMicroseconds(10);
  pinMode( Valgus1, INPUT );

  long time2 = micros();

  while (digitalRead(Valgus1) == HIGH && micros() - time2 < 3000); 
  int diff = micros() - time2;
  return diff;
}

int readQD2(){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( Valgus2, OUTPUT );
  digitalWrite( Valgus2, HIGH );  
  delayMicroseconds(10);
  pinMode( Valgus2, INPUT );

  long time3 = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(Valgus2) == HIGH && micros() - time3 < 3000); 
  int diff2 = micros() - time3;
  return diff2;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Ouh yeha forgot to tell im using motor shield and i give 5 Volts from batteries to the motor shield. aka the two pins next to A and B channel.
So but the power got me to relaise if the below arduino uno board even gets pwoer when i power the motor shield.
And this makes sense cause when i plug the arduino in it works when unpluged the board stops working.

Edit:
Holy cow the code has errors. " Feeling ashemed of myself "
Im using outputs for Sensors. almost fried the sensors while i was giving the arduino uno board 9V. :frowning: Glad that i ralaised it before. xD kk. code needs to be more fixed and i know the code is half copy paste from other sources. SO would need to link them when i link it here.
im using digital colour sensors QRE.
ANd by the power it seems that Motor Shield and arduino uno need diffrent power supplies or if i give motor shield power the uno gets it. That i dont know and i dont know if or how INPUT and OUTPUT work properly cause the refrence page says something but without examples its hard to understand it. And the only code part that is done by myself is the IF statements.

And from the connections its simple. I got Motor shield ontop of the Arduino UNO + and - going to the Motor shield VIn and gnd plugs, Then from the code itself you can see where the sensors are located and yeah the code aint pretty. xD But will write here what sensor is where.
QRE ( Light Sensor ) 2 of them :
Power supply. is at 6 & 10. using OUTPUT HIGH
GND is 7 and one is at the Boards self GND
Read sensor: 4 and 5 pins.
Ultrasonic:
Power: 0.o kk missing. Need to chekc the machine itself for this.
GND: 0.o Kk missing. Need to check the machine itself for this.
Read: Pin 8

Motors are connected to Channel A & B on the motor shield
using PWM to run them.

What is absolutely essential is that the motors - from the motor shield - are not powered from the Arduino itself.

So this operates in reverse. The motor power does not and must not go back to power the Arduino.

As to the sensors, these must be powered from the same 5V as the Arduino, if they were powered from the unregulated or "raw" 9V, it would be necessary to consider protective isolation of their outputs before connecting them to ports on the Arduino. Fortunately almost all sensors that you would be likely to use do function on 5V and use trivial amounts of power; no more than a few milliamps.

//////Valgus Andurid//////////
int valgus1 = 4;
int valgus2 = 5;
//Ultra sonic///////////////
int ultra = 8;
////MOOTORID////////////////
int pwm_a = 3; 
int pwm_b = 11;  
int dir_a = 12;  
int dir_b = 13;  
unsigned long time;
unsigned long sizeofpulse;
float distance;
////Siit edasi hakkame nende pinidega tegema asju/////////
void setup()
{
  Serial.begin(9600);
//////////////////////////////////////////////////
  pinMode(ultra, OUTPUT);
//////////////////////////////////////////////////
  pinMode(pwm_a, OUTPUT);  
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
//////////////////////////////////////////////////
  digitalWrite(ultra, HIGH);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
  pinMode(ultra, OUTPUT); //return digital pin to OUTPUT mode after reading
  digitalWrite(ultra, LOW);
  delayMicroseconds(25);
  digitalWrite(ultra, HIGH); //Trig pin pulsed LOW for 25usec
  time = micros(); //record timer
  pinMode(ultra, INPUT); //change pin to INPUT to read the echo pulse 
  sizeofpulse = pulseIn(ultra, LOW, 18000); //should be approx 150usec, timeout at 18msec
  time = micros() - time - sizeofpulse; // amount of time elapsed since we sent the trigger pulse and detect the echo pulse, then subtract the size of the echo pulse
  distance = (time*340.29/2/10000)-3; // convert to distance in centimeters
////////////////////////////////////////////////////////////////////////////////////
  int Valgus_Value = readQD();
  Serial.println(Valgus_Value);
  int Valgus_Value2 = readQD2();
  Serial.println(Valgus_Value2);  
  Serial.print(" Kaugus: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(10);
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (distance >= 25) //Anduri threshold
  {
    digitalWrite(dir_a, LOW);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 100);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

  else if (distance <= 25) //Anduri threshold
  {
    digitalWrite(dir_a, LOW);  
    digitalWrite(dir_b, HIGH);  
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(10);
  }

  if (Valgus_Value <= 255) //Anduri threshold
  {
    digitalWrite(dir_a, HIGH);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

  if (Valgus_Value2 <= 255) //Anduri threshold
  {
    digitalWrite(dir_a, HIGH);
    digitalWrite(dir_b, LOW);
    delay(10);
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 100);
    delay(10);
  }

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

int readQD(){
  pinMode( valgus1, OUTPUT );
  digitalWrite( valgus1, HIGH );  
  delayMicroseconds(10);
  pinMode( valgus1, INPUT );

  long time2 = micros();

  while (digitalRead(valgus1) == HIGH && micros() - time2 < 3000); 
  int diff = micros() - time2;
  return diff;
}
///////////////////////////////////////////////////////////////////////////////////////////////
int readQD2(){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( valgus2, OUTPUT );
  digitalWrite( valgus2, HIGH );  
  delayMicroseconds(10);
  pinMode( valgus2, INPUT );

  long time3 = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(valgus2) == HIGH && micros() - time3 < 3000); 
  int diff2 = micros() - time3;
  return diff2;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Confession time. I am a huge noob and a beginner.
First step what i did wrong was using I/O pins for sensor power and ground while on 4 AA batteries.
buted this new code up aswell here. Now im using arduinos own and i saw that i wasent using a jumper so the power goes from shield to uno and back. So used unos own 5V pin and grounds for sensors.
But thx guys for the help. Now got it to work annd works good.

May close the topic now.