This individual documentation is the continuation of week12 when we started designing and assembling our machine - Sauron Tower.
Among other small stuff and issues arised during past week, I decided to focus on DC motor coding.
It must have been pretty easy, in theory. The action that we were trying to program was the movement to the right and left of the "head" where the Sauron eye was placed pressing respectively two buttons "right" and "left". We figured out that we needed a while loop for it and I wrote my first sketch. Our instructor suggested to start with a Led coding, becase the concept is the same. So I wrote this simple code for one Led on Tinkercad.
int Button1 = 5; int Led = 7; int buttonState =1; void setup() { //setup pin modes pinMode(Led, OUTPUT); pinMode(Button, INPUT_PULLUP); } void loop() { buttonState = digitalRead(Button1); while(buttonState == 0) //while the button is pressed { digitalWrite(Led, HIGH); buttonState = digitalRead(Button1); } while(buttonState == 1) //while the button is pressed { digitalWrite(Led, LOW); buttonState = digitalRead(Button1); } }
I used this schematic to wire L293D chip and motor with Arduino, then I added also buttons to control the movement of fan.
As it can be seen in the block diagram above, L293D has one full H bridge and two half H bridges that allow to control the direction of the motor. Theoretically, it must be the same which part of the chip is used to keep control of direction, but in practice it caused issues: once the code was written correctly, the fan would move just in one direction. After a while, my instructor and I tried to switch the chip's side and it worked, even though in datasheet it is specified that both full and half H bridges would work for full control.
This was the code that did not work: the fan would move just in one direction. As explained later by my instructor once entered the while loop it would not exit it, so when the button is pressed the fan moves in one direction when it is not pressed, it stays still without considering the second while loop, consequently the second direction.
#define ENABLE 5 #define DIRA 3 #define DIRB 4 int Button1 = 7; int Button2 = 8; int buttonState = 1; void setup() { //---set pin direction pinMode(ENABLE,OUTPUT); pinMode(DIRA,OUTPUT); pinMode(DIRB,OUTPUT); pinMode(Button1, INPUT_PULLUP); pinMode(Button2, INPUT_PULLUP); Serial.begin(9600); } void loop() { buttonState = digitalRead(Button1); while(buttonState == 0) //while the button is pressed { Serial.println("One way"); analogWrite(ENABLE,180); // enable on digitalWrite(DIRA,HIGH); //one way digitalWrite(DIRB,LOW); buttonState = digitalRead(Button1); } while(buttonState == 1) { Serial.println("stop"); digitalWrite(ENABLE,LOW); // enable on digitalWrite(DIRA,LOW); //one way digitalWrite(DIRB,LOW); buttonState = digitalRead(Button1); } //Another way buttonState = digitalRead(Button2); while(buttonState == 0) //while the button is pressed { Serial.println("Another way"); analogWrite(ENABLE,180); // enable on digitalWrite(DIRA,LOW); //another way digitalWrite(DIRB,HIGH); buttonState = digitalRead(Button2); } while(buttonState == 1) { Serial.println("stop"); digitalWrite(ENABLE,LOW); // enable on digitalWrite(DIRA,LOW); //one way digitalWrite(DIRB,LOW); buttonState = digitalRead(Button2); } }
The suggestion was not to read button state in every loop and to delete digitalWrite(LOW) keeping instead Enable(LOW).
#define ENABLE 5 #define DIRA 3 #define DIRB 4 int Button1 = 7; int Button2 = 8; void setup() { //---set pin direction pinMode(ENABLE,OUTPUT); pinMode(DIRA,OUTPUT); pinMode(DIRB,OUTPUT); pinMode(Button1, INPUT_PULLUP); pinMode(Button2, INPUT_PULLUP); } void loop() { while(digitalRead(Button1)==0) //while the button is pressed { digitalWrite(DIRA,HIGH); //one way digitalWrite(DIRB,LOW); digitalWrite(ENABLE, HIGH); } digitalWrite(ENABLE,LOW); // enable off //Another way while(digitalRead(Button2) == 0) //while the button is pressed { digitalWrite(DIRA,LOW); digitalWrite(DIRB,HIGH); //another way digitalWrite(ENABLE, HIGH); // enable on } }
We had some time limits, but anyway our project arrived to this state of art:
The tower moves and scares people, even us!
Anyway it would be a great idea to work a bit more on its general design: to hide a bit the wires, to paint it in dark colors such black/red-ish shades. Moreover, it would be also a big step forward not just to implement ligts inside of the eye, but make them red and directed like in a real watching tower. One of the design ideas was also to add spikes that re-create the rock texture. Plus, it would be more comfortable to use the controller if the buttons implemented were bigger.