top of page

Learning Arduino IDE

Coding has been the largest challenge for me in the whole project. I spent around 4 months on just the code alone and the next 1.5 placing coding as a lower priority as I built the animal and final circuit.

Image by Arnold Francisca
Coding: Text

I started with the code that was shown in the YouTube video linked on the planning page, that piece of code allowed one potentiometer to control one motor. I then worked my way up to one potentiometer controlling the rate aspect of two motors. I had problems with the intensity aspect of my code both separate and combined with the rate aspect. The motors would not respond to any variable via potentiometer which didn't go away after I switched the motors with a different type. The serial monitor also was unreliable which hindered my progress in coding.

Coding: Text

Main Coding attempts. My other pieces of code are slight variations of these trials.

Trial 1


int mvm = 5; // micro vibration motor is connected with pin number: 5 which is the pvm pin.

int vresistor = A1;

int data = 0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

pinMode (mvm, OUTPUT);

pinMode (vresistor, INPUT);

}


void loop() {

  // put your main code here, to run repeatedly:

data = analogRead(vresistor);

data = map(data, 0, 1023, 0,255);

analogWrite(mvm, data);

Serial.print(data);

}




Trial 2


int mvm1 = 5; // micro vibration motor is connected with pin number: 5 which is the pvm pin.

int mvm2 = 8; // micro vibration motor is connected with pin number: 8 which is the pvm pin.

int vresistor = A1;

int data = 0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

pinMode (mvm1, OUTPUT);

pinMode(mvm2, OUTPUT);

pinMode (vresistor, INPUT);

}


void loop() {

  // put your main code here, to run repeatedly:

data = analogRead(vresistor);

int data1 = map(data, 0, 1023, 0,255);

int data2 = map(data, 0, 1023, 0,255);

analogWrite(mvm1, data1);

delay(3);

analogWrite(mvm2, data2);

delay(3);

Serial.print(data);

}


Trial 3


 Rate code


int mvm1 = 5;

int mvm2 = 10;

int vres = A1;

int data = 0;

void setup() {

pinMode(vres, INPUT);  // put your setup code here, to run once:

pinMode(mvm1,OUTPUT);

pinMode(mvm2,OUTPUT);

}


void loop() {

  data = analogRead(vres);

  int data1 = map(data,0,1023,0,255);

  // put your main code here, to run repeatedly:

digitalWrite(mvm1, HIGH);

digitalWrite(mvm2,LOW);

delay(data1+500);

digitalWrite(mvm1, LOW);

digitalWrite(mvm2,HIGH);

delay(data1+500);

}


Intensity code


int mvm1 = 9;

int mvm2 = 10;

int vresistor = A1;

int data = 0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  

pinMode (mvm1, OUTPUT);

pinMode(mvm2, OUTPUT);

pinMode(vresistor, INPUT);

}


void loop() {

  // put your main code here, to run repeatedly:

  data = analogRead(vresistor);

  int x = map(data,11,655,0,255);

//for(x= 0; x<154; x++){

{

analogWrite(mvm1,x);

analogWrite(mvm2,x);

}

//digitalWrite(mvm1,0);

Serial.println(x);

delay(1000);

}


Trial 4 combined


int mvm1 = 9;

int mvm2 = 10;

int vres = A1;

int data = 0;

void setup(){}

void loop(){

  //put your main code here to run repeatedly:

  data = analogRead(vres);

  int x = map(data,11,655,0,255);

  int data1 = map(data,0,1023,0,255);

  analogWrite(mvm1,x);

  analogWrite(mvm2,x);

  digitalWrite(mvm1,HIGH);

  digitalWrite(mvm2,LOW);

  delay(data1+500);

  digitalWrite(mvm1,LOW);

  digitalWrite(mvm2,HIGH);

  delay(data1+500);

}


Trial idk - final code


int mvm1 = A4;

int mvm2 = A3;

int vres = A0;

int vresistor = A1;

int data = 0;

int datab = 0;

void setup(){

  Serial.begin(9600);

  pinMode(mvm1, OUTPUT);

  pinMode(mvm1, OUTPUT);

  pinMode(vres, INPUT);

  pinMode(vres, INPUT); 

}

void loop(){

  //put your main code here to run repeatedly:

  data = analogRead(vres);

  datab = analogRead(vresistor);

  int x = map(data,11,655,0,255);

  int data1 = map(datab,0,1023,0,255) * 10;

  //digitalWrite(mvm1,HIGH);

  //digitalWrite(mvm2,LOW);

  analogWrite(mvm1,x);

  analogWrite(mvm2,0);

  delay(data1+500);

  //digitalWrite(mvm1,LOW);

  //digitalWrite(mvm2,HIGH);

  analogWrite(mvm1,0);

  analogWrite(mvm2,x);

  delay(data1+500);

  Serial.println("=====");

  Serial.println(data1);

  Serial.println(x);

  delay(50);

}

Coding: Text
bottom of page