You are here

arduino code for pneumatic air piston for table saw sled controlled by an arduino

Last modified: 
Wednesday, May 4, 2016 - 00:22

Heres the arduino code code for the Pneumatic-air-piston-for-table-saw-sled-controlled-by-an-arduino.

Download Link:

The code is a bit of a mess, after everything was working i never whent back to clean it up, sorry.

#include <digitalwritefast.h>
#include <wire.h>
#include <adafruit_gfx.h>
#include <gfxfont.h>
#include <adafruit_ledbackpack.h>
#include <keypad.h>

const int pin7SegmentDisplaySDA = 20; // SDA GREEN data  Analog-4 or Digital 20 for the Mega
const int pin7SegmentDisplaySCL = 21; // SCL WHITE clock  Analog-5 or Digital 21 for the Mega
Adafruit_7segment segmentCounter = Adafruit_7segment();

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
// 36 = yellow
// 37 = green
byte rowPins[ROWS] = { 30, 32, 34, 36 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 31, 33, 35, 37}; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const int pinPiston_Out_Switch = 5; // yellow "HOME" 980X
const int pinPiston_In_Switch = 6; // orange 987X

const int pinPiston_Out_Relay = 3; // yellow
const int pinPiston_In_Relay = 4; // orange

const int pinSwitchCountUp = 10; // yellow
const int pinSwitchCountDown = 11; // blue
volatile char countUPorDOWN = 'u';

const int pinClearCounter = 8; // white // reset button

const int pinHome = 9; // blue
//const int pinReset = 12; // on the board with 1k restore
const int pinStart = 2; // yellow
const int LEDrunning = 7; // green
const int LEDreset = 52; // white
const int LEDblinkHome = 53; // greeb
const int LEDblinkERROR = 50; // BLUE

//const int LEDreset = 50; // BLUE

// revurse
const int manualOut = 40; // blue
const int LEDmanualOut = 41; // yellow

// Forward
const int manualIn = 42; // green 
const int LEDmanualIn = 43; // white

volatile int pistonOut = 0;
volatile int pistonIn = 0;

volatile int triggerError = 0;
volatile int runDone = 1;

volatile int partCounter = 0;
int partCounterSetTo = 0;
char partCounterSetToArry[5];
int partCounterSetToArryIndex=0;
int settingCounter = 0;
char keyPressed = 0;

volatile int cycleRunning = 0;

volatile int isHome = 0;

unsigned long blink_PreviousMillis = 0;
const long blink_Interval = 500;
int blink_ledState = LOW;

const int pinBuzz = 12; // orange
int buzzOn = 1;

void setup() {
  // put your setup code here, to run once:
	pinMode(LEDrunning, OUTPUT);
	pinMode(LEDreset, OUTPUT);
	pinMode(LEDblinkHome, OUTPUT);
	pinMode(LEDblinkERROR, OUTPUT);
	pinMode(LEDmanualOut, OUTPUT);
	pinMode(LEDmanualIn, OUTPUT);
	pinMode(pinPiston_Out_Switch, INPUT_PULLUP);
	digitalWriteFast(pinPiston_Out_Switch, HIGH);
	pinMode(pinPiston_In_Switch, INPUT_PULLUP);
	digitalWriteFast(pinPiston_In_Switch, HIGH);
	pinMode(pinPiston_Out_Relay, OUTPUT);
	digitalWriteFast(pinPiston_Out_Relay, 0);
	pinMode(pinPiston_In_Relay, OUTPUT);
	digitalWriteFast(pinPiston_In_Relay, 0);
	pinMode(pinStart, INPUT_PULLUP);
	pinMode(pinHome, INPUT_PULLUP);
	//pinMode(pinReset, OUTPUT);
	pinMode(pinClearCounter, INPUT_PULLUP);
	pinMode(pinSwitchCountUp, INPUT_PULLUP);
	pinMode(pinSwitchCountDown, INPUT_PULLUP);
	pinMode(manualOut, INPUT_PULLUP);
	pinMode(manualIn, INPUT_PULLUP);
	pinMode(pinBuzz, OUTPUT);

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void loop() {
	//digitalWriteFast(pinPiston_In_Relay, LOW);
	//digitalWriteFast(pinPiston_Out_Relay, LOW);
	if (digitalReadFast(pinPiston_Out_Switch) == 0){
		isHome = 1;
		digitalWriteFast(LEDblinkHome, HIGH);
	} else {
		isHome = 0;
		if(cycleRunning == 0){
			char ledArray[2];
			ledArray[1] = LEDblinkHome;
		} else {
			digitalWriteFast(LEDblinkHome, LOW);
	// Make sure values are not open when they should not be...
	if (digitalReadFast(pinPiston_Out_Switch) == 0){
		digitalWriteFast(pinPiston_Out_Relay, LOW);
	if (digitalReadFast(pinPiston_In_Switch) == 0){
		digitalWriteFast(pinPiston_In_Relay, LOW);
	if (digitalReadFast(pinPiston_Out_Relay) == 1){
		ledOnOff(true, LEDmanualOut);
	} else {
		ledOnOff(false, LEDmanualOut);
	if (digitalReadFast(pinPiston_In_Relay) == 1){
		ledOnOff(true, LEDmanualIn);
	} else {
		ledOnOff(false, LEDmanualIn);
	// Manual OUT
	if (digitalReadFast(manualOut) 

LOW && digitalReadFast(pinPiston_Out_Switch)

HIGH && cycleRunning == 0){ cycleRunning = 0; extentPiston(); } else if (digitalReadFast(manualOut)

HIGH && cycleRunning

0){ digitalWriteFast(pinPiston_Out_Relay, LOW); } // Manual IN if (digitalReadFast(manualIn)

LOW && digitalReadFast(pinPiston_In_Switch)

HIGH && cycleRunning == 0){ cycleRunning = 0; retractPiston(); } else if (digitalReadFast(manualIn)

HIGH && cycleRunning

0){ digitalWriteFast(pinPiston_In_Relay, LOW); } // CYCLE START //if (digitalReadFast(pinStart)

LOW && isHome

1 && cycleRunning

0 && triggerError

0){ if (digitalReadFast(pinStart)

LOW && isHome

1 && cycleRunning == 0){ //if (digitalReadFast(pinStart)

LOW && cycleRunning

0){ delay(5); triggerError = 0; isHome = 0; cycleRunning = 0; //sendHome(); //stop(); startCycle(); delay(30); runDone = 0; cycleRunning = 1; } else if(digitalReadFast(pinStart)

LOW && isHome

0 && cycleRunning == 0){ ledOnOff(true, LEDblinkERROR); } else { if(triggerError == 0){ ledOnOff(false, LEDblinkERROR); } else { //ledOnOff(false, LEDblinkERROR); } //ledOnOff(false, LEDblinkERROR); } // END OF CYCLE if (digitalReadFast(pinPiston_Out_Switch)

LOW && digitalReadFast(pinPiston_In_Relay)

LOW && cycleRunning == 1){ //if (digitalReadFast(pinPiston_Out_Switch)

LOW && cycleRunning

1){ //isHome = 1; partCounter = count(partCounter); displayCount(partCounter); stop(); cycleRunning = 0; } // AUTO EXTENT PISTION WHILE IN CYCLE if (digitalReadFast(pinPiston_In_Switch)

LOW && cycleRunning

1){ extentPiston(); } // COUNTER SWTICH if (digitalReadFast(pinSwitchCountUp) == LOW){ countUPorDOWN = 'd'; } else if (digitalReadFast(pinSwitchCountDown) == LOW ){ countUPorDOWN = 'u'; } else { countUPorDOWN = 'n'; } // HOME if (digitalReadFast(pinHome) == LOW){ isHome = 0; cycleRunning = 0; sendHome(); } if (partCounter

0 && countUPorDOWN

'd' && runDone == 0){ segmentCounter.blinkRate(1); triggerError = 1; } else { triggerError = 0; } // RESET ARDUINO if (digitalReadFast(pinClearCounter)

LOW && countUPorDOWN

'n'){ //pinMode(pinReset, OUTPUT); // sets the digital pin as output //digitalWriteFast(pinReset, LOW); // sets the LED off ledOnOff(true, LEDreset); delay(5); //digitalWriteFast(pinReset, HIGH); resetFunc(); //digitalWriteFast(pinReset, HIGH); //resetFunc(); //call reset } if (digitalReadFast(pinClearCounter) == LOW && countUPorDOWN != 'n'){ zeroDisplay(); runDone = 1; } char key = kpd.getKey(); if(key && cycleRunning == 0) { //Serial.println(key); keyPadPressed(key); } if (digitalReadFast(pinClearCounter)

LOW || (key && key

'C')){ Serial.println(key); ledOnOff(true, LEDreset); } else { ledOnOff(false, LEDreset); } if(settingCounter == 1) { segmentCounter.blinkRate(1); displayCount(partCounterSetTo); } else { if(runDone

0 && triggerError

1) { segmentCounter.blinkRate(1); displayCount(partCounter); } else { segmentCounter.blinkRate(0); displayCount(partCounter); } } //if (isHome

0 && cycleRunning

0){ //char ledArray[3]; //ledArray[0] = LEDblinkHome; //ledArray[1] = LEDblinkERROR; //makeLEDblink(ledArray); //} else { //digitalWriteFast(LEDblinkHome, LOW); //if (triggerError == 0) { //digitalWriteFast(LEDblinkERROR, LOW); //} //} if (triggerError == 1){ char ledArray[2]; ledArray[1] = LEDblinkERROR; makeLEDblink(ledArray); } //if (isHome == 1){ //digitalWriteFast(LEDblinkHome, LOW); //} else { //digitalWriteFast(LEDblinkHome, HIGH); //} } void makeLEDblink_(char led[]){ // try and loop over the LEDs passed to we can blink more then one at a time for( int a = 0; a < (sizeof(led)); a = a + 1 ){ unsigned long blink_CurrentMillis = millis(); if (blink_CurrentMillis - blink_PreviousMillis >= blink_Interval) { // save the last time you blinked the LED blink_PreviousMillis = blink_CurrentMillis; // if the LED is off turn it on and vice-versa: if (blink_ledState == LOW) { blink_ledState = HIGH; } else { blink_ledState = LOW; } digitalWriteFast(led[a], blink_ledState); } } } void makeLEDblink(char led[]){ unsigned long blink_CurrentMillis = millis(); if (blink_CurrentMillis - blink_PreviousMillis >= blink_Interval) { // save the last time you blinked the LED blink_PreviousMillis = blink_CurrentMillis; // if the LED is off turn it on and vice-versa: if (blink_ledState == LOW) { blink_ledState = HIGH; } else { blink_ledState = LOW; } for( int a = 0; a < (sizeof(led)); a = a + 1 ){ digitalWriteFast(led[a], blink_ledState); } } } void keyPadPressed(char keyIs){ switch (keyIs) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': addKeyPadNumber(keyIs); break; case 'A': // break; case 'B': // break; case 'C': // clear zeroDisplay(); break; case 'D': deleteKeyPadChr(); break; case '*': exitKeyPad(); break; case '#': poundKey(); break; } // Serial.println(keyIs); keyPressed = keyIs; } void poundKey(){ if(settingCounter == 1){ settingCounter = 0; partCounter = partCounterSetTo; partCounterSetTo = 0; partCounterSetToArryIndex = 0; memset(partCounterSetToArry, 0, sizeof(partCounterSetToArry)); } } void exitKeyPad(){ settingCounter = 0; partCounterSetTo = 0; partCounterSetToArryIndex = 0; memset(partCounterSetToArry, 0, sizeof(partCounterSetToArry)); } void addKeyPadNumber(char numIs){ settingCounter = 1; if (partCounterSetToArryIndex == 0){ memset(partCounterSetToArry, 0, sizeof(partCounterSetToArry)); partCounterSetTo = 0; } partCounterSetToArry[partCounterSetToArryIndex] = numIs; partCounterSetTo = atoi(partCounterSetToArry); partCounterSetToArryIndex +=1; } void deleteKeyPadChr(){ //Serial.println(partCounterSetToArryIndex, DEC); if (partCounterSetToArryIndex > 0){ settingCounter = 1; partCounterSetToArryIndex -=1; //Serial.print("BIGGER new index count = "); //Serial.println(partCounterSetToArryIndex, DEC); char TEMPpartCounterSetToArry[5]; //TEMPpartCounterSetToArry = partCounterSetToArry; memcpy( TEMPpartCounterSetToArry, partCounterSetToArry, 5 ); //Serial.print("partCounterSetToArry index 1 = "); //Serial.print(partCounterSetToArry[1]); //Serial.println(" ----- "); memset(partCounterSetToArry, 0, sizeof(partCounterSetToArry)); for( int a = 0; a < partCounterSetToArryIndex; a = a + 1 ) { //Serial.print("a = "); //Serial.println(a); //Serial.print("tmp = "); //Serial.println(TEMPpartCounterSetToArry[a]); partCounterSetToArry[a] = TEMPpartCounterSetToArry[a]; } partCounterSetTo = atoi(partCounterSetToArry); } } void sendHome(){ //Serial.println("HOMING OUT"); cycleRunning = 0; digitalWriteFast(pinPiston_In_Relay, LOW); //digitalWriteFast(pinPiston_Out_Relay, LOW); if (digitalReadFast(pinPiston_Out_Switch) == LOW){ isHome = 1; digitalWriteFast(pinPiston_Out_Relay, LOW); ledOnOff(false, LEDmanualOut); } if (isHome == 0){ digitalWriteFast(pinPiston_Out_Relay, HIGH); ledOnOff(true, LEDmanualOut); sendHome(); } } void startCycle(){ ledOnOff(true, LEDrunning); digitalWriteFast(pinPiston_In_Relay, LOW); delay(30); digitalWriteFast(pinPiston_Out_Relay, LOW); delay(30); retractPiston(); // cut delay(30); } void extentPiston(){ digitalWriteFast(pinPiston_In_Relay, LOW); digitalWriteFast(pinPiston_Out_Relay, LOW); digitalWriteFast(pinPiston_Out_Relay, HIGH); } void retractPiston(){ digitalWriteFast(pinPiston_In_Relay, LOW); digitalWriteFast(pinPiston_Out_Relay, LOW); digitalWriteFast(pinPiston_In_Relay, HIGH); } void stop(){ digitalWriteFast(pinPiston_Out_Relay, LOW); digitalWriteFast(pinPiston_In_Relay, LOW); delay(10); digitalWriteFast(pinPiston_Out_Relay, LOW); digitalWriteFast(pinPiston_In_Relay, LOW); ledOnOff(false, LEDrunning); } char count(int num){ if (countUPorDOWN == 'u'){ num = (num + 1); } if (countUPorDOWN == 'd'){ num = (num - 1); } return num; } void displayCount(int num){ segmentCounter.print(num); segmentCounter.writeDisplay(); if(num == 0){ buzzard(true); } } void zeroDisplay(){ partCounter = 0; segmentCounter.print(0000, DEC); segmentCounter.writeDisplay(); buzzard(false); } void buzzard(bool l){ if(l){ digitalWriteFast(pinBuzz, HIGH); buzzOn = 1; } else { digitalWriteFast(pinBuzz, LOW); buzzOn = 0; } } void ledOnOff(bool l,int LED){ if(l){ digitalWriteFast(LED, HIGH); } else { digitalWriteFast(LED, LOW); } }
Mobile QR Barcodes: 
Save Article as PDF
Current URL Address

Mobile: File Download for - AirPiston.ino

File Downloads: 


ShikhaSharma's picture

After a long hold up, the time has at long last started beating your entryway when you #need to #move out of your present home and move into another flat suite. In any case, the attempt of #home #moving start with a one area then onto the accompanying is truly not a cakewalk and does not appear, in every way, to be as clear as you think. There are various exercises that must be managed while playing out this work, which is unquestionably, a champion among the most upsetting and dull errands that each individual needs to association with any rate once in their lives. On the off chance that a man who does not have the objectives of having aptitudes and experience of supervising such an unprecedented business already, it is fitting that he ought not try his hands in playing out the same in light of the way that the result would be a hostile one and not to reinforce him. Just a strong Packers and Movers in Jaipurcan help the clients when the condition takes an undesirable turn and will be harmed. Packers And Movers In Jaipur On the off chance that you start your interest for a pro and #best home moving associations, at that point you are at right stop and furthermore #most expected stage. group has secured and made course of action of trusted and expert pressing and moving affiliations. They give most reasonable moving associations with help of solid structure in India. The going with walk is to simply fill moving unassuming parts and present the deals. We ensure that the associations of best #moving affiliation will come to you with best Moving Quotes. Crushing, #stacking, and moving, depleting, exhausting and fixing up are the means which must be considered with true blue slants of quietness and applying the endeavours so everybody of them gets executed reasonably. In such way, you should come into contact with the capable Movers and Packers in Jaipur so you can get a put stock in brand to deal with your essential things.  Why Proficient Pressing And Moving Affiliation Is Best Decision!!The advancement associations rendered by the #best #home #moving association suppliers are not restricted to offering help to the clients as for moving their homes however remarkable associations too much like #office #moving, #business #moving, #auto #moving, #bicycle #moving, pet migration, warehousing and the quick overview does not get in contact at an end. They besides consider the end clients' demand to move starting with a one area then onto the accompanying inside a comparable city or interstate moving. In like way, the essentials for overall Packers and Movers in Jaipur are overseen by a solid moreover best home moving affiliation. The standard strategy for thinking behind a moving affiliation is to set up its branch in different parts of the nation to affirm that the end clients don't get any issues regarding settling in their favoured zone.  There is no need for you to hold any nerves in like manner as your vital things are concerned in light of the way that they will unmistakably be overseen by the pro and sensible stars in a perfect and #best way the way you anticipate that them will be. In the event that in light of some reason or the other, the stock come into contact with flame and face harms, the moving confirmation degree is given to the end clients. You should contact that enables the clients to accomplish the trusted home moving relationship out there which are orchestrated in their general locale. #packers and #movers #jaipur #charges,packers and movers jaipur quotes,packers and movers jaipur price cost,packers and movers jaipur reviews,cheap and best packers and movers jaipur,packers and movers rate list chart,packers and movers jaipur local shifting charges approx,car transportation in jaipur,Bike shifting in jaipur,Top Packers And Movers in jaipur,Movers Packers jaipur,Home relocation,Household shifting Source: Packers And Movers Jaipur to Noida  Packers And Movers Bundi     

We Provide Best Packers And Movers Jaipur List for Get Free Best Quotes, Compare Charges,
Save Mone And Time,Household Shifting Services @

When you relocate into appealing house a few years back, things seemed a little differ - or may be a lot disparate! Not that your family is growing, it might be the right time to move to a bigger #Vignesh Shivan‬, ‪Nayantara‬, ‪Naanum Rowdy Dhaan‬, ‪New York City‬, ‪Thaana Serndha Kootam‬‬ house in Kolkata. When you are relocating to accommodate a growing family, there is a lot to consider ---- so Packers and Movers in Kolkata have compiled a list to help you get started.  How you’ll know it’s instant to upsize → living in happily married life once a stage come when you realize you are growing up in size. What to do in such situations? Some families are still there comprising with needs and living in middle structures. When you realize it’s time to growing up the simple step you want to do is let your house be get upsized by moving with Packers and Movers in Kolkata to a new house where you all can live comfortable and the most crucial thing you must be satisfy with this. Safe Movers And Packers In Kolkata Date is fixed; time schedule for moving → when you have recognize already that you are pregnant or either you are going to be a father now; a date is just fixed beyond which the matter for this will be ended with a happy ending. When you have already been alert by the doctor that within this duration you can be stepping on the door of growing family. Before this instant you have a time to plan up for the move and manage all the essential requirements to be needed while managing this all. Packers and Movers in Kolkata has defined some tips above this point and below this point; has a focus on them and find out what works are there to be finished early the time of pregnancy. Get a #safe zoned covered with safety of new child and mother with Safe and secure Packers and Movers Kolkata How much can you afford → working job consist of limited budget area where you have to manage everything and also to care about your family. When updated with the things of #need to be #move because of homologize of growing family; what is the most important thing to be focused by you? “Budget” according to Packers and Movers Kolkata charges. First look out the new apartment of big size in Kolkata with suitable locality and then make a budget you can spend in buying the apartment and then can spend on Packers and Movers of KolkataPackers and Movers Kolkata charges are right and genuine providers and will serve you reliable quotes. Compare if you have doubt; tell your budget to Packers and Movers in Kolkata and get services within this with best facilities.   Do you want to update your belongings → shifting to a new house and then the icing on the cake is stepping into new life as growing family good news”? Would you like to prefer to update your belongings like furniture’s and other accessories because to maintain the proper hygiene and facility of new comer. Then mark the # Nia Sharma‬, ‪Fear Factor: Khatron Ke Khiladi words by safe and best Packers and Movers Kolkata to get rid of such items and buy new one. Your new house should be a new look not a compulsion just a suggestion; Packers and Movers of Kolkata is giving you. Try to sell out the old stuffs not using and in replacement of this buy something useful and good for your new house or either don’t buy anything but to reduce quotation price this will help you.  Hire the right movers and packers @Reliable and genuine packers and movers Kolkata → when you are at cream stage of life where a single mistake can mess everything. Don’t you want a safe and secure transaction in Kolkata. Than hire right movers at right package only with Safe and fats Packers and Movers Kolkata at very affordable rates @Packers and Movers Kolkata price rates. A right mover can make everything awesome and a wrong mover will messed up with your things. Move in safe zone; move with Packers and Movers in Kolkata for best results and full security and assurance of ought to take care of you and your belongings.  Well these were some the tips by Packers and Movers of Kolkata. As Packers and Movers in Murshidabad is concern about your family and your smile we want you to travel #safe with insured hands. That is the reason why Safe and best packers and movers Kolkata is accommodating you for cheap and #best rate services at bunch of happiness. Let the new world gets your new starting with new family growing soon; get the best service range at cheap price quotes @Packers and Movers Kolkata price quotesPackers and Movers in Kolkata is giving you the best opportunity to shift safe and easy with cheap rates try it now as soon you requisite. #packers and #movers #kolkata #charges,packers and movers kolkata quotes,packers and movers kolkata price cost,packers and movers kolkata reviews,cheap and best packers and movers kolkata,packers and movers rate list chart,packers and movers kolkata local shifting charges approx,car transportation in kolkata,Bike shifting in kolkata,Top Packers And Movers in Kolkata,Movers Packers Kolkata, Home relocation,Household shifting Source: Packers And Movers Midnapore  Packers And Movers Kolkata to ahmedabad     

We Provide Best Packers And Movers Kolkata List for Get Free Best Quotes, Compare Charges,
Save Money And Time, Household Shifting Services @

Add new comment