# data[].attributes.body
/* CarroBluetooth.ino
* Carro controlado por Bluetooth
*/
const byte MOTORDA = 3; // Motor Direito-Avançar
const byte MOTORDR = 2; // Motor Direito-Recuar
const byte MOTOREA = 4; // Motor Esquerdo-Avançar
const byte MOTORER = 5; // Motor Esquerdo-Recuar
const byte SENSOR_F = 11;
const byte SENSOR_D = 10;
const byte SENSOR_E = 12;

const byte EN_D = 6;
const byte EN_E = 9;

const byte Comutador = 7;

void setup()
{
//Inicializa porta série e espera que esta se abra:
Serial.begin(9600);
/* while (!Serial) {
; // Necessário só para o Leonardo
}*/
pinMode(SENSOR_F, INPUT);
pinMode(SENSOR_D, INPUT);
pinMode(SENSOR_E, INPUT);

pinMode (MOTORDA, OUTPUT);
pinMode (MOTORDR, OUTPUT);
pinMode (MOTOREA, OUTPUT);
pinMode (MOTORER, OUTPUT);

pinMode (Comutador, INPUT);

pinMode (EN_D, OUTPUT);
pinMode (EN_E, OUTPUT);

Serial.setTimeout(5);

}

void loop() {

// Espera que haja uma ordem de comando
int Valor_Comutador = digitalRead (Comutador);

if (Valor_Comutador == 0)
{
int Valor_Sensor_F = digitalRead (SENSOR_F);
int Valor_Sensor_D = digitalRead (SENSOR_D);
int Valor_Sensor_E = digitalRead (SENSOR_E);

if ((Valor_Sensor_F == HIGH) && (Valor_Sensor_D == HIGH) && (Valor_Sensor_E == HIGH)) //AVANÇAR
{
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 255);

}

if ((Valor_Sensor_F == LOW) && (Valor_Sensor_D == HIGH) && (Valor_Sensor_E == HIGH)) //RECUAR E VIRAR
{
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, HIGH);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, HIGH);
analogWrite(EN_E, 255);
delay (500);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 100);
// delay(400);

}

if ((Valor_Sensor_F == HIGH) && (Valor_Sensor_D == LOW) && (Valor_Sensor_E == HIGH)) //ESQUERDA
{
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 100);
//delay(200);
}
if ((Valor_Sensor_F == HIGH) && (Valor_Sensor_D == HIGH) && (Valor_Sensor_E == LOW)) //DIREITA
{

digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 100);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_D, 255);
//delay(200);
}

}

if (Serial.available() )

{

if (Valor_Comutador == 1)
{
// Lê comando do controlador:
int A, R, E, D, P, X, Y;
char  comando = Serial.read();

switch (comando)
{
case 'A': // Avançar

A = Serial.parseInt();
// Serial.println (A);

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, A);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, A);

break;

case 'R': // Recuar

R = Serial.parseInt();
// Serial.println (A);

delay(10);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, HIGH);
analogWrite(EN_D, R);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, HIGH);
analogWrite(EN_E, R);

break;

case 'P': // Parar

P = Serial.parseInt();
// Serial.println (A);

delay(10);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, P);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, P);

break;

case 'E': // Esquerda

E = Serial.parseInt();
// Serial.println (A);

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, E);

break;

case 'D': // Direita

D = Serial.parseInt();
// Serial.println (A);

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, D);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 255);

break;

case '0': // Parado

delay(10);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 0);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 0);

break;

case '1': // Tras Lento

delay(10);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, HIGH);
analogWrite(EN_D, 130);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, HIGH);
analogWrite(EN_E, 130);

break;
case '2': // Tras Rápido

delay(10);
digitalWrite(MOTORDA, LOW);
digitalWrite(MOTORDR, HIGH);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, LOW);
digitalWrite(MOTORER, HIGH);
analogWrite(EN_E, 255);

break;
case '3': // Direita lento

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 190);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 255);

break;
case '4': // Direita rápido

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 130);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 255);

break;
case '5': // Esquerda lento

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 190);

break;
case '6': // Esquerda rápido

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 130);

break;

case '7': // Avançar lento

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 130);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 130);

break;
case '8': // Avançar rápido

delay(10);
digitalWrite(MOTORDA, HIGH);
digitalWrite(MOTORDR, LOW);
analogWrite(EN_D, 255);
digitalWrite(MOTOREA, HIGH);
digitalWrite(MOTORER, LOW);
analogWrite(EN_E, 255);

break;

}

}
}
}

# data[].attributes.body
/*
* Created by SharpDevelop.
* User: BTT
* Date: 30/10/2018
* Time: 14:32
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace MarcoSerialConx
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
foreach(string porta in SerialPort.GetPortNames()){
comboBox1.Items.Add(porta);
}

conx.DataReceived += conx_DataReceived;
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private void conx_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line = conx.ReadLine();
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}
private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox1.Text = line;
//progressBar1.Value = valor;
//progressBar1.Value= Convert.ToInt32(line);
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (conx.IsOpen) conx.Close();
}

private void conx_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
{
conx.Close();
}

void Button1Click(object sender, EventArgs e)
{
conx.PortName = comboBox1.SelectedItem.ToString();
conx.BaudRate = 9600;
conx.DtrEnable = true;
button1.Enabled=false;
conx.Open();
}
}
}

# data[].attributes.body
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogTempPin = A1;
const int led = 13; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int sensorTemp = 0;
int outputLux = 0;
int outputTemp = 0;

void setup() {
// initialize serial communications at 9600 bps:
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
sensorTemp = analogRead(analogTempPin);
// map it to the range of the analog out:
outputLux = map(sensorValue, 0, 1023, 0, 100);
outputTemp = map(sensorTemp, 0, 1023, 0, 100);
// change the analog out value:
//analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:
Serial.print("L"); Serial.println(outputLux);
Serial.print("T");Serial.println(outputTemp);
if(sensorValue<100){
digitalWrite(led,HIGH);
}
else{
digitalWrite(led,LOW);
}

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(200);
}