While Programming
Programming was really a big challenge for me. It's been a really hard time in project development week. There were a lot of problems and some successes. In particular, it was really difficult in programming for I2C networking. As a result, I thought that I could learn a lot of things unexpectedly and enjoy it.
Current Codes
This code shows that the motor runs when the button is pressed using the current I2C networking, and the motor turns off when the button is pressed again.
Sensors & Button Board Code
#include <Wire.h> const int buttonPin = 10; int buttonState = 0; int lastButtonState = 1; void setup() { Wire.begin(); pinMode(buttonPin, INPUT); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState != lastButtonState) { if (buttonState == 1) { Wire.beginTransmission(2); Wire.write('p'); Wire.endTransmission(2); } lastButtonState = buttonState; } delay(20); }Motor & Heater Board Code
#include <Wire.h> int STEP = 9; int DIR = 10; boolean turn = false; void receiveEvent(int howMany) { //Read Transfer Data while (Wire.available() > 0) { int A = Wire.read(); Serial.print(A); turn = !turn; } } void setup() { Wire.begin(2); //slave address Wire.onReceive(receiveEvent); //Call the reciveEvent function when data is sent Serial.begin(9600); pinMode(STEP,OUTPUT); pinMode(DIR,OUTPUT); } void loop() { if(turn == true) { motergo(); } delay(20); } void motergo() { digitalWrite(DIR,HIGH); // + Direction for(int x = 0; x < 200; x++) { // 200 pulses one revolution digitalWrite(STEP,HIGH); delayMicroseconds(1200); digitalWrite(STEP,LOW); delayMicroseconds(1200); } }