Project 2





The project code comes in two parts for two different arduinos.

//mastercode
//Controls the LCD arduino for movement
#include <LiquidCrystal.h>
int buttonVal = 0;
int buttonNum;
boolean lastButton = false;
boolean currentButton = false;
int tMode = 0; // default dist
const int button = 720;
int vt = 0;
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);

//creates up arrow
byte up[8]=
{
  B00100,
  B01110,
  B10101,
  B00100,
  B00100,
  B00100,
  B00100,
  B00100,
};

//creates down arrow
byte dow[8]=
{
  B00100,
  B00100,
  B00100,
  B00100,
  B00100,
  B10101,
  B01110,
  B00100,
};

//creates right arrow
byte ri[8]=
{
  B01000,
  B00100,
  B00010,
  B00001,
  B00010,
  B00100,
  B01000,
  B00000,
};

//creates left arrow
byte lef[8]=
{
  B00010,
  B00100,
  B01000,
  B10000,
  B01000,
  B00100,
  B00010,
  B00000,
};



boolean debounce(int current)
{
  int brange = button - current;
  if ((-5 <=brange&& brange<= 5))
  {
    delay(10);
    current = analogRead(A0);
    brange = button - current;
    if ((-5 <=brange&& brange<= 5))
    {
    currentButton = true;
    }
  }
  else
  {
    currentButton = false;
  }
  if (currentButton == true && lastButton == false)
  {
    if(tMode <= 1)
    {
    tMode++;
    }
    else
    {
      tMode = 0;
    }
  }
  lastButton = currentButton;
  return tMode;
}

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.createChar(3,up); //assigns up arrow as "3"
  lcd.createChar(4,dow); //assigns down arrow as "4"
  lcd.createChar(5,ri); //assigns right arrow as "5"
  lcd.createChar(6,lef); //assigns left arrow as "6"
  lcd.setCursor(0,0);
  lcd.print("MODE:");
}

void loop()
{
  int k = 0;

  int buttonVal = analogRead(A0);
   int buttonp = analogRead(A0);
  debounce(buttonp);
  lcd.setCursor(0,1);
  if(tMode ==0)
  {
    lcd.setCursor(0,0);
  lcd.print("OBSTACLE:");
  vt = Serial.read();
    lcd.setCursor(10,0);
    lcd.print(vt);
    lcd.print("   ");
    //direc();
    Serial.write('p');
  }

  else if (tMode == 1)
  {
    lcd.setCursor(0,0);
  lcd.print("DISTANCE:");
  vt = Serial.read();
  lcd.setCursor(10,0);
  lcd.print(vt);
  lcd.print("   ");
  //dist();
  Serial.write('d');
  }

  else if (tMode == 2)
  {
  lcd.setCursor(0,0);
  lcd.print(" VOLTAGE:");
  vt = Serial.read();
  lcd.setCursor(10,0);
  lcd.print(vt);
  lcd.print("   ");
  //power();
  Serial.write('v');
  }


//forward
  if (buttonVal > 125 && buttonVal < 135)
  {
    Serial.write('1'); //sends char "1" the forward signal to serial
    lcd.setCursor(0,1);
    //lcd.print("forward");
    lcd.setCursor(0,1);
    lcd.write(3); 
  }

  //backward
  else if (buttonVal > 300 && buttonVal < 310)
  {
    Serial.write('2'); //sends char "2" the backward signal to serial
    lcd.setCursor(0,1);
    //lcd.print("backwards");
    lcd.setCursor(0,1);
    lcd.write(4);
  }

  //left
  else if (buttonVal > 473 && buttonVal < 483)
  {
    Serial.write('3'); //sends char "1" the turn left signal to serial
    lcd.setCursor(0,1);
    //lcd.print("left");
    lcd.setCursor(0,1);
    lcd.write(6);     
  }

  //right
  else if (buttonVal < 5 && buttonVal > -0.1)
  {
    Serial.write('4'); //sends char "1" the turn right signal to serial
    lcd.setCursor(0,1);
    //lcd.print("right");
    lcd.setCursor(0,1);
    lcd.write(5);     
  }

  else
  {
    Serial.write('0'); //default signal to stop motors
    lcd.setCursor(0,1);
    lcd.print("      ");
  }
  if(Serial.available() > 0)
  {
  vt = Serial.read();
  }


  delay(90);
}







and:








//slave code
//controls car and sensors

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "direction.h"
#include "output.h"
LiquidCrystal_I2C lcd (0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

char buttonVal;
int POT_X = 0;
int POT_Y = 0;

int range = 220;
int dataOut = 0;
int dist = 0;
int obst = 0;
int volt = 0;
const int D_pin = 3;
const int V_pin = 0;


void setup()
{
  pinMode(M1_Left, OUTPUT);
  pinMode(M1_Right , OUTPUT);
  pinMode(M2_Left, OUTPUT);
  pinMode(M2_Right , OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
}

void loop()
{
  dist = analogRead(D_pin);
  volt = analogRead(V_pin);
  //vol = (v_in * 5) * 0.000977;
  if (dist > range)
  {
    obst ++;
    while(dist > range)
    {
      dist = analogRead(D_pin);
      forward();
      lcd.setCursor(11,1);
      lcd.print("obj");
      lcd.setCursor(0,0);
      lcd.print(dist);
   
    }
 
  }
  else
  {
    lcd.setCursor(11,1);
    lcd.print("   ");
    if (Serial.available() > 0)
    {
      buttonVal = Serial.read();

      //forward
      if (buttonVal == '1')
      {
        forward();
        //Serial.write('1');

      }

      //backward
      else if (buttonVal == '2')
      {
        reverse();
        //Serial.write('2');
      }

      //left
      else if (buttonVal == '3')
      {
        left();
        //Serial.write('3');
      }

      //right
      else if (buttonVal == '4')
      {
        right();
        //Serial.write('4');
      }

      else if (buttonVal == '0')
      {
        halt();
        //Serial.write('0');
      }
else
      {
        halt();
        //Serial.write('0');
      }
      lcd.setCursor(0, 1);
      lcd.print(buttonVal);
      lcd.print("          ");

      if(buttonVal == 'p')
      {
        dataOut = 1;
      }
      if(buttonVal == 'd')
      {
        dataOut = 2;
      }
      if(buttonVal == 'v')
      {
        dataOut = 3;
      }
    }
 

  }
  volt_o = volt;
  obst_o = obst;
  dist_o = dist;
  dataOutput(dataOut);
  lcd.setCursor(0,1);
  lcd.print("VOLT:");
  lcd.print(volt);
  lcd.setCursor(14,1);
  lcd.print(obst);
  delay(10);
}


DIRECTIONS HEADER


int M1_Left = 11;
int M1_Right = 10;
int M2_Left = 9;
int M2_Right = 8;





void halt() {
  digitalWrite(M1_Left, LOW);
  digitalWrite(M1_Right , LOW);
  digitalWrite(M2_Left, LOW);
  digitalWrite(M2_Right , LOW);
}

void forward()
{
  //spd = map(POT_Y, 562, 1023, 0, 255);
  digitalWrite(M1_Left, HIGH);
  analogWrite(M1_Right, 0);
   digitalWrite(M2_Left, HIGH);
  analogWrite(M2_Right, 0);

}

void reverse()
{
  //spd = map(POT_Y, 462, 0, 0, 255);
  digitalWrite(M1_Left, LOW);
  analogWrite(M1_Right, 255);
  digitalWrite(M2_Left, LOW);
  analogWrite(M2_Right, 255);

}

void left()
{
  //spd = map(POT_Y, 562, 1023, 0, 255);
  digitalWrite(M1_Left, HIGH);
  analogWrite(M1_Right, 0);
    digitalWrite(M2_Left, LOW);
  analogWrite(M2_Right, 255);

}

void right()
{
  //spd = map(POT_Y, 462, 0, 0, 255);
   digitalWrite(M1_Left, LOW);
  analogWrite(M1_Right, 255);
 digitalWrite(M2_Left, HIGH);
  analogWrite(M2_Right, 0);

}



DATA OUTPUT HEADER


char obst_o = 0;
char dist_o = 0;
char volt_o = 0;

void dataOutput(int dataMode)
{
  switch (dataMode)
  {
    case 1:
    Serial.write(obst_o);
    break;
    case 2:
    Serial.write(dist_o);
    break;
    case 3:
    Serial.write(volt_o);
    break;
    default:
    Serial.write(obst_o);
  }
}







A big challenge for this project was finding out how to get the bluetooth transceivers to talk to each other, we got them to talk to each other thanks to this website: http://www.instructables.com/id/AT-command-mode-of-HC-05-Bluetooth-module/





Comments

Popular posts from this blog

DAY 17

day 19

Day 21