I wanted to power a dc-motor with 12V from the arduino uno , using the special power supply and plugging it in the socket. I used a transistor (mos) connecting the drain to the motor, the source to the Vin pin of the arduino uno and the gate to a digital pin.
Everything works fine until i "open" the gate. The motor works fine, after it has stopped the rest of the programm doesnt work correctly (i use an ldc screen on the same project and strange characters begin to appear).
At first i thought that the motor drains too much current, but the same happens with a simple led. any help would be appreciated
#include <CapacitiveSensor.h>
#include <LiquidCrystal.h>
//#include <Servo.h>
#define samples 20 //posa deigmata pairnoume
CapacitiveSensor capSensor = CapacitiveSensor(3,2);
//Servo myservo;
LiquidCrystal lcd(13,12,11,10,9,8);
int cap_threshold = 800; //gia ton cap sensor
int keylength = 0; //to mhkos tou kwdikou
const int numberPin = A1; //to pin pairnw data apo to thlexeir.
const int gate = 6; //h gate tou transistor
char *key = "19962016"; //o kwdikos
char password[17]; //edw apo8hkeuoume auta pou dinei o xrhsths
int password_pointer = 0; //deixnei poio pshfio 8a apo8hkeusoume sto password
int analogdata[samples]; //edw apo8hkeuoume ta data apo thn analog input
int analog_pointer = 0; //deixnei posa data exoume balei sto analogdata
int print_ready = 0; //print_ready: eimaste etoimoi na typwsoume ari8mo;
void setup() {
Serial.begin(9600);
//myservo.attach(6);
//myservo.write(100);
//delay(150);
pinMode(gate, OUTPUT);
digitalWrite(gate, LOW);
lcd.begin(16, 2);
lcd.print("Password:"); //sto setCursor mpainei prwta h sthlh
lcd.setCursor(0, 1); //kai meta h seira
for(int i=0; i<17; i++){ //arxikopoioume to password
password[i] = '\0';
}
int i = 0; //briskoume to mhkos tou kwdikou
while(number_or_not(i)){
keylength = keylength + 1;
i = i + 1;
}
}
void loop() {
long sensorValue = capSensor.capacitiveSensor(30); //pairnoume data apo
int numberValue = analogRead(numberPin); //tis eisodous
print_ready = analog_update(numberValue);
if(print_ready == 1 && password_pointer < 16){
int number_teliko = max_analog(); //h telikh analogikh eisodos
lcd_print(number_teliko); //typwnoume sthn o8onh
}
if(sensorValue > cap_threshold){ //8eloume na eleg3oume ton kwdiko
int flag = check_password();
if(flag)
correct_password();
else
wrong_password();
}
}
//--------------------------------
//elegxei ean ta stoixeia tou key einai ari8moi
int number_or_not(int i){
if(key[i] == '0' || key[i] == '1' || key[i] == '2' || key[i] == '3' || key[i] == '4' || key[i] == '5' || key[i] == '6' || key[i] == '7' || key[i] == '8' || key[i] == '9')
return 1;
else
return 0;
}
//--------------------------------
//elgxei ean prepei na enhmerwsoume ton analogdata
//kai ean exoume mazepsi arketa deigmata gia ektypwsoume ari8mo
int analog_update(int numberValue){
if(analog_pointer < samples && numberValue >= 480){
analogdata[analog_pointer] = numberValue;
analog_pointer = analog_pointer + 1;
}
return (analog_pointer == samples);
}
//--------------------------------
//briskei to megisto apo ta analogdata
int max_analog(){
int megisto = analogdata[0];
int i = 0;
for(i=1; i<samples; i++){
if(analogdata[i] > megisto)
megisto = analogdata[i];
}
for(i=0; i<samples; i++){
Serial.print(analogdata[i]);
Serial.print("\n");
}
Serial.print("\n\n");
Serial.print(megisto);
Serial.print("\n\n\n");
return megisto;
}
//--------------------------------
//typwnoume sthn lcd kai epanaferoume tis alles metablhtes
void lcd_print(int number_teliko){
char print_teliko = getnumber(number_teliko);
lcd.print(print_teliko);
print_ready = 0;
analog_pointer = 0;
password[password_pointer] = print_teliko;
password_pointer = password_pointer + 1;
delay(300);
}
//--------------------------------
//elegxoume ean exoume balei ton swsto kwdiko
int check_password(){
int flag = 1;
int i = 0;
for(i = 0; i < keylength; i++){
if(password[i] != key[i]){
flag = 0;
break;
}
}//edw to flag shmainei oti exoun ta prwta keylength stoixeia isa
if(flag == 1 && key[keylength] != password[keylength])
flag = 0;//edw deixnei pragmatika ean einai to idio
return flag;
}
//--------------------------------
void wrong_password(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Wrong Password.");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password:");
lcd.setCursor(0,1);
int i = 0;
for(i=0;i<17;i++){
password[i] = '\0';
}
password_pointer = 0;
analog_pointer = 0;
}
//------------------------------
void correct_password(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Correct Password");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password:");
lcd.setCursor(0,1);
int i = 0;
for(i=0;i<17;i++){
password[i] = '\0';
}
password_pointer = 0;
analog_pointer = 0;
//moveservo();
digitalWrite(gate, HIGH); //3ekinaei kai stamataei ton kinhthra
delay(3000);
digitalWrite(gate, LOW);
}
//--------------------------------
/*void moveservo(){
myservo.write(135);
delay(8000);
myservo.write(45);
delay(1500);
}*/
//--------------------------------
//mas epistrefei ti prepei na typwsoume
//analoga me to number_teliko
char getnumber(int a){
if(a <=530)
return '9';
else if(a >=531 && a <= 710)
return '1';
else if(a >= 711 && a <= 775)
return '2';
else if(a >= 776 && a <= 850)
return '3';
else if(a >= 851 && a <=933)
return '4';
else if(a >= 934 && a <= 979)
return '5';
else if(a >= 980 && a <= 996)
return '6';
else if(a >= 997 && a <= 1003)
return '7';
else if(a >= 1004 && a <= 1013)
return '8';
else if(a >= 1014 && a <= 1023)
return '0';
}
the spooky stuff happens in the correct_password function. As for the diode, since it happens with an led i dont think it will help but i'll try. behold my drawing skills:
It is customary to show, on the schematic, part numbers and power source(s). If you are using a N channel MOSFET or NPN transistor it is on the wrong side of the motor. A flyback diode is REQUIRED. Where is motor power coming from? It is best if the motor and Arduino have separate power supplies.