e
Hello, ive recently got an arduino car that I previously was able to get voltage through on a breadboard, but when transferring it to the final car it doesnt seem to be powering to anything even when power is turned on. I am wondering if theres something wrong in the code or if its a hardware problem as I am currently trying to power a motor but cannot power it at all through the circuit now
Hardware per motor:
Dac0800
2N3906 PNP
TIP32 and TIP 31 PNP andNPN
LM393 comparator
LM741 Op Amp
NE555 timer
33GB-520-18.7 DC Geared Motor
Arduino Nano
This is the code im trying to test if it works
Arduino Code:
// This driver handles serial transmission of data.
// Each command or response is sent as a four byte package, formatted as follows:
// <startByte><commandByte><dataByte><checkByte>
// The start byte identifies the first byte of a new message. Ideally this value would be reserved for the start byte
// build of the message will be confirmed using a check sum.
// The command byte allows selection of which port to interact with.
// The data byte is the value set to or read from a port, this is the main information that needs to be transmitted.
// The check sum allows for confirmation that the correct four bytes have been received as a message. This is useful as it
// confirm that the end of the message has been reached and that no bytes were lost during the transmission.
//Declare variables for storing the port values.
byte output1 = 255;
byte output2 = 255;
byte input1 = 0;
byte input2 = 0;
//Declare variables for each byte of the message.
byte startByte = 0;
byte commandByte = 0;
byte dataByte = 0;
byte checkByte = 0;
//Declare variable for calculating the check sum which is used to confirm that the correct bytes were identified as the four message bytes.
byte checkSum = 0;
//Declare a constant for the start byte ensuring that the value is static.
const byte START = 255;
//Declare constants to enumerate the port values.
const byte INPUT1 = 0;
const byte INPUT2 = 1;
const byte OUTPUT1 = 2;
const byte OUTPUT2 = 3;
const byte DACPIN1[8] = {9,8,7,6,5,4,3,2};
const byte DACPIN2[8] = {A2,A3,A4,A5,A1,A0,11,10};
const byte SENSOR1 = A6;
const byte SENSOR2 = A7;
void outputToDAC1( byte data ) //loop through lookup table of Arduino pins connected to DAC and write correct bit to each
{
for( int i = 0; i<=7; i++ )
{
digitalWrite( DACPIN1[i] , ((data>>i)&1 ? HIGH : LOW));
}
}
void outputToDAC2( byte data ) //loop through lookup table of Arduino pins connected to DAC and write correct bit to each
{
for( int i = 0; i<=7; i++ )
{
digitalWrite( DACPIN2[i] , ((data>>i)&1 ? HIGH : LOW));
}
}
void initDACs() //loop through lookup table of Arduino pins connected to DAC and set to output mode
{
for( int i = 0; i<=7; i++ )
{
pinMode( DACPIN1[i] , OUTPUT);
pinMode( DACPIN2[i] , OUTPUT);
}
}
//Setup initialises pins as inputs or outputs and begins the serial.
void setup()
{
Serial.begin(9600);
initDACs();
}
//Main program manages setting/reading of ports via serial.
void loop()
{
if (Serial.available() >= 4) // Check that a full package of four bytes has arrived in the buffer.
{
startByte = Serial.read(); // Get the first available byte from the buffer, assuming that it is the start byte.
if(startByte == START) // Confirm that the first byte was the start byte, otherwise begin again checking the next byte.
{
//Read the remaining three bytes of the package into the respective variables.
commandByte = Serial.read();
dataByte = Serial.read();
checkByte = Serial.read();
checkSum = startByte + commandByte + dataByte; // Calculate the check sum, this is also calculated in visual studio and is sent as he final byte of the package.
if(checkByte == checkSum) //Confirm that the calculated and sent check sum match, if so it is safe to process the data.
{
//Check the command byte to determine which port is being called and respond accordingly.
switch(commandByte)
{
case INPUT1: //In the case of Input 1 the value is read from pin A5 and sent back in the same four byte package format.
{
input1 = digitalRead(SENSOR1);
Serial.write(START); //Send the start byte indicating the start of a package.
Serial.write(commandByte); //Echo the command byte to inform Visual Studio which port value is being sent.
Serial.write(input1); //Send the value read.
int checkSum1 = START + commandByte + input1; //Calculate the check sum.
Serial.write(checkSum1); //Send the check sum.
}
break;
case INPUT2: //Input 2 is the same as Input 1, but read from pin A4
{
input2 = digitalRead(SENSOR2);
Serial.write(START); //Send the start byte indicating the start of a package.
Serial.write(commandByte); //Echo the command byte to inform Visual Studio which port value is being sent.
Serial.write(input2); //Send the value read.
int checkSum2 = START + commandByte + input2; //Calculate the check sum.
Serial.write(checkSum2); //Send the check sum.
}
break;
case OUTPUT1: //For Output 1 the value of the data byte is written to pins in DACPIN1.
{
output1 = dataByte; //The value of the data byte is stored in variable output 1, this step is redundant as the value could be written directly to the port.
outputToDAC1(output1); //However keeping the data in a variable could prove useful if further processing was done on the arduino side.
}
break;
case OUTPUT2: //For Output 1 the value of the data byte is written to pins in DACPIN2.
{
output2 = dataByte;
outputToDAC2(output2);
}
break;
}
}
}
}
}
//Function to reverse the order of the bits.
byte bitFlip(byte value)
{
byte bFlip = 0;
byte j=7;
for (byte i=0; i<8; i++) {
bitWrite(bFlip, i, bitRead(value, j));
j--;
}
return bFlip;
}
Visual Studio Code (C#)
// Serial I/O Card - Sample GUI Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SerialGUISample
{
public partial class Form1 : Form
{
// Declare variables to store inputs and outputs.
bool runSerial = true;
bool byteRead = false;
int Input1 = 0;
int Input2 = 0;
//int voltage = 0;
byte[] Outputs = new byte[4];
byte[] Inputs = new byte[4];
const byte START = 255;
const byte ZERO = 0;
const byte VOLTAGE = 128*(8 / 18 + 255 / 256);
public Form1()
{
// Initialize required for form controls.
InitializeComponent();
// Establish connection with serial
if (runSerial == true)
{
if (!serial.IsOpen) // Check if the serial has been connected.
{
try
{
serial.Open(); //Try to connect to the serial.
}
catch
{
statusBox.Enabled = false;
statusBox.Text ="ERROR: Failed to connect."; //If the serial does not connect return an error.
}
}
}
}
// Send a four byte message to the Arduino via serial.
private void sendIO(byte PORT, byte DATA)
{
Outputs[0] = START; //Set the first byte to the start value that indicates the beginning of the message.
Outputs[1] = PORT; //Set the second byte to represent the port where, Input 1 = 0, Input 2 = 1, Output 1 = 2 & Output 2 = 3. This could be enumerated to make writing code simpler... (see Arduino driver)
Outputs[2] = DATA; //Set the third byte to the value to be assigned to the port. This is only necessary for outputs, however it is best to assign a consistent value such as 0 for input ports.
Outputs[3] = (byte)(START + PORT + DATA); //Calculate the checksum byte, the same calculation is performed on the Arduino side to confirm the message was received correctly.
if (serial.IsOpen)
{
serial.Write(Outputs, 0, 4); //Send all four bytes to the IO card.
}
}
private void Send1_Click(object sender, EventArgs e) //Press the button to send the value to Output 1, Arduino Port A.
{
double V_ref = 15;
double V_out = (double)OutputBox1.Value;
double Voltage = 128.0 * (V_out / V_ref + 255.0 / 256.0);
sendIO(2, (byte)Voltage);
statusBox.Text = ((byte)Voltage).ToString();
//float voltage = (float)OutputBox1.Value;
//byte bytes2 = (byte)((voltage + 15) * 255 / 30);
//sendIO(2, VOLTAGE); // The value 2 indicates Output1, value for output set in OutputBox1 128[Vout/Vef + 255/256]
//sendIO(2, VOLTAGE);
}
private void Send2_Click(object sender, EventArgs e) //Press the button to send the value to Output 2, Arduino Port C.
{
sendIO(3, (byte)OutputBox2.Value); // The value 3 indicates Output2, value for output set in OutputBox1.
}
private void Get1_Click(object sender, EventArgs e) //Press the button to request value from Input 1, Arduino Port F.
{
sendIO(0, ZERO); // The value 0 indicates Input 1, ZERO just maintains a fixed value for the discarded data in order to maintain a consistent package format.
}
private void Get2_Click(object sender, EventArgs e) //Press the button to request value from Input 1, Arduino Port K.
{
sendIO(1, ZERO); // The value 1 indicates Input 2, ZERO maintains a consistent value for the message output.
}
private void getIOtimer_Tick(object sender, EventArgs e) //It is best to continuously check for incoming data as handling the buffer or waiting for event is not practical in C#.
{
if (serial.IsOpen) //Check that a serial connection exists.
{
if (serial.BytesToRead >= 4) //Check that the buffer contains a full four byte package.
{
//statusBox.Text = "Incoming"; // A status box can be used for debugging code.
Inputs[0] = (byte)serial.ReadByte(); //Read the first byte of the package.
if (Inputs[0] == START) //Check that the first byte is in fact the start byte.
{
//statusBox.Text = "Start Accepted";
//Read the rest of the package.
Inputs[1] = (byte)serial.ReadByte();
Inputs[2] = (byte)serial.ReadByte();
Inputs[3] = (byte)serial.ReadByte();
//Calculate the checksum.
byte checkSum = (byte)(Inputs[0] + Inputs[1] + Inputs[2]);
//Check that the calculated check sum matches the checksum sent with the message.
if (Inputs[3] == checkSum)
{
//statusBox.Text = "CheckSum Accepted";
//Check which port the incoming data is associated with.
switch (Inputs[1])
{
case 0: //Save the data to a variable and place in the textbox.
//statusBox.Text = "Input1";
Input1 = Inputs[2];
InputBox1.Text = Input1.ToString();
break;
case 1: //Save the data to a variable and place in the textbox.
//statusBox.Text = "Input2";
Input2 = Inputs[2];
InputBox2.Text = Input2.ToString();
break;
}
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}