NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//Stepper motor
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };
IPAddress ip( 192, 168, 88, 3 );
IPAddress gateway( 192, 168, 88, 1 );
IPAddress dnsServer( 192, 168, 88, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change to your server
//IPAddress server( 192, 168, 1, 16 );
IPAddress server( 192, 168, 88, 2 );
EthernetServer server1(80);
EthernetClient client;
EthernetClient dclient;
String readString;



char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "senor_2.txt";

const int chipSelect = 4;



int row=0;
int Distance = 0;
int Speed=55;
int y;
char incomingByte;
const byte dir= 8;
const byte pul= 9;
boolean resetCheck= 0;

//Scanner parameters
const byte common = A0; // where the multiplexer in/out port is connected
int thres=0;

const byte addressS2 = 2; // high-order bit
const byte addressS1 = 3;
const byte addressS0 = 14; // low-order bit
const byte addressM0 = 5; // low-order bit
const byte addressM1 = 6;
const byte addressM2 = 7; // high-order bit

//Limit switchs
int startPin = 22; // pushbutton connected to digital pin 7 //10---8
int endPin=24;
//int light=26;
int startVal; // variable to store the read value
int endVal;
// Actuator parameters
const int Act101 = 40;
const int Act102 = 38;
const int Act201 = 36;
const int Act202 = 34;

int doorPin1 = 44;
int doorPin2 = 42;
int targetPin1 = 48;
int targetPin2 = 46;
int dOpen = 0;
int dClose=0;
int tOpen=0;
int tClose=0;

unsigned long previousMillis = 0; // will store last time was updated

// constants won't change :
const long ethernetRenewInterval = 10000;

void setup ()
{

Serial.begin (115200);
digitalWrite(10,HIGH);
// digitalWrite(light,HIGH);
if(SD.begin(4) == 0)
{
Serial.println(F("SD init fail"));
}
//Ethernet.begin(mac, ip, gateway, gateway, subnet); //old
Ethernet.begin(mac, ip, dnsServer, gateway, subnet); //new
//Ethernet.begin(mac, ip);
//delay(5000);
server1.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
digitalWrite(10,HIGH);
delay(2000);
Serial.println(F("Ready. Press f or r"));




//Serial.println ("Starting multiplexer test ...");
pinMode (addressS0, OUTPUT);
pinMode (addressS1, OUTPUT);
pinMode (addressS2, OUTPUT);

pinMode (addressM0, OUTPUT);
pinMode (addressM1, OUTPUT);
pinMode (addressM2, OUTPUT);


pinMode(dir, OUTPUT);
//pinMode(light, OUTPUT);
pinMode(pul, OUTPUT);
digitalWrite(dir, LOW);
digitalWrite(pul, LOW);

pinMode(startPin, INPUT); // sets the digital pin 7 as input
pinMode(endPin, INPUT);
//Actuator setup
pinMode(Act101, OUTPUT);
pinMode(Act102, OUTPUT);
pinMode(Act201, OUTPUT);
pinMode(Act202, OUTPUT);
digitalWrite(Act101, LOW);
digitalWrite(Act102, LOW);
digitalWrite(Act201, LOW);
digitalWrite(Act202, LOW);


pinMode(doorPin1,INPUT);
pinMode(doorPin2,INPUT);
pinMode(targetPin1,INPUT);
pinMode(targetPin2,INPUT);



} // end of setup



void loop (){
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= ethernetRenewInterval) {
previousMillis = currentMillis;
Ethernet.begin(mac, ip, dnsServer, gateway, subnet); //new
server1.begin();
digitalWrite(10,HIGH);
}




EthernetCont();
//Hridoy loop
dOpen=digitalRead(doorPin1);
dClose=digitalRead(doorPin2);
tOpen=digitalRead(targetPin1);
tClose=digitalRead(targetPin2);



if (Serial.available() > 0) {
incomingByte = Serial.read();
}

startVal = digitalRead(startPin); // read the input pin
endVal = digitalRead(endPin); // read the input pin

if(resetCheck==0){
reset();
}
else if(incomingByte=='s' && endVal!=1){
// digitalWrite(light,HIGH);
preScan();
if(dClose==1 && tClose==1){
scan();
stop();

row++;
}
}
//else if(resetCheck==1 && incomingByte=='R'){
else if((endVal==1 && resetCheck==1) || incomingByte=='R'){
stop();
resetCheck=0;
reset();

}

else if(incomingByte == 'f')
{
if(doFTP()) Serial.println(F("FTP OK"));
else Serial.println(F("FTP FAIL"));
}

else if(incomingByte == 'r')
{
readSD();
}
else if(incomingByte == 'O')
{
doorOpen();
}

// else{
// stop();
// row=0;
// incomingByte='A';
// }



}


//Sensor reading methods
int readSensor (const byte which)
{
// select correct MUX channel
digitalWrite (addressM2, (which & 32) ? HIGH : LOW); // MSB
digitalWrite (addressM1, (which & 16) ? HIGH : LOW);
digitalWrite (addressM0, (which & 8) ? HIGH : LOW); // LSB

digitalWrite (addressS2, (which & 4) ? HIGH : LOW); // MSB
digitalWrite (addressS1, (which & 2) ? HIGH : LOW);
digitalWrite (addressS0, (which & 1) ? HIGH : LOW); // LSB
// now read the sensor
return analogRead (common);
}


//Scanning process
void scan(){
File dataFile = SD.open("senor_2.txt", FILE_WRITE);
/* if (dataFile) {
dataFile.println("64,93");
}*/
y=0;
for (byte i = 0; i <64; i++){
Speed=60;
int value= readSensor (i);
if(i>33 && i<64 && i!=42 && i!=41){
thres=800;
}
else{
thres=550;
}
if(value<thres){
Serial.print("1");
if (dataFile) {
dataFile.print("1");
}
}
else{
Serial.print("0");
if (dataFile) {
dataFile.print("0");
}
}

if(i==63 && endVal!=1){
Rev(3);
//row++;
delay(100);
//Serial.print(row);
Serial.println();
if (dataFile) {
dataFile.println();
}
}
else{
Serial.print(",");
if (dataFile) {
dataFile.print(",");
}
}
}
dataFile.close();
// digitalWrite(light,HIGH);
}



//Stepper motor methods
void Rev(int x){
digitalWrite(dir, y);
for(int j=0;j<x;j++){
for(int i=0; i<1525; i++){
digitalWrite(pul,HIGH );
delayMicroseconds(Speed);
digitalWrite(pul, LOW);
delayMicroseconds(Speed);
Distance++;
}
}
if(Distance==x*1600){
Distance=0;
}
}



void stop(){
digitalWrite(pul,LOW);
}



void reset(){
postScan();
if(startVal!=1 && resetCheck==0){
y=1;
Speed= 55;
Rev(1);
}
else if(startVal==1 && dClose==1 && tOpen==1){
row=0;
y=0;
stop();
resetCheck=1;
incomingByte='A';
//Speed=60;
}
}
// Actuator and related methods
void preScan(){
// open the door
if(dClose == 1 && tOpen == 1){
doorOpen();
}
// close the target

if(dOpen == 1){
targetIn(); //after completion, tClose == 1
}

// colose the door

if(tClose == 1 && dClose == 0){
doorClose();
}

// varify
if(tClose==1 && dClose==1){
stopAct();
//isPreScanComplete=true;
}
}



void postScan(){
// open the door
if(dClose == 1 && tClose == 1){
doorOpen();
}
// open the target
if(dOpen == 1){
targetOut(); //after completion, tOpen == 1
}
// close the door
if(tOpen == 1){
doorClose();
}
//Varify
if(tOpen==1 && dClose==1){
stopAct();
//isPreScanComplete=true;
}

}




void doorOpen(){
if(dOpen!=1 &&(tOpen==1 || tClose==1)){
digitalWrite(Act101, HIGH); digitalWrite(Act102, LOW); digitalWrite(Act201, LOW); digitalWrite(Act202, LOW);
}
}
void doorClose(){
if(dClose!=1 &&(tOpen==1 || tClose==1)){
digitalWrite(Act101, LOW); digitalWrite(Act102, HIGH); digitalWrite(Act201, LOW); digitalWrite(Act202, LOW);
}
}
void targetOut(){
if(tOpen!=1 && dOpen==1){
digitalWrite(Act101, LOW); digitalWrite(Act102, LOW); digitalWrite(Act201, HIGH); digitalWrite(Act202, LOW);
}
}
void targetIn(){
if(tClose!=1 && dOpen==1){
digitalWrite(Act101, LOW); digitalWrite(Act102, LOW); digitalWrite(Act201, LOW); digitalWrite(Act202, HIGH);
}
}
void stopAct(){

//Serial.print("stopAct method call");

digitalWrite(Act101, LOW); digitalWrite(Act102, LOW); digitalWrite(Act201, LOW); digitalWrite(Act202, LOW);

}


File fh;

byte doFTP()
{
#ifdef FTPWRITE
fh = SD.open(fileName,FILE_READ);
#else
SD.remove(fileName);
fh = SD.open(fileName,FILE_WRITE);
#endif

if(!fh)
{
Serial.println(F("SD open fail"));
return 0;
}

#ifndef FTPWRITE
if(!fh.seek(0))
{
Serial.println(F("Rewind fail"));
fh.close();
return 0;
}
#endif

Serial.println(F("SD opened"));

if (client.connect(server,21)) {
Serial.println(F("Command connected"));
}
else {
fh.close();
Serial.println(F("Command connection failed"));
return 0;
}

if(!eRcv()) return 0;

client.println(F("USER admin"));

if(!eRcv()) return 0;

client.println(F("PASS admin"));

if(!eRcv()) return 0;

client.println(F("SYST"));

if(!eRcv()) return 0;

client.println(F("Type I"));

if(!eRcv()) return 0;

client.println(F("PASV"));

if(!eRcv()) return 0;

char *tStr = strtok(outBuf,"(,");
int array_pasv[6];
for ( int i = 0; i < 6; i++) {
tStr = strtok(NULL,"(,");
array_pasv[i] = atoi(tStr);
if(tStr == NULL)
{
Serial.println(F("Bad PASV Answer"));

}
}

unsigned int hiPort,loPort;

hiPort = array_pasv[4] << 8;
loPort = array_pasv[5] & 255;

Serial.print(F("Data port: "));
hiPort = hiPort | loPort;
Serial.println(hiPort);

if (dclient.connect(server,hiPort)) {
Serial.println(F("Data connected"));
}
else {
Serial.println(F("Data connection failed"));
client.stop();
fh.close();
return 0;
}

#ifdef FTPWRITE
client.print(F("STOR "));
client.println(fileName);
#else
client.print(F("RETR "));
client.println(fileName);
#endif

if(!eRcv())
{
dclient.stop();
return 0;
}

#ifdef FTPWRITE
Serial.println(F("Writing"));

byte clientBuf[64];
int clientCount = 0;

while(fh.available())
{
clientBuf[clientCount] = fh.read();
clientCount++;

if(clientCount > 63)
{
dclient.write(clientBuf,64);
clientCount = 0;
}
}

if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
while(dclient.connected())
{
while(dclient.available())
{
char c = dclient.read();
fh.write(c);
//Serial.write(c);
}
}
#endif

dclient.stop();
Serial.println(F("Data disconnected"));

if(!eRcv()) return 0;

client.println(F("QUIT"));

if(!eRcv()) return 0;

client.stop();
Serial.println(F("Command disconnected"));

fh.close();
Serial.println(F("SD closed"));
return 1;
}

byte eRcv()
{
byte respCode;
byte thisByte;

while(!client.available()) delay(1);

respCode = client.peek();

outCount = 0;

while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);

if(outCount < 127)
{
outBuf[outCount] = thisByte;
outCount++;
outBuf[outCount] = 0;
}
}

if(respCode >= '4')
{
efail();
return 0;
}

return 1;
}


void efail()
{
byte thisByte = 0;

client.println(F("QUIT"));

while(!client.available()) delay(1);

while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}

client.stop();
Serial.println(F("Command disconnected"));
fh.close();
Serial.println(F("SD closed"));
}

void readSD()
{
fh = SD.open(fileName,FILE_READ);

if(!fh)
{
Serial.println(F("SD open fail"));
return;
}

while(fh.available())
{
Serial.write(fh.read());
}

fh.close();
}

void EthernetCont(){

EthernetClient client1 = server1.available();
if (client1) {
while (client1.connected()) {
if (client1.available()) {
char c = client1.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == 'n') {

///////////////
Serial.println(readString); //print to serial monitor for debuging

//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client1.println("HTTP/1.1 204 Zoomkat");
client1.println();
client1.println();
}
else {
client1.println("HTTP/1.1 200 OK"); //send new page
client1.println("Content-Type: text/html");
client1.println();

client1.println("<HTML>");
client1.println("<HEAD>");
client1.println("<TITLE>Alienide Shooting Range Trigger</TITLE>");
client1.println("</HEAD>");
client1.println("<BODY>");

client1.println("<H1>Alienide Shooting Range Trigger</H1>");

client1.println("<a href="/?on" target="inlineframe">Upload/Download file</a>");
client1.println("<a href="/?scan" target="inlineframe">Run Scan</a>");
client1.println("<a href="/?reset" target="inlineframe">Reset Scanner</a>");
client1.println("<a href="/?off" target="inlineframe">Read from SD Card</a>");
client1.println("<a href="/?delete" target="inlineframe">Delete file from SD Card</a>");


//client1.println("<IFRAME name=inlineframe src="res://D:/WINDOWS/dnserror.htm" width=1 height=1">");
client1.println("<IFRAME name=inlineframe style="display:none" >");
client1.println("</IFRAME>");

client1.println("</BODY>");
client1.println("</HTML>");
}

delay(1);
//stopping client
client1.stop();

///////////////////// control arduino pin
if(readString.indexOf("on") >0)//checks for on
{
//digitalWrite(4, HIGH); // set pin 4 high
Serial.println("Upload or Download file");
if(doFTP()) Serial.println(F("FTP OK"));
else Serial.println(F("FTP FAIL"));
}
if(readString.indexOf("scan") >0)//checks for on
{

Serial.println("Scan");
incomingByte='s';
}

if(readString.indexOf("reset") >0)//checks for on
{

Serial.println("Reset");
incomingByte='R';
}
if(readString.indexOf("delete") >0)//checks for on
{

Serial.println("Delete");
if(SD.remove("senor_2.txt")==1){
Serial.println("File Deleted");;
}
else{
Serial.println("File not found");;
}
}
if(readString.indexOf("off") >0)//checks for off
{
//digitalWrite(4, LOW); // set pin 4 low
Serial.println("Check file in SD");
readSD();
}
//clearing string for next read
readString="";

}
}
}
}
}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.