Saturday 5 November 2016

Autonomous Car - Hacking an RC and controlling it using Arduino

Teaching my kids 7 & 6 years on how to build robots :)

Autonomous Remote Control (RC) Car

const int turnOnMotorPin = 11;
const int controlMotorPin_1 = 12;
const int controlMotorPin_2 = 10;
const int turnMotor_1 = 9;
const int turnMotor_2 = 8;
const int echoSensor = 7;
const int triggerSensor = 6;

int motion = 1; //forward
int direction = 0; // straight


void setup() {
  pinMode(controlMotorPin_1, OUTPUT);
  pinMode(controlMotorPin_2, OUTPUT);
  pinMode(turnOnMotorPin, OUTPUT);
  pinMode(turnMotor_1, OUTPUT);
  pinMode(turnMotor_2, OUTPUT);
  pinMode(echoSensor,INPUT);
  pinMode(triggerSensor,OUTPUT);
  digitalWrite(turnOnMotorPin, HIGH);
  
  delay(5000); // Some initial delay to place the car in the right location

}

void loop() {
  

  long duration, inches;
  digitalWrite(triggerSensor, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerSensor, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerSensor, LOW);
  pinMode(echoSensor, INPUT);
  duration = pulseIn(echoSensor, HIGH);
  inches = microsecondsToInches(duration);

  int oldMotion = motion;

  if(motion == 1 && inches > 15){
    forward();
    motion = 1;
  }else
  if(motion == 1 && inches <= 15){
    reverse();
    motion = 0;
  }else if(motion == 0 && inches <= 30) {
    reverse();
    motion = 0;
  }else if(motion == 0 && inches > 30){
    forward();
    motion = 1;
  }else {
    forward();
    motion = 1;
  }

  if(oldMotion != motion){
    direction = random(0, 2);
  }

  if(direction == 0){
    goStragith();
  }else if(direction == 1){
    changeDirectionLeft();
  }else {
    changeDirectionRight();
  }
  

  
  delay(100);
}

void changeDirectionLeft(){
  
      digitalWrite(turnMotor_1, HIGH);
      digitalWrite(turnMotor_2, LOW);
}

void changeDirectionRight(){
  
      digitalWrite(turnMotor_1, LOW);
      digitalWrite(turnMotor_2, HIGH);
}
void goStragith(){
  
      digitalWrite(turnMotor_1, LOW);
      digitalWrite(turnMotor_2, LOW);
}

void stop(){
    digitalWrite(controlMotorPin_1, LOW);
    digitalWrite(controlMotorPin_2, LOW);
}

void forward(){
    digitalWrite(controlMotorPin_1, HIGH);
    digitalWrite(controlMotorPin_2, LOW);
}

void reverse(){
    digitalWrite(controlMotorPin_1, LOW);
    digitalWrite(controlMotorPin_2, HIGH);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Random Servo Motion Code

#include <Servo.h>

Servo myServo;

void setup() {
  // put your setup code here, to run once:
  myServo.attach(9);
}

void loop() {
  // put your main code here, to run repeatedly:
  int number = random(0, 179);
  myServo.write(number);
  delay(1000);
}

Temperature Sensor

The following is the code for using the Temperature Sensor:


const int tempSensorOutputPin = A0;

const int firstLEDPwrPin = 4;
const int secondLEDPwrPin = 3;
const int thirdLEDPwrPin = 2;

int tempSensorValue = 0;

void setup() {
  Serial.begin(9600);
  for(int pinLoop = 2; pinLoop <=4; pinLoop++){
    pinMode(pinLoop, OUTPUT);
    digitalWrite(pinLoop,LOW);
  }
}

void loop() {
  run();  
}

void run(){
  
  tempSensorValue = analogRead(tempSensorOutputPin);
  float voltage = (tempSensorValue / 1024.0) * 5.0;
  float temp = (voltage - 0.5) * 100;
  Serial.print("Temp = ");
  Serial.println(temp);
  delay(1000);
  
}

Proximity Sensor Arduino - Sample Code

The project video: https://www.youtube.com/watch?v=yBHyAFmtH-E&feature=youtu.be

const int greenLightPin = 6;
const int yellowLightPin = 5;
const int redLightPin = 4;

const int ussTrigPin = 2;
const int ussEchoPin = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode(greenLightPin,OUTPUT);
  pinMode(yellowLightPin,OUTPUT);
  pinMode(redLightPin,OUTPUT);

  pinMode(ussTrigPin,OUTPUT);
  pinMode(ussEchoPin,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  long duration, inches;
  digitalWrite(ussTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ussTrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ussTrigPin, LOW);
  pinMode(ussEchoPin, INPUT);
  duration = pulseIn(ussEchoPin, HIGH);
  inches = microsecondsToInches(duration);
  if(inches > 10 && inches < 15){
    digitalWrite(greenLightPin, HIGH);
    delay(100);
    digitalWrite(greenLightPin, LOW);
    delay(100);
  }
  if(inches < 10 && inches > 5){
    digitalWrite(yellowLightPin, HIGH);
    delay(100);
    digitalWrite(yellowLightPin, LOW);
    delay(100);
  }
  if(inches < 5){
    digitalWrite(redLightPin, HIGH);
    delay(100);
    digitalWrite(redLightPin, LOW);
    delay(100);
  }
  
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Azure OpenAI Architecture Patterns & Deployment Patterns

Sharing some useful links that will help customers architect Azure OpenAI solution using the best practices: (1) Azure OpenAI Landing Zone r...