Go Down

Topic: Arduino Obstacle-Light avoiding Rover - Confusion with code setup (Read 498 times) previous topic - next topic

Dosh20

Hello, I am new to this forum and would like help with the code made for an Arduino Uno robot I have built. Unfortunately, I've let someone else borrow my three wheel rover after I was busy working out the errors in my code and testing it on my arduino rover. I forgot to take a picture of it, but that does not matter for now.

The components I am using are: "two reduced DC motors, a directional turn wheel, two pressure-squeeze sensors to act as bumpers for the bot to turn back out and turn the other direction, two push buttons for connections and light sensor adjustment, a photo-resistor, and piezo speaker.

I have fixed the errors of the code and did some tests, but the rover did nothing so far and I have checked over the codes once more to see if anything else was missing or conflicted.

I would like someone to please look over my codes and see if I am doing anything wrong that I do not know of.

Thank you for your time.

Here is my code:
Code: [Select]
#include <Servo.h>
Servo moodtrigger;
int pos = 90; //servo position
int buttonOne = 3; //raises light threshold
int buttonTwo = 2; //lowers light threshold
int motorRight = 9;
int motorLeft = 11;
int rightfeelPin = 2; //Right Feeler
int lightPin = 0; //Photosensor
int lightThreshold = 500;
boolean lastButton = LOW;
boolean currentButton = LOW;
int lightLevel = analogRead(lightPin);

int speakerPin = 5; //for relaxed melody and scream
char names[] = {'c','d','e','f','g','a','b','C','D','E','F','G','A','B'};
int rangeOfNotes = 14;
int tones[] ={1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 850, 760, 716, 638, 568, 507};
int length = 27;
char notes[] = " ceabecbCecCfdafecacecagaaa";

char notesTwo[] = " B";
double beatsTwo[] = {.5, 2};
int lengthTwo = 2;

double beats[] = {.5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, 1, .5, .5, .5, .5, .5, .5, 1};
int tempo = 1000;

void setup()
{
  Serial.begin(9600);

  pinMode(motorLeft, OUTPUT);
  pinMode(motorRight, OUTPUT);
  moodtrigger.attach(6);
   pinMode (buttonOne, INPUT);
pinMode (buttonTwo, INPUT);

 
  pinMode(speakerPin, OUTPUT);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonOne);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonOne);
  }
  return current;
}
boolean debouncetwo(boolean last)
{
  boolean current = digitalRead(buttonTwo);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonTwo);
  }
  return current;
 
}

void loop()
{
  buttons();
scream();
  song();
  servoSubroutine();
moveAround();
  Serial.println(lightLevel);
}

void buttons()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    lightThreshold = lightThreshold += 10;
  }
    lastButton = currentButton;
currentButton = debouncetwo(lastButton);
if (lastButton == LOW && currentButton == HIGH)
  {
    lightThreshold = lightThreshold -= 10;
  }
    lastButton = currentButton;
}

void scream()
{
    int lightLevel = analogRead(lightPin); // Read the light level
   for (int i = 0; i < lengthTwo; i++){//for each note in the character array (string)
if (notesTwo[i] == ' '&& lightLevel>lightThreshold){//if the character is a space, do the following line.
delay(tempo/2); //rest (there is a pause in the music)
} else if (notesTwo[i] != ' '&& lightLevel>lightThreshold) { //otherwise (if the character is not a space), do the following line.
playNotetwo(notesTwo[i], beatsTwo[i] * tempo);/*play the note notes[i] for
the ammount of time beats[i] * tempo, by using the playNote function defined below*/
}
//delay(tempo/2);//if you want to include a pause between notes, but throws rhythm off.
}
  }

void playNotetwo(char note, int duration){//plays the note "note" for the time "duration"
//play the tone corresponding to the note name
for (int i = 0; i < rangeOfNotes; i++){
if (names[i] == note){
playTone(tones[i], duration);
}
}
}

void playTone(int tone, int duration){
for (long i = 0; i < duration *1000L; i += tone * 2){
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
}
}

  void song()
{
     int lightLevel = analogRead(lightPin); // Read the light level
   for (int i = 0; i < length; i++){//for each note in the character array (string)
if (notes[i] == ' '&&lightLevel<=lightThreshold){//if the character is a space, do the following line.
delay(tempo/2); //rest (there is a pause in the music)
} else  if (notes[i] != ' '&&lightLevel<=lightThreshold){ //otherwise (if the character is not a space), do the following line.
playNote(notes[i], beats[i] * tempo);/*play the note notes[i] for
the ammount of time beats[i] * tempo, by using the playNote function defined below*/
}
//delay(tempo/2);//if you want to include a pause between notes, but throws rhythm off.
}
  }

void playNote(char note, int duration){//plays the note "note" for the time "duration"
//play the tone corresponding to the note name
for (int i = 0; i < rangeOfNotes; i++){
if (names[i] == note){
playTone(tones[i], duration);
}
}
}

void servoSubroutine()
{
   int lightLevel = analogRead(lightPin); // Read the light level
  lightLevel = map(lightLevel, 0, 900, 0, 255);
  //adjust the value 0 to 900 to
  lightLevel = constrain(lightLevel, 0, 255);
  //make sure the value is between 0 and 255

  if (lightLevel > lightThreshold)
  {
    pos = pos += 45;
    delay (5);
    pos = pos -= 45;
    delay (5);
  }
  else
  {
    pos = pos += 45;
    delay (150);
    pos = pos -=45;
    delay (150);
  }

moodtrigger.write(pos);
}

void moveAround()
{
  int value = analogRead(rightfeelPin)/4;
int lightLevel = analogRead(lightPin); // Read the light level
  if (lightLevel <= lightThreshold, value>1)
  {
    digitalWrite(motorRight && motorLeft, HIGH);
    delay (1000);
    digitalWrite(motorLeft, LOW);
    delay (500);
  }
  else if (lightLevel <= lightThreshold, value>1)
  {
    digitalWrite(motorRight && motorLeft, HIGH);
    delay (1000);
    digitalWrite(motorRight && motorLeft, LOW);
    delay (500);
  }
    else
  {
    delay (150);
  }
}

Wyldhunt

The first thing I would do is add debug code to see where it breaks.

Have it print to your PC console and report what the variables are when you use them.
When you check the light level, have it report what the light level is to your screen.

That might give you some clues.

Go Up