Skip to content

Instantly share code, notes, and snippets.

@facchinm
Created March 23, 2015 10:00
Show Gist options
  • Save facchinm/aa54d74ceb808dc0467e to your computer and use it in GitHub Desktop.
Save facchinm/aa54d74ceb808dc0467e to your computer and use it in GitHub Desktop.
Test + benchmark for bug #2198
/*
Setup:
A0 <-> 3.3V
A9 <-> A7
A9 <-> 5
A3 -> floating
expected behavior:
digitalRead(A0) == 1
analogRead(A0) == 1023
if (digitalWrite(A9, HIGH) {
analogRead(A7) == 1023
digitalRead(A7) == 1
digitalRead(5) == 1
}
if (digitalWrite(A9, LOW) {
analogRead(A7) == 0
digitalRead(A7) == 0
digitalRead(5) == 0
}
analogRead(A9) ~ 400
*/
int pin_io=A9;
int pin_3v3=A0;
int pin_check_a=A7;
int pin_check_d=5;
int pin_a_spare=A3;
void setup() {
Serial.begin(115200);
Serial.println("Let's go benchmarking!!!");
}
int t_start, sum;
void loop() {
t_start = millis();
pinMode(pin_io, OUTPUT);
for (int i=0; i<100000; i++)
{
digitalWrite(pin_io, HIGH);
}
Serial.print("100000 digitalWrite of the same value took ");
Serial.println((millis()-t_start));
t_start = millis();
pinMode(pin_io, OUTPUT);
for (int i=0; i<50000; i++)
{
digitalWrite(pin_io, HIGH);
digitalWrite(pin_io, LOW);
}
Serial.print("100000 digitalWrite of the different values took ");
Serial.println((millis()-t_start));
t_start = millis();
pinMode(pin_3v3, INPUT);
for (int i=0; i<100000; i++)
{
analogRead(pin_3v3);
}
Serial.print("100000 analogRead on the same pin took ");
Serial.println((millis()-t_start));
t_start = millis();
pinMode(pin_3v3, INPUT);
pinMode(pin_io, INPUT);
for (int i=0; i<50000; i++)
{
analogRead(pin_io);
analogRead(pin_3v3);
}
Serial.print("100000 analogRead on different pins took ");
Serial.println((millis()-t_start));
t_start = millis();
sum = 0;
pinMode(pin_3v3, INPUT);
for (int i=0; i<50000; i++)
{
analogRead(pin_3v3);
sum+= digitalRead(pin_3v3);
}
Serial.print("100000 analogRead+digitalRead on the same pin took ");
Serial.println((millis()-t_start));
if (sum!=50000) {
Serial.println("analogRead+digitalRead is broken!");
}
t_start = millis();
sum = 0;
pinMode(pin_check_a, INPUT);
for (int i=0; i<50000; i++)
{
pinMode(pin_io, INPUT);
analogRead(pin_io);
pinMode(pin_io, OUTPUT);
digitalWrite(pin_io, HIGH);
sum+=digitalRead(pin_check_a);
}
Serial.print("100000 analogRead+digitalWrite (check on A pin) on the same pin took ");
Serial.println((millis()-t_start));
if (sum!=50000) {
Serial.println("analogRead+digitalWrite is broken!");
}
t_start = millis();
sum = 0;
pinMode(pin_check_d, INPUT);
for (int i=0; i<50000; i++)
{
pinMode(pin_io, INPUT);
analogRead(pin_io);
pinMode(pin_io, OUTPUT);
digitalWrite(pin_io, HIGH);
sum+=digitalRead(pin_check_d);
}
Serial.print("100000 analogRead+digitalWrite (check on D pin) on the same pin took ");
Serial.println((millis()-t_start));
if (sum!=50000) {
Serial.println("analogRead+digitalWrite is broken!");
}
t_start = millis();
sum = 0;
pinMode(pin_check_d, INPUT);
for (int i=0; i<50000; i++)
{
pinMode(pin_a_spare, INPUT);
analogRead(pin_a_spare);
pinMode(pin_io, OUTPUT);
digitalWrite(pin_io, HIGH);
sum+=digitalRead(pin_check_d);
}
Serial.print("100000 analogRead+digitalWrite (check on D pin) on different pins took ");
Serial.println((millis()-t_start));
if (sum!=50000) {
Serial.println("analogRead+digitalWrite is broken!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment